Since 1970s, Vi has been one of the best programmers. no matter you are a beginner or veteran of Vi, the following section provides 100 useful Vi commands, which I believe will be helpful to you. Basics: efilenameOpenfilenameforedition: wSavefile: qExitVim: w! E...
Since 1970s, Vi has been one of the best programmers. no matter you are a beginner or veteran of Vi, the following section provides 100 useful Vi commands, which I believe will be helpful to you.
Basics
| : E filename |
OpenFilenameFor edition |
| : W |
Save file |
| : Q |
Exit Vim |
| : W! |
Exit Vim without saving |
Search
| /Word |
SearchWordFrom top to bottom |
| ? Word |
SearchWordFrom bottom to top |
| /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 |
Replace
| : % S/old/new/g |
Replace all occurencesOldByNewIn file |
| : % S/old/new/gw |
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/\ r // 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 |
| : 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 |
| : 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
| : 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 |
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 |
Puts cursor in next window |
| Ctrl-w _ |
Maximise current window |
| 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
| Mk |
Marks current positionK |
| ˜ K |
Moves cursor to markK |
| D™K |
Delete all until markK |
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 |
Syntax highlighting
| : Syntax on |
Turn on syntax highlighting |
| : Syntax off |
Turn off syntax highlighting |
| : Set syntax = perl |
Force syntax highlighting |
From: BugZone-http://www.pulog.org/Resources/2290/vim-cmd