130多個基本的Vim命令
在程序員中vi和vim是非常流行的文本編輯器 。 這是之前作者寫了一篇名為“100 個 Vim命令每個程序員都應該知道”的一個返工,更新版本。Enjoy!
Basics
| :e filename |
Open filename for edition |
</tr>
| :w |
Save file |
</tr>
| :q |
Exit Vim |
</tr>
| :q! |
Quit without saving |
</tr>
| :x |
Write file (if changes has been made) and exit |
</tr>
| :sav filename |
Saves file as filename |
</tr>
| . |
Repeats the last change made in normal mode |
</tr>
| 5. |
Repeats 5 times the last change made in normal mode |
</tr>
</tbody>
</table>
Moving in the file
| k or Up Arrow |
move the cursor up one line |
</tr>
| j or Down Arrow |
move the cursor down one line |
</tr>
| e |
move the cursor to the end of the word |
</tr>
| b |
move the cursor to the begining of the word |
</tr>
| 0 |
move the cursor to the begining of the line |
</tr>
| G |
move the cursor to the end of the line |
</tr>
| gg |
move the cursor to the begining of the file |
</tr>
| L |
move the cursor to the end of the file |
</tr>
| :59 |
move cursor to line 59. Replace 59 by the desired line number. |
</tr>
| 20| |
move cursor to column 20. |
</tr>
| % |
Move cursor to matching parenthesis |
</tr>
| [[ |
Jump to function start |
</tr>
| [{ |
Jump to block start |
</tr>
</tbody>
</table>
Cut, copy & paste
| y |
Copy the selected text to clipboard |
</tr>
| p |
Paste clipboard contents |
</tr>
| dd |
Cut current line |
</tr>
| yy |
Copy current line |
</tr>
| y$ |
Copy to end of line |
</tr>
| D |
Cut to end of line |
</tr>
</tbody>
</table>
Search
| /word |
Search word from top to bottom |
</tr>
| ?word |
Search word from bottom to top |
</tr>
| * |
Search the word under cursor |
</tr>
| /\cstring |
Search STRING or string, case insensitive |
</tr>
| /jo[ha]n |
Search john or joan |
</tr>
| /\< the |
Search the, theatre or then |
</tr>
| /the\> |
Search the or breathe |
</tr>
| /\< the\> |
Search the |
</tr>
| /\< |.\> |
Search all words of 4 letters |
</tr>
| /\/ |
Search fred but not alfred or frederick |
</tr>
| /fred\|joe |
Search fred or joe |
</tr>
| /\<\d\d\d\d\> |
Search exactly 4 digits |
</tr>
| /^\n\{3} |
Find 3 empty lines |
</tr>
| :bufdo /searchstr/ |
Search in all open files |
</tr>
| bufdo %s/something/somethingelse/g |
Search something in all the open buffers and replace it with somethingelse |
</tr>
</tbody>
</table>
Replace
| :%s/old/new/g |
Replace all occurences of old by new in file |
</tr>
| :%s/onward/forward/gi |
Replace onward by forward, case unsensitive |
</tr>
| :%s/old/new/gc |
Replace all occurences with confirmation |
</tr>
| :2,35s/old/new/g |
Replace all occurences between lines 2 and 35 |
</tr>
| :5,$s/old/new/g |
Replace all occurences from line 5 to EOF |
</tr>
| :%s/^/hello/g |
Replace the begining of each line by hello |
</tr>
| :%s/$/Harry/g |
Replace the end of each line by Harry |
</tr>
| :%s/onward/forward/gi |
Replace onward by forward, case unsensitive |
</tr>
| :%s/ *$//g |
Delete all white spaces |
</tr>
| :g/string/d |
Delete all lines containing string |
</tr>
| :v/string/d |
Delete all lines containing which didn’t contain string |
</tr>
| :s/Bill/Steve/ |
Replace the first occurence of Bill by Steve in current line |
</tr>
| :s/Bill/Steve/g |
Replace Bill by Steve in current line |
</tr>
| :%s/Bill/Steve/g |
Replace Bill by Steve in all the file |
</tr>
| :%s/^M//g |
Delete DOS carriage returns (^M) |
</tr>
| :%s/\r/\r/g |
Transform DOS carriage returns in returns |
</tr>
| :%s#<[^>]\+>##g |
Delete HTML tags but keeps text |
</tr>
| :%s/^\(.*\)\n\1$/\1/ |
Delete lines which appears twice |
</tr>
| Ctrl+a |
Increment number under the cursor |
</tr>
| Ctrl+x |
Decrement number under cursor |
</tr>
| ggVGg? |
Change text to Rot13 |
</tr>
</tbody>
</table>
Case
| Vu |
Lowercase line |
</tr>
| VU |
Uppercase line |
</tr>
| g~~ |
Invert case |
</tr>
| vEU |
Switch word to uppercase |
</tr>
| vE~ |
Modify word case |
</tr>
| ggguG |
Set all text to lowercase |
</tr>
| gggUG |
Set all text to uppercase |
</tr>
| :set ignorecase |
Ignore case in searches |
</tr>
| :set smartcase |
Ignore case in searches excepted if an uppercase letter is used |
</tr>
| :%s/\<./\u&/g |
Sets first letter of each word to uppercase |
</tr>
| :%s/\<./\l&/g |
Sets first letter of each word to lowercase |
</tr>
| :%s/.*/\u& |
Sets first letter of each line to uppercase |
</tr>
| :%s/.*/\l& |
Sets first letter of each line to lowercase |
</tr>
</tbody>
</table>
Read/Write files
| :1,10 w outfile |
Saves lines 1 to 10 in outfile |
</tr>
| :1,10 w >> outfile |
Appends lines 1 to 10 to outfile |
</tr>
| :r infile |
Insert the content of infile |
</tr>
| :23r infile |
Insert the content of infile under line 23 |
</tr>
</tbody>
</table>
File explorer
| :e . |
Open integrated file explorer |
</tr>
| :Sex |
Split window and open integrated file explorer |
</tr>
| :Sex! |
Same as :Sex but split window vertically |
</tr>
| :browse e |
Graphical file explorer |
</tr>
| :ls |
List buffers |
</tr>
| :cd .. |
Move to parent directory |
</tr>
| :args |
List files |
</tr>
| :args *.php |
Open file list |
</tr>
| :grep expression *.php |
Returns a list of .php files contening expression |
</tr>
| gf |
Open file name under cursor |
</tr>
</tbody>
</table>
Interact with Unix
| :!pwd |
Execute the pwd unix command, then returns to Vi |
</tr>
| !!pwd |
Execute the pwd unix command and insert output in file |
</tr>
| :sh |
Temporary returns to Unix |
</tr>
| $exit |
Retourns to Vi |
</tr>
</tbody>
</table>
Alignment
| :%!fmt |
Align all lines |
</tr>
| !}fmt |
Align all lines at the current position |
</tr>
| 5!!fmt |
Align the next 5 lines |
</tr>
</tbody>
</table>
Tabs/Windows
| :tabnew |
Creates a new tab |
</tr>
| gt |
Show next tab |
</tr>
| :tabfirst |
Show first tab |
</tr>
| :tablast |
Show last tab |
</tr>
| :tabm n(position) |
Rearrange tabs |
</tr>
| :tabdo %s/foo/bar/g |
Execute a command in all tabs |
</tr>
| :tab ball |
Puts all open files in tabs |
</tr>
| :new abc.txt |
Edit abc.txt in new window |
</tr>
</tbody>
</table>
Window spliting
| :e filename |
Edit filename in current window |
</tr>
| :split filename |
Split the window and open filename |
</tr>
| ctrl-w up arrow |
Puts cursor in top window |
</tr>
| ctrl-w ctrl-w |
Puts cursor in next window |
</tr>
| ctrl-w_ |
Maximize current window vertically |
</tr>
| ctrl-w| |
Maximize current window horizontally |
</tr>
| ctrl-w= |
Gives the same size to all windows |
</tr>
| 10 ctrl-w+ |
Add 10 lines to current window |
</tr>
| :vsplit file |
Split window vertically |
</tr>
| :sview file |
Same as :split in readonly mode |
</tr>
| :hide |
Close current window |
</tr>
| :nly |
Close all windows, excepted current |
</tr>
| :b 2 |
Open #2 in this window |
</tr>
</tbody>
</table>
Auto-completion
| Ctrl+n Ctrl+p (in insert mode) |
Complete word |
</tr>
| Ctrl+x Ctrl+l |
Complete line |
</tr>
| :set dictionary=dict |
Define dict as a dictionnary |
</tr>
| Ctrl+x Ctrl+k |
Complete with dictionnary |
</tr>
</tbody>
</table>
Marks
| m {a-z} |
Marks current position as {a-z} |
</tr>
| ' {a-z} |
Move to position {a-z} |
</tr>
| '' |
Move to previous position |
</tr>
</tbody>
</table>
Abbreviations
| :ab mail mail@provider.org |
Define mail as abbreviation of mail@provider.org |
</tr>
</tbody>
</table>
Text indent
| :set autoindent |
Turn on auto-indent |
</tr>
| :set smartindent |
Turn on intelligent auto-indent |
</tr>
| :set shiftwidth=4 |
Defines 4 spaces as indent size |
</tr>
| ctrl-t, ctrl-d |
Indent/un-indent in insert mode |
</tr>
| >> |
Indent |
</tr>
| << |
Un-indent |
</tr>
| =% |
Indent the code between parenthesis |
</tr>
| 1GVG= |
Indent the whole file |
</tr>
</tbody>
</table>
Syntax highlighting