The following text is based on the tutorial Learn Vim Progressively that can be found here
Vim starts by default in NORMAL mode. Use i to change to INSERT mode. To get back to NORMAL mode just press the ESC key.
i-> Go to INSERT mode. Inserts in the place where the cursor isESC-> Back to NORMAL modex-> Delete the character under the cursor:wq-> Save (:w) and Quit (:q)dd-> Delete (and copy to clipboard) the current linep-> Paste what is in clipboardhjkl-> Move Left, Down, Up, Right (jlooks like a down arrow):help <command>-> Show help about command.:helpprovides general help
Note: The key for productivity in Vim is to stay most of the time in NORMAL mode, and use from there all the available commands to modify text and move around.
-
Insert mode variations:
i-> insert IN the cursora-> insert AFTER the cursorI-> insert at the BEGINNING of the current lineA-> insert at the END of the current lineo-> insert a new line AFTER the current oneO-> insert a new line BEFORE the curren onecw-> replace from the cursor to the end of the word
-
Basic moves:
0-> (Zero) go to the first column of the line$-> go to the end of the line^-> go to the first NON-BLANK character of the lineg_-> go to the last NON-BLANK character of the line/pattern-> search for pattern
-
Copy/Paste:
P-> paste BEFORE current positionp-> paste AFTER current positionyy-> copy the current line
-
Undo/Redo:
u-> undoCtrl-r-> redo
-
Load/Save/Quit/Change File (Buffer):
:e <path/to/file>-> open path/to/file:w-> save:saveas <path/to/file>-> save to path/to/file:wq,:xorZZ-> save and quit (:xonly save if necessary):q!-> quit without saving:qa!-> quit without saving even if there are modified hidden buffers:ls-> show all the files (buffers) currently opened:bn-> show NEXT file (buffer):bp-> show PREVIOUS file (buffer):bd-> close current buffer and delete it from buffer list
-
.-> (dot) will repeat the last command -
N<command>will repeat command N timesFor example:
100idesu [ESC]-> will write "desu " 100 times.(dot) -> just after last command will write again "desu " 100 times3.-> will write "desu " 3 times (and not 300 times)
-
NG-> go to line N -
gg-> go to start of file (shortcut for1G) -
G-> go to last line of file
Mind that 'words' are composed of letters and underscore character, while 'WORDS' are a group of characters separated by blank characteres. Thence, in the following string:
(name_1,vision_3); # this is a comment.
the first 'word' is name_1, while the first 'WORD' is (name_1,vision_3);
-
w-> go to the START of the following 'word' -
e-> go to the END of this 'word' -
W-> go to the START of the following 'WORD' -
E-> go to the END of this 'WORD' -
%-> go to the correspondig (, {, [ -
*-> go to the NEXT occurence of the word under the cursor -
#-> go to the PREVIOUS occurence of the word under the cursor
Most commands can be used using the following general format:
<START-position><COMMAND><END-position>
For example, 0y$ means:
0-> (Zero) go to the beginning of this liney-> yank from here...$-> ...up to the end of this line
Other examples are:
ye-> yank to the end of this wordy2/foo-> yank up to the second occurence of foo
-
General:
-
fX-> go to the next occurence of character X on this lineFor instance:
-
3fX-> find the 3rd occurence of character X in this line -
F-> likef, but backwards -
;-> find the NEXT occurrence -
,-> find the PREVIOUS occurrence
-
-
tX-> go to just BEFORE the character XT-> liket, but backwards
-
dtX-> remove everything up to X, without including X
-
-
Zone selection:
This follows the patterns:
<action>a<object> and <action>i<object>
Where action can be d (delete), y (yank), v (select in visual mode),etc. The object can be: w (word), W (WORD), s (sentence), p (paragraph), but also natural characters like ", ', ), }, ].
Let's suppose the cursor is on the first 'o' of (map (+) ("foo")):
vi"-> will selectfoova"-> will select"foo"vi)-> will select"foo"va)-> will select("foo")v2i)-> will selectmap (+) ("foo")v2a)-> will select(map (+) ("foo"))
-
Rectangular blocks:
They are very useful for commenting many lines of code. For instance:
^<Ctrl-v><Ctrl-d>I-- [ESC][ESC]
-
^-> go to the first non-blank character of the line -
Ctrl-v-> start of block selection (ATTENTION!, it is CTRL!!!) -
Ctrl-d-> move down (could also bejjj,%, etc.) -
I-- [ESC][ESC]-> insert--at the beginning of each line -
o-> in Visual Block Mode, toggles selection cursor to OPPOSITE cornerNote #1: Instead of
Iwe can usei,A,a,c,r, etc.Note #2: Don't confuse
Ctrl-v(Visual Block Mode) withVandvmodes
To add something at the end of all visually selected lines:
Ctrl-v-> gets into Visual Block Mode- go to the desired line (with
j,Ctrl-d,/pattern, etc) $-> go to the end of the lineA+ write new text +ESC
A fabulous video about the Visual Block Mode can be found at Vimcasts.
-
Completion:
Ctrl-p-> call the autocompletion feature
-
Macros:
qa-> records your actions in register 'a'@a-> this will call the macro saved in register 'a'@@-> calls the last executed macro
Example: On a line containing only the number 1, type:
qaYp<Ctrl-a>qqa-> starts recording the macroYp-> duplicates this lineCtrl-a-> increments the numberq-> stops recording the macro
@a-> writes 2 under 1@@-> writes 3 under 2100@@-> writes increasing numbers up to 103
-
Visual selection:
v,VandCtrl-vOnce the selection has been made:
J-> joins all the lines together<-> indent to the left>-> indent to the right=-> autoindent
-
Splits:
:split-> create a (horizontal) split (shorthand::sp):vsplit-> create a vertical split (shorthand::vs)Ctrl-w <direction>-> (where direction ishjkl) to change splitCtrl-w_-> Maximise the size of the (horizontal) splitCtrl-w |-> Maximise the size of the vertical splitCtrl-w +-> Grow the splitCtrl-w --> Shrink the split
-
The manuals:
:help usr_01.txt-> Start the User ManualCtrl-]-> jumps to the subject under the cursorCtrl-o-> jumps back