Dark Mode

91 Vim Keyboard Shortcuts to Get Started with Vim

vim keyboard shortcuts

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.

iInsert mode at the cursor.
IInsert mode at the beginning of the line.
aInsert mode after the cursor.
AInsert mode at the end of the line.
oInsert mode with a new line below.
OInsert mode with a new line above.
sInsert mode at the cursor, after deleting the current character.
SInsert mode, after deleting the current line.
vVisual mode at the cursor.
VVisual mode at the beginning of the line.
rReplace mode to replace the current character.
RReplace mode.
EscNormal Mode or Command Mode.

Motion or Movement

Motions are a set of shortcut keys that allow us to quickly move around the text document.

kMove one line up.
jMove one line down.
hMove one line left.
lMove one line right.
wGo to the beginning of the next word (separated by space/punctuation).
WGo to the beginning of the next word separated by space.
eGo to the end of the next word separated by punctuations/space.
EGo to the end of the next word separated by space.
bGo to the beginning of the previous word separated by punctuation/space.
BGo 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.
0Go to the beginning of the line.
$Go to the end of the line.
ggGo to the beginning of the file.
GGo to the end of the file.
gdGo 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.

fFind the next occurrence of a character in the current line and go to it.
tFind the next occurrence of a character in the current line and go to its previous character.
FFind the previous occurrence of a character in the current line and go to it.
TFind 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.

nFind the next occurrence. To be used after using / or ?
NFind 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.

dwDelete from current character to end of a word(space/punctuation/EOL).
dWDelete from current character to end of a word (space/EOL).
dbDelete from current character to beginning of a word(space/punctuation/EOL).
dBDelete from current character to beginning of a word(space/EOL).
diwDelete the current word.
diWDelete the current word.
ddDelete 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.
xDelete the current character under the cursor.
XDelete 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.

cwDelete from current character to end of a word(space/punctuation/EOL) and then go to insert mode.
cWDelete from current character to end of a word (space/EOL) and then go to insert mode.
cbDelete from current character to beginning of a word(space/punctuation/EOL) and then go to insert mode.
ccChange the current line.
cBChange the current block.
ciwChange inside a word.
ciWChange 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.
sDelete the current character and go to insert mode.
SDelete the current line and go to insert mode.
>>Indent current line.
<<Unindent current line.

Copy(Yank) and Paste

yyCopy the current line
ywCopy the current word from cursor till space/punctuation
yWCopy the current word from cursor till space.
yiwCopy the current word.
yiBCopy 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.
pPaste below the current line.
PPaste 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

uUndo the last action.
URedo 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.
gUUMake current line uppercase.
guuMake 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.

Join our Newsletter

Get weekly updates on new content. No Spams whatsoever!

Written By
Praveen
Programmer | Tech Blogger | Multitasker | Learner Forever | Someone who believes knowledge grows by sharing | Looking forward to using spare time productively
Write the first response

Weekly Newsletter


 

Get weekly updates on new content right in your Inbox!


We Respect your Privacy | No Spams whatsoever!