Practical Manual: over 130 common Vim commands to improve development efficiency
Vim is a text editor developed from vi. Code Completion, compilation, error jump, and other convenient programming functions are particularly rich and widely used by programmers. It is used in parallel with Emacs to become your favorite editor for Unix-like systems. The vim commands required by more than 130 programmers are included here to help you improve development efficiency.
Basic commands
: E filename |
OpenFilenameFor edition |
: W |
Save file |
: Q |
Exit Vim |
: Q! |
Quit without saving |
: X |
Write file (if changes has been made) and exit |
: Sav filename |
Saves fileFilename |
. |
Repeats the last change made in normal mode |
5. |
Repeats 5 times the last change made in normal mode |
Move in file
K or Up Arrow |
Move the cursor up one line |
J or Down Arrow |
Move the cursor down one line |
E |
Move the cursor to the end of the word |
B |
Move the cursor to the begining of the word |
0 |
Move the cursor to the begining of the line |
G |
Move the cursor to the end of the file |
Gg |
Move the cursor to the begining of the file |
L |
Move the cursor to the bottom of the screen |
: 59 |
Move cursor to line59. Replace59By the desired line number. |
20 | |
Move cursor to column20. |
% |
Move cursor to matching parenthesis |
[[ |
Jump to function start |
[{ |
Jump to block start |
Cut, copy, and paste
Y |
Copy the selected text to clipboard |
P |
Paste clipboard contents |
Dd |
Cut current line |
Yy |
Copy current line |
Y $ |
Copy to end of line |
D |
Cut to end of line |
Search
/Word |
SearchWordFrom top to bottom |
? Word |
SearchWordFrom bottom to top |
* |
Search the word under cursor |
/\ Cstring |
SearchSTRINGOrString, Case insensitive |
/Jo [ha] n |
SearchJohnOrJoan |
/\ < |
Search the, theater orThen |
/The \> |
SearchTheOrBreathe |
/\ <The \> |
SearchThe |
/\ <Rule. \> |
Search all words of 4 letters |
/\/ |
SearchFredBut notAlfredOrFrederick |
/Fred \ | joe |
SearchFredOrJoe |
/\ <\ D \> |
Search exactly 4 digits |
/^ \ N \ {3} |
Find 3 empty lines |
: Bufdo/searchstr/ |
Search in all open files |
Bufdo % s/something/somethingelse/g |
SearchSomethingIn all the open buffers and replace itSomethingelse |
Replace
: % S/old/new/g |
Replace all occurencesOldByNewIn file |
: % S/onward/forward/gi |
Replace onward by forward, case unsensitive |
: % S/old/new/gc |
Replace all occurences with confirmation |
: 2, 35 s/old/new/g |
Replace all occurences between lines 2 and 35 |
: 5, $ s/old/new/g |
Replace all occurences from line 5 to EOF |
: % S/^/hello/g |
Replace the begining of each lineHello |
: % S/$/Harry/g |
Replace the end of each lineHarry |
: % S/onward/forward/gi |
ReplaceOnwardByForward, Case unsensitive |
: % S/* $ // g |
Delete all white spaces |
: G/string/d |
Delete all lines containingString |
: V/string/d |
Delete all lines containing which didn't containString |
: S/Bill/Steve/ |
Replace the first occurenceBillBySteveIn current line |
: S/Bill/Steve/g |
ReplaceBillBySteveIn current line |
: % S/Bill/Steve/g |
ReplaceBillBySteveIn all the file |
: % S/^ M/g |
Delete DOS carriage returns (^ M) |
: % S/\ r/g |
Transform DOS carriage returns in returns |
: % S # <[^>] \ +> # g |
Delete HTML tags but keeps text |
: % S/^ \ (. * \) \ n \ 1 $/\ 1/ |
Delete lines which appears twice |
Ctrl + |
Increment number under the cursor |
Ctrl + x |
Decrement number under cursor |
GgVGg? |
Change text to Rot13 |
Case sensitivity
Vu |
Lowercase line |
VU |
Uppercase line |
G ~~ |
Invert case |
VEU |
Switch word to uppercase |
VE ~ |
Modify word case |
GgguG |
Set all text to lowercase |
GggUG |
Set all text to uppercase |
: Set ignorecase |
Ignore case in searches |
: Set smartcase |
Ignore case in searches incluted if an uppercase letter is used |
: % S/\ <./\ u &/g |
Sets first letter of each word to uppercase |
: % S/\ <./\ l &/g |
Sets first letter of each word to lowercase |
: % S/. */\ u & |
Sets first letter of each line to uppercase |
: % S/. */\ l & |
Sets first letter of each line to lowercase |
Read/write files
: W outfile |
Saves lines 1 to 10 inOutfile |
: W> outfile |
Appends lines 1 to 10Outfile |
: R infile |
Insert the contentInfile |
: 23r infile |
Insert the contentInfileUnder line 23 |
File Browser
: E. |
Open integrated file explorer |
: Sex |
Split window and open integrated file explorer |
: Sex! |
Same: SexBut split window vertically |
: Browse e |
Graphical file explorer |
: Ls |
List buffers |
: Cd .. |
Move to parent directory |
: Args |
List files |
: Args *. php |
Open file list |
: Grep expression *. php |
Returns a list of. php files conteningExpression |
Gf |
Open file name under cursor |
Interaction with Unix systems
:! Pwd |
ExecutePwdUnix command, then returns to Vi |
!! Pwd |
ExecutePwdUnix command and insert output in file |
: Sh |
Temporary returns to Unix |
$ Exit |
Retourns to Vi |
Alignment
: %! Fmt |
Align all lines |
!} Fmt |
Align all lines at the current position |
5 !! Fmt |
Align the next 5 lines |
Tab/window
: Tabnew |
Creates a new tab |
Gt |
Show next tab |
: Tabfirst |
Show first tab |
: Tablast |
Show last tab |
: Tabm n (position) |
Rearrange tabs |
: Tabdo % s/foo/bar/g |
Execute a command in all tabs |
: Tab ball |
Puts all open files in tabs |
: New abc.txt |
EditAbc.txtIn new window |
Split-screen display
: E filename |
EditFilenameIn current window |
: Split filename |
Split the window and openFilename |
Ctrl-w up arrow |
Puts cursor in top window |
Ctrl-w |
Puts cursor in next window |
Ctrl-w _ |
Maximize current window vertically |
Ctrl-w | |
Maximize current window horizontally |
Ctrl-w = |
Gives the same size to all windows |
10 ctrl-w + |
Add 10 lines to current window |
: Vsplit file |
Split window vertically |
: Sview file |
Same as: split in readonly mode |
: Hide |
Close current window |
: Nly |
Close all windows, sorted Ted current |
: B 2 |
Open #2 in this window |
Automatic completion
Ctrl + n Ctrl + p (in insert mode) |
Complete word |
Ctrl + x Ctrl + l |
Complete line |
: Set dictionary = dict |
DefineDictAs a dictionnary |
Ctrl + x Ctrl + k |
Complete with dictionnary |
Mark
M {a-z} |
Marks current position{A-z} |
'{A-z} |
Move to position{A-z} |
'' |
Move to previous position |
Abbreviations
: AB mail mail@provider.org |
DefineMailAs abbreviationMail@provider.org |
Text indent
: Set autoindent |
Turn on auto-indent |
: Set smartindent |
Turn on intelligent auto-indent |
: Set shiftwidth = 4 |
Defines 4 spaces as indent size |
Ctrl-t, ctrl-d |
Indent/un-indent in insert mode |
> |
Indent |
< |
Un-indent |
= % |
Indent the code between parenthesis |
1GVG = |
Indent the whole file |
Code highlighting
: Syntax on |
Turn on syntax highlighting |
: Syntax off |
Turn off syntax highlighting |
: Set syntax = perl |
Force syntax highlighting |
For more Vim tutorials, see the following:
Build VIM into a simple and practical IDE
Vim Learning Guide
Quick learn Vi Editor
Powerful Vim Editor
Build a Vim Development Environment on CentOS 6.2
Install the highlighted Vim editing tool in CentOS 5.4
Vim tips: C language settings
Set the Vim row number in Ubuntu
Vim editor basic tutorial
This article permanently updates the link address: