91 Vim Keyboard Shortcuts to Get Started with Vim
This article gives you a list of most commonly used Vim keyboard shortcuts that can help you get started with Vim.
Modes
Vim has multiple modes namely Normal mode, Insert mode, Replace mode, and Visual mode.
Normal mode is where you can use most of the shortcuts. In insert mode, Vim behaves like a normal text editor. In replace mode, existing texts are replaced or overwritten as you type. Visual mode allows us to select texts visually and then make changes to it.
i | Insert mode at the cursor. |
I | Insert mode at the beginning of the line. |
a | Insert mode after the cursor. |
A | Insert mode at the end of the line. |
o | Insert mode with a new line below. |
O | Insert mode with a new line above. |
s | Insert mode at the cursor, after deleting the current character. |
S | Insert mode, after deleting the current line. |
v | Visual mode at the cursor. |
V | Visual mode at the beginning of the line. |
r | Replace mode to replace the current character. |
R | Replace mode. |
Esc | Normal Mode or Command Mode. |
Motion or Movement
Motions are a set of shortcut keys that allow us to quickly move around the text document.
k | Move one line up. |
j | Move one line down. |
h | Move one line left. |
l | Move one line right. |
w | Go to the beginning of the next word (separated by space/punctuation). |
W | Go to the beginning of the next word separated by space. |
e | Go to the end of the next word separated by punctuations/space. |
E | Go to the end of the next word separated by space. |
b | Go to the beginning of the previous word separated by punctuation/space. |
B | Go to the beginning of the previous word separated by space. |
{ | Go to the previous line break. |
} | Go to the next line break. |
% | Go to matching Bracket. |
# | Go to the previous occurrence of the current word under the cursor. |
* | Go to the next occurrence of the current word under the cursor. |
^ | Go to the first non-empty character in the line. |
0 | Go to the beginning of the line. |
$ | Go to the end of the line. |
gg | Go to the beginning of the file. |
G | Go to the end of the file. |
gd | Go to definition. |
:{number} | Go to the line number. Note: This is not really a shortcut but a Vim command. |
Motion shortcuts can be prefixed with a number to repeat the motion. For example, to move 5 lines down, we can use 5j
. Similarly, to move 2 words forward, we can use 2w
.
Also Read: How to Set up Vim as an IDE for React and TypeScript in 2020
Searching
You can search for a character in the current line using find f
and till t
.
The difference between “find” and “till” is that “find” moves the cursor to the searched character, whereas “till” moves the cursor to the previous character of the searched character.
f | Find the next occurrence of a character in the current line and go to it. |
t | Find the next occurrence of a character in the current line and go to its previous character. |
F | Find the previous occurrence of a character in the current line and go to it. |
T | Find the previous occurrence of a character in the current line and go to its next character. |
To search for a phrase, you can use Vim command /
or ?
/{search-word} | Search for a word forward. For example /export will search and find the next instance of the word “export”. |
?{search-word} | Search for a word backward. For example /export will search and find the previous instance of the word “export”. |
After searching, n
and N
can be used to find next and previous occurrences respectively.
n | Find the next occurrence. To be used after using / or ? |
N | Find the previous occurrence. To be used after using / or ? |
Deleting
Delete(d
) is an operator in Vim. Operators cannot function without motion and henced
is always followed by a motion. Here, the motion is what tells Vim what to delete.
That being said, you can combine all motions with d
, like so.
dw | Delete from current character to end of a word(space/punctuation/EOL). |
dW | Delete from current character to end of a word (space/EOL). |
db | Delete from current character to beginning of a word(space/punctuation/EOL). |
dB | Delete from current character to beginning of a word(space/EOL). |
diw | Delete the current word. |
diW | Delete the current word. |
dd | Delete the current line. |
di' | Delete everything within the single quotes. |
di" | Delete everything within the double-quotes. |
di( | Delete everything within the brackets. |
di{ | Delete everything within the curly braces. |
di[ | Delete everything within the square brackets. |
x | Delete the current character under the cursor. |
X | Delete the previous character. |
You can also repeat these commands by prefixing a number. For example, you can delete 5 lines using 5dd
or d4j
Changing
In Vim, changing is similar to delete, the only difference is that after deleting, insert mode gets activated.
cw | Delete from current character to end of a word(space/punctuation/EOL) and then go to insert mode. |
cW | Delete from current character to end of a word (space/EOL) and then go to insert mode. |
cb | Delete from current character to beginning of a word(space/punctuation/EOL) and then go to insert mode. |
cc | Change the current line. |
cB | Change the current block. |
ciw | Change inside a word. |
ciW | Change inside a word. |
ci' | Change everything inside a pair of single quotes. |
ci" | Change everything inside a pair of double-quotes. |
ci( | Change everything inside a pair of parentheses. |
ci{ | Change everything inside a pair of curly braces. |
ci[ | Change everything inside a pair of square brackets. |
s | Delete the current character and go to insert mode. |
S | Delete the current line and go to insert mode. |
>> | Indent current line. |
<< | Unindent current line. |
Copy(Yank) and Paste
yy | Copy the current line |
yw | Copy the current word from cursor till space/punctuation |
yW | Copy the current word from cursor till space. |
yiw | Copy the current word. |
yiB | Copy the block. |
yi' | Copy everything inside a pair of single quotes. |
yi" | Copy everything inside a pair of double-quotes. |
yi( | Copy everything inside a pair of parentheses. |
yi{ | Copy everything inside a pair of curly braces. |
yi[ | Copy everything inside a pair of square brackets. |
p | Paste below the current line. |
P | Paste above the current line. |
When a text is yanked, it goes into Vim registers and Vim has many registers. You can see contents present in all the Vim registers by running the Vim command :reg
.
To yank a text to a particular register, you can prefix the yank command with "{register}
. For example, to yank the text to register “1”, you can use the shortcut "1yy
.
Similarly, you can paste the contents of a particular register. For example, "2p
will paste the content present in register “2”.
Undo and Redo
u | Undo the last action. |
U | Redo the last action. |
These commands can be repeated by prefixing a number. For example, to undo last 3 actions, you can use 3u
.
Toggling Case
~ | Toggle case at the current cursor position. |
gUU | Make current line uppercase. |
guu | Make current line lowercase. |
You can also use gu
and gU
with a motion. For example, to convert 3 lines to uppercase, you can use gU3j
.
Repeat Last Change
. | Repeats the last change. |
This is where the real power of Vim comes in. For example, say you need to replace all occurrences of a word. You can first search for the word using /
or ?
. Then to change the word you can use ciw
. After changing go back to Normal mode, hit n
to go to the next occurrence. Now you can simply press .
to replace the word.
What’s Next?
Once you get hold of these Vim keyboards shortcuts, open Vim and run the command :help
to open the Vim documentation or you can use the online version of Vim documentation. It provides a list of every command there is with an explanation. So, you can pick up a few more useful Vim shortcuts and also get a better understanding.