Home  >  Article  >  Operation and Maintenance  >  Summarize some common commands of VIM

Summarize some common commands of VIM

零下一度
零下一度Original
2017-06-27 11:20:152063browse

There are many detailed commands in VIM. We have selected some commonly used entry-level commands, which are enough for daily code editing work. If you need to use other commands in the future, it is not too late to check again.

vim generally has three editing modes, namely insert mode, normal mode, and last line mode.

The following are mainly operations in normal mode. Operations in other modes will indicate the relevant modes

1.1 Moving the cursor

h------>Move left each time you press

j------>Down each time you press Move

k------>Move up each time

##l------>Every time Press to move right

1.2 Entering and exiting vim

Press < ;Esc> key to enter normal mode

Then enter the following methods to exit

:q           #不保存并退出vim:q!          #强制退出:wq           #保存文件并退出:x            #相当于:wq

1.3 Delete text editing

In normal mode (Normal mode), you can press the x key to Delete the character at the cursor position.

1.4 Insertion of text editing

Enter insert mode in normal mode, you can There are several methods:

You can press the i key to insert text at the cursor.

Press the a key to insert text behind the cursor.

Press the capital A key to insert text after the last character on the line where the cursor is.

1.5 Text Editing Add

Press the A key and click on the line where the cursor is. Add

to the end of the text. Press the a key and add

after the cursor position. 2.1 Delete command

Enter dw to delete from the cursor to the end of a word.

2.2 About commands and objects

Many commands that change text are composed of an operation The symbol is composed of an action.

The format of the delete command using the delete operator d is as follows:

 dmotion

Among them:

## d   -Delete operator

 motion                                    die through in >> motion     - Delete operator

##A short action list:

w - From the current cursor position to the beginning of the next word, excluding its first characters.

 e - From the current cursor position to the end of the word, including the last character.

 $ - From the current cursor position to the end of the current line.

2.3 Use counting to formulate actions

Enter the number n before the action, it will It repeats n times.

Enter 2w to move the cursor back 2 words.

Enter 3e to move the cursor backward to the end of the 3rd word.

Enter 0 (number zero) to move the cursor to the beginning of the line.

2.4 Use counting to delete more

Enter the number n when using the operator , which can be repeated n times.

For example: operation number (number) motion

d2w You can delete 2 words.

2.5 Operate the entire line

Enter dd to delete a current line and save it to the register, which functions like a "cut" operation and can be used in conjunction with the p operation.

######

2.6 Undo commands

Enter u to undo the last executed command, and enter U to undo modifications to the entire line.

Use Ctrl + r to undo a previous undo command.

3.1 Insert class command

Enter p to "cut" the last time The content is placed after the cursor.

3.2 Replacement command

Move the cursor to the character position to be modified and enter r and a character to replace the character at the cursor position.

3.3 Change command (c command, meaning "change")

To change text until the end of a word, enter ce.

3.4 Use c to change more

 c [number] motion

The action parameter motion is the same, it can be w, e, $d.

4.1 Positioning and file status

Enter Ctrl + g to display the currently edited file The line position of the current cursor and file status information.

Enter capital G to jump directly to the last line of the file.

Enter nG to jump to the line with line number n.

Enter gg to jump to the first line of the file.

4.2 Search command

Enter / plus a string, you can Find this string in the current file. To find the next string, press the n key. To search in reverse, enter a capital N.

If you want to search in reverse, enter ? replace / .

4.3 Search for matching brackets

Position the cursor at the bracket to be matched , enter % to find another bracket that matches it), ], }.

4.4 Replacement command

Enter: s/old/new, you can replace it once The string at old in this line is the string at new.

Enter :s/old/new/g to replace all the strings at old in the line with the strings at new.

Input:#,#s/old/new/g, where #,# represent the line numbers of the starting line and the ending line of the replacement operation.

Enter :%s/old/new/g to replace every matching string in the entire file.

Enter :%s/old/new/gc, each matching string in the entire file will be found, and each matching string will be prompted whether to replace it.

5.1 How to execute external commands in VIM

Enter:! Then enter an external command immediately Execute this external command.

For example, :!ls + Enter, this command lists the contents of your current directory.

5.2 More information about saving files

To save changes to a file, enter :w filename.

5.3 A selective save command

Select by pressing the v key To save part of the file, then enter:w file name to save the selected content to the target file.

5.4 Extract and merge files

To insert another file into the current file For the content, please enter:r file name.

6.1 Open the class command

Enter o, a new file will be opened under the cursor row and enter insert mode.

Enter a capital O to open a new line above the cursor and enter insert mode.

6.2 Another version of the substitution command

Enter uppercase R, you can Replace multiple characters in a row.

6.3 Copy and paste text

Use operator y to copy text and p to paste text.

Enter yy to copy the line where the cursor is.

can be used with the action parameter motion:

Enter yw to copy a word.

The above is the detailed content of Summarize some common commands of VIM. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn