vim file editor

Vim is an enhanced version of Vi (an increase of regular expressions to find, such as multi-window editing functions on the basis of Vi), using Vim program development will be more convenient.

Want to learn the difference between Vi and Vim, you can enter in the Vim command mode  :help vi_diff, you can see a summary of the difference between the two.

1, the installation

yum install vim

Do you want to manually enter the process of "y" is omitted, it is desirable to install automatic, you can use the following command: yum -y install vim

When the command line, enter "Vim" display screen shown in FIG. 1, as a Vim successful installation.

2, operating mode

When using Vim editing file, there are three modes of operation, namely, command mode, input mode, and edit mode, three modes of operation can easily switch:

 

 

Use vi or vim enter command mode , enter the following command to achieve different functions:

  • i is then inserted in the input text of the current cursor position, the cursor text corresponding to the right
  • I then enters the first insertion of text in the line of the cursor line, the first line is the first non-blank character in the line, corresponds to the beginning of the line the cursor moves to the execute command i
  • o Insert a new row below the cursor line. Cursor in the first empty row, waiting to enter text
  • O insert a new row in the row above the cursor. Cursor in the first row blank line, waiting to enter text
  • subsequently inserting a text input after the current cursor position
  • A subsequent insertion of text in the input end of the line the cursor is located, corresponding to the cursor to the end of the line then execute a command

Edit mode

Edit mode for carrying out the preservation of the contents of the specified file, find or replace operation.

Vim switch to edit mode so that the method is in the command mode state, press ":" button , then the bottom left of the Vim window appears a ":" symbol, which is can be operated enter the relevant instruction.

After the instruction execution Vim automatically return to command mode. If you want to return directly to the command mode, press Esc to.

3, the basic operation

vim filename or open a new file, and the cursor on the first line of the header portion 
vim - file crash when r filename last restoration open vim 
vim - R & lt filename specified by the read-only file into Vim editor 
vim + filename to open the file, and the last line of the cursor header
 VI + n filename to open the file, and the cursor of the n-th row header
 VI + / pattern file filename Jian play, and with the first cursor pattern matching position
 vi -c filename before the command edit the file, perform the specified command

Insert text

Entering commands from input mode to edit mode, press I, i, O, o, A, a key to complete the like, the use of different keys at different positions, the cursor is located:

i inserted in the current cursor position is then entered text, the text corresponding to the right cursor 
I insert text header row is then entered in the cursor line, the first line is the first non-blank character in the line, corresponding to the cursor i is moved to execute the first command line 
o insert a new row below the cursor line. Cursor in the first empty row, waiting to enter text 
O insert a new line above the line where the cursor is. In the beginning of the line cursor blank line, waiting for the input text 
a subsequently entered text is inserted after the current cursor position 
A is then inserted into the text input cursor in the line end of the line, corresponds to the end of the line the cursor and then execute a command

Find text

/ Abc abc search string from the cursor position forward
 / ^ abc abc as the beginning of the line to find the row
 / abc $ abc look at the row end of the line
 ? Abc mainly from the cursor backward to find the string abc 
the n-the same repeat the last command lookup direction 
N in the opposite direction to repeat the last find specific

If you want to ignore case when searching, enter the command ": set ic", enter the command to adjust the back ": set noic"

If the special symbols appear in the string, you need to add the escape character "\." Common special symbols have \, *,?, $ And so on. If these characters appear, for example, to find the string "$ 10", you need to enter "/ 10 \ $" in command mode.

Replace Text

r replace the character at the cursor position 
R starts to replace characters from the cursor position, which is the input content will overwrite long text behind the like, press the "Esc" can end 
: S / A1 / A2 / G the current cursor line a1 a2 replace all used 
: n1, N2S / a1 / a2 / G into the document n1 n2 all rows a1 a2 are replaced by 
: G / a1 / a2 / G all files a1 a2 are replaced by

