Since the 70's, VI and Vim are very popular text editors among programmers. 5 years ago, I wrote an article named "100 Vim commands Every programmer shoshould know" and here is a reworked, updated version. enjoy!
Basics
: 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 |
Moving in the 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 & 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
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 Explorer
: 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 |
Interact with Unix
:! 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 |
Tabs/Windows
: 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 |
Window spliting
: E filename |
EditFilenameIn current window |
: Split filename |
Split the window and openFilename |
CTRL-w up arrow |
Puts cursor in top window |
CTRL-W 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: SplitIn readonly Mode |
: Hide |
Close current window |
: NLY |
Close all windows, sorted Ted current |
: B 2 |
Open #2 in this window |
Auto-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 |
Marks
M {A-z} |
Marks current position{A-z} |
'{A-z} |
Move to position{A-z} |
'' |
Move to previous position |
Abbreviations
: AB Mail [email protected] |
DefineMailAs abbreviation[Email protected] |
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 |
Syntax highlighting
: Syntax on |
Turn on syntax highlighting |
: Syntax off |
Turn Off syntax highlighting |
: Set syntax = Perl |
Force syntax highlighting |