Vim cheat sheet
This is a cheat sheet for the text editor Vim. If you want to learn Vim, I strongly recommend you to start out with doing vimtutor.
A short note: Vim works by combining operators with (optional numbers and) motions.
To abort what you're doing (leaving Insert mode or Visual mode, aborting an operation, etc.) press <ESC>; Ctrl+c usually works too.
Motions:
wmoves to first character of next word;bmoves to first character of current or previous word;emoves to last character of current or next word;0moves to the first character of line;^moves to first non-blank character of line;$moves to last character of line;t{char}moves forward on the line to the position before the character entered aftert;T{char}moves backward on the line to the position before the character entered afterT;f{char}moves forward on the line to the position of the character entered afterf;F{char}moves backward on the line to the position of the character entered afterF;- These motions can be appended to a number to repeat the movement;
Navigation:
gggoes to first line of file;Ggoes to last line of file;Ctrl+gdisplays current line position of cursor and total number of lines of file;{line}Gor{line}ggwhere{line}is the number of the line where you want to place the cursor;
Insert mode:
ienters insert mode at cursor location;aenters insert mode one step AFTER cursor position (eaappends to current word);Iprepends to line, i.e. enters insert mode at the first non-blank character of line;Aappends to line, i.e. enters insert mode AFTER the end of the line;oopens the line below the cursor and enters Insert mode;Oopens the line above the cursor and enters Insert mode;rreplaces the symbol the cursor is placed on with the symbol entered afterr.Rreplaces multiple symbols with the ones entered afterR, starting where the cursor was placed and stopping when pressing<ESC>.Ctrl+wdeletes a whole word before the cursor while in Insert mode;Ctrl+udeletes the line before the cursor while in Insert mode;
Visual selection mode:
venter Visual mode, where you can mark text by using motions, which can be deleted, yanked (copied), saved to a new file namedNAME(by typing:w NAMEafter text has been marked).Venters Visual Line mode with whole lines are being marked;Ctrl+venters Visual Block mode where rectangular blocks are marked. End with<ESC>to apply the following operations: ** to insert text, useShift+i; ** to change text, usec; ** to delete text, usedorx(this will be applied right away followed by exiting Visual Block mode);
Cutting, copying, pasting using Vim's clipboard:
dfollowed by motion to cut text:ddto cut a whole line;Dord$to cut rest of line;cfollowed by motion cuts the chosen text and enters Insert mode:cccuts the whole line;xto cut a single character;yfollowed by motion to copy (AKA 'yank') text:yyto yank a whole lineppastes text from Vim's clipboard, inserting it after the cursor;Ppastes text from Vim's clipboard, inserting it after the cursor;- The above operators can be used when text has been marked in Visual mode;
Edit:
uundo last operation;Ctrl+rto redo undone operation;>>indents line where cursor is placed,>indents text marked via Visual mode;<<dedents line where cursor is placed,<dedents text marked via Visual mode;
Commands:
:wto save;:w NAMEto save to not pre-existing file namedNAME, either saving the whole file or only the text marked via visual mode;:q!to quit without saving changes;:wqto save and quit;:r NAMEto insert content of fileNAMEin line below the cursor;:r !{external command}reads from command and puts it below the cursor position;
Cutting, copying, pasting using system clipboard:
Shift+Ctrl+vor"+ppastes text to the file from the system clipboard;Shift+Ctrl+xor"+xcuts text from the file to the system clipboard;Shift+Ctrl+cor"+ccopies text from the file to the system clipboard;- Explanation: In the second alternative of the operations above, the system clipboard is interacted with by entering
"(theregisters symbol), then+to access the clipboard register, followed by what we want to do with the clipboard;
Search, find, replace:
:/followed by a search word followed by<Enter>to find the first instance of the word, followed bynto continue search forward orNto continue search backward;:?works like/but starts to search backward right away after<Enter>;:s/old/newsubstitutesnewforoldat the first match found: append with/gto do it for the whole line;:%s/old/new/greplaces every occurence in the whole file;:%s/old/new/gcfinds all occurances in the file and asks whether you want to replace them;:#,#s/old/new/greplacesoldfornewbetween lines#,#;%to find matching),], or}which cursor is placed on;:set icto set search to ignore case (:set ignorecasealso works);:set noicto set search to not ignore case;:/SEARCHWORD\cto search forSEARCHWORDand ignoring cases in just that search;:set hlsis to enable highlighting of matches in search (:set hlsearchalso works);:set nohlsis to disable highlighting of matches in search;- TODO: What does
:set isdo? So calledinsearch;
Execute external commands:
:!{external command}followed by<Enter>runs any external shell command, such asls -aordir;
Completion:
- Starting to write a command, like
:efollowed by<TAB>autocompletes it in alphabetical order, such as the case in most shells and terminals. PressingCtrl+ddisplays all possible completions.
Navigating directories
- Running
vim .in a terminal opens a directory listing that can be navigated withj,kand<Enter>. To create a new file, press%; to create a new directory, pressd. To return to this view from a file that has been entered from here, use:Ex. External commands can be run by:! {command}.