To a file of all the "root" is replaced with "liudehua", there are two input commands, respectively:

: . 1 , $ S / the root / liudehua / G 
or 
: % S / the root / liudehua / G 

from the first row to the last row, i.e. to find text represented by "root", and then replace "liudehua".

If you have just become a command  :10,20 s/root/liudehua/g, only replacing from line 10 to line 20 of the "root".

Delete Text

x delete the cursor position character
 dd     delete the line where the cursor 
ndd deletes the current line (including this line) after n lines of text 
all content dG rows remove the cursor until the end of the document 
D to delete the cursor position to the end of the line 
: a1, a2d text function from a1 to a2 row lines

The deleted text does not really deleted, they are placed in the clipboard. Move the cursor to the specified position, press the "p" key, you can just delete the content and paste it here.

Copy the text

p Copy the contents of the clipboard to the cursor 
P copy the contents of the clipboard to the front cursor 
yy line where the cursor to the clipboard, this command can be added before the number n, can be copied multiple rows 
yw the word cursor position copy to clipboard

Other shortcuts

The two lines are connected, for example, the following two lines of text file, which now need to be merged into one line (in fact, the line breaks between the two lines removed). You can press the "J" key directly in command mode.

 

 

If you accidentally accidentally deleted the content of the document, you can undo command just executed through the "u" key. To undo multiple operations just can press it several times "u" key .

Save and exit the text

: wq to save and exit Vim editor 
: wq !     to save and forced to quit Vim editor 
: q exit without saving Vim Editor 
: q !     is not saved, and forced to quit Vim editor 
: w     save but does not exit Vim editor 
: w !     forced save text 
: w filename to save the file filename
 x! Save the text and exit the editor Vim, vim command a more general 
ZZ exit Vim editor

Note that, "w!" And "wq!" And other similar instructions, are typically used when the file does not have write permission (display readonly, 12), but if you are the owner of a file or root users, it can be enforced.

4, the cursor

The table all the shortcuts are in command mode (default) directly.

Vim shortcut arrow keys

  • h move the cursor one left
  • j line cursor (carriage return linefeed) downward, the cursor is moved downwardly
  • k cursor up one line (i.e., moved upward)
  • l Move the cursor to the right one

Vim move the cursor to the word as a unit

In some cases, you may need to quickly move the cursor to a location in a row, the cursor will move to the word as a unit it will be very convenient.

  • w W or move the cursor to the next word in the first word
  • b B or move the cursor to the previous word of the first word
  • e E, or move the cursor to the next word end of word
  • nw nW or n is a number, n represents the cursor to the right word
  • or n is the number nB of nb, n represents the cursor moves to the left word

Vim move the cursor to the beginning or end of the line

  • 0 ^ cursor to the beginning or to the current row
  • $ Move the cursor to the end of the current row
  • n $ move the cursor to the end of the current line only row n rows, n is an number

Move the cursor to the specified character Vim

Line, if you need to move the cursor to a feature of the current line of characters, you can use the table 4 shortcuts.

  • fx current line cursor to the next character at x
  • Fx current line cursor to the next character at x

Vim cursor to the specified line

  • gg cursor to the beginning of the file
  • Move the cursor to the end of the document G
  • nG cursor moves to the n-th row, n is a number
  • : N shortcuts used under the Edit mode, you can quickly define the cursor to the beginning of the specified line

Vim cursor to the matching parentheses at

When programmers editor, the cursor is often moved to a "(" matching ")" (for [] and {} is the same) at the headache. Vim which provides a very easy to find matching brackets command, which is "%."

For example, the script file /etc/init.d/sshd (after copying best practice, Caution is the ship), to quickly position the cursor to the first row 49 "{" corresponding "} ", the cursor can be positioned to" { "at, and then use the"% "command, so that it is positioned in the"} "at, as shown in FIG.

Use "%" command to locate brackets

 

Guess you like

Origin www.cnblogs.com/pacino12134/p/11482875.html