Introduction to Vim
Vim is one of the most powerful text editors you can think of. In this article, I will introduce you some basic and useful Vim commands.
Vim modes
There are two main modes in Vim. Normal Mode and Insert Mode. In Insert Mode you can type on the document as in any other text editor. In Normal Mode you can execute commands to navigate though the file or modify it.
Entering Normal Mode
Press the escape key to enter Normal Mode. (Most Vim users usually remap escape to caps lock. On a Mac you can do it using Karabiner.)
Entering Insert Mode
To enter insert mode you just have to type i or a while on Normal Mode. If you entered Insert Mode with the i key, you will be able to insert text on the left side of your cursor. In contrast, with a, you will insert text on the right side of the cursor. C (Shift-c) deletes all the text from the cursor position to the end of the line and puts you in Insert Mode.
There is also o that allows you to insert text on the next line.
Uppercased versions
I (Shift-i) will put you on insert mode on the beginning of the line.
A (Shift-a) will put you on insert mode on the end of the line. O (Shift-o) will put on insert mode on the previous line.
Visual Mode
Visual Mode is used for selecting text.
vEnter visual mode.VEnter visual line mode.ctrl-vEnter visual block mode.
Exiting Vim
The most known way of exiting Vim is using :wq (write and quit) or :q! (quit without saving).
But, there is a more efficient ways of exiting Vim. You type ZZ (shift-zz) to exit Vim saving or ZQ (shift-zq) to exit it without saving.
Cursor Movement
Basic Movement (HJKL)
HMove to the left.JMove down.KMove up.LMove to the right.
Movement word per word
-
wMove to the next word. -
WMove to the next word (separated by whitespace). -
bMove a word backwards. -
BMove a word backwards (separated by whitespace). -
eMove to the end of the next word. -
EMove to the end of the next word (separated by whitespace).
You can use all of these commands with a count. e.g. 5w to move the cursor 5 words forward.
Moving the cursor
HPut the cursor on the top.MPut the cursor on the middle.LPut the cursor on the bottom.
Mnemonic: High, Middle, Low.
Movement by paragraph
{Move the cursor a paragraph up.}Move the cursor a paragraph down.
Movement through the line
Including whitespace
0 Go to the beginning of the line.
$ Go to the end of the line.
Not including whitespace
^ Go to the beginning of the line.
g_ Go to the end of the line.
G and gg
Use G Go to the bottom of the file.
Usegg Go to the top of the file.
f and t
ffollowed by a wordamoves the cursor the the next occurrence of the wordaon a line.tfollowed by a wordamoves your cursor a word before the the next occurrence of the wordaword on a line.
Example
The quick brown fox jumps over the lazy dog.
^
Using fb
The quick brown fox jumps over the lazy dog.
^
Using tb
The quick brown fox jumps over the lazy dog.
^
Moving the screen
ztPut the current line on the top of the screen.zzPut the current line on the middle of the screen.zbPut the current line on the bottom of the screen.
Deleting text
To delete text, you can use the d followed by what you want to delete.
(You can use all of the following commands with c instead of d to delete and CHANGE what you just deleted. In other words, it deletes and puts you on Insert Mode.)
Counts
In Vim you can specify the number of times to execute a command. For example, you can delete a line with dd and 5dd for deleting 5 lines at once. It also works for other commands like 5dap, 5daw, etc.
Some delete commands
-
diwDelete a word. -
dawDelete a word w/ surrounding whitespace. -
di(Delete inside a (). -
da(Delete a () w/ surrounding whitespace. -
di[Delete inside a []. -
da[Delete a [] w/ surrounding whitespace. -
di{Delete inside a {}. -
da{Delete a {} w/ surrounding whitespace. -
dipDelete inside a paragraph. -
dapDelete a paragraph w/ surrounding whitespace. -
ditDelete inside an HTML tag. -
datDelete an HTML tag w/ surrounding whitespace. -
dwDelete word, only works if the cursor is positioned on the beginning of the word. -
DDelete from the cursor to the end of the line. -
ddDelete the current line. -
5ddDelete the next 5 lines. -
dGDelete from the current line until the end of the document. -
dggDelete form the current line until the beginning of the document.
Copying and Pasting
y(for YANK) copy. (e.g. to copy a word typeyw)yyCopy a whole line.pPaste.
Searching
-
/Search. (Navigate with ‘n/N’) -
?Search backwards. (Navigate with ‘n/N’) -
//Search for the last pattern searched. -
??Search for the last pattern searched backwards. -
*Search the current word. -
#Search the current word backwards. -
:set icSearch case insensitively.
Replacing Text
:%s/old/new/g
:%s/old/new/gi (case insensitive)
:%s/old/new/gc (prompts before each replacement)
Undo and Redo
uUndo the last change.UUndo the last changes on the current line.ctrl-rRedo the last change.
Tabs
:tabnewOpen a new tab.:tabnextGo to the next tab. (Also:tabn):tabpreviousGo to the previous tab. (Also:tabp):tabfirstGo to the first tab.:tablastGo to the last tab.:tabmove (num)Move the current tab to the specifiednum.
Editing
edit or e open a file in Vim. (On a new tab for example.)
Macros
Start / Stop recording a macro with q on one of Vim’s 26 registers (a-z).
Use @ with the register key to play the recorded macro.
You can play it n times with n@q with q being the register.
Marks
Record your current position in a register.
mbSet a mark on registerb. (Using an uppercased mark likemBmakes it accessible on all the files being edited.)’bGo the the mark set onb.:marksList all the current marks.:delmarks bDelete a mark.:delmarks a-cDelete a range of marks.:demarks!Delete all the marks.
. command
Rerun the last executed command on Normal Mode.
g
g+ hjkl, 0, $, etc navigates an one line paragraph.gqReformat an one line paragraph.gaGive you the ASCII value of the current character.gfOpen the file under the cursor.giContinue inserting text to where you were before.
Indentation
Indent the current line using < and >.
Running a Shell command
Use :! to run a Shell command.
e.g. :! ls to run ls.
%
% Jump to the matching parenthesis, brackets, etc.