Words of introduction
Vim is considered an advanced editor for programmers and those who mastered Vim rarely would want to switch back.
I was avoiding Vim for some time, instead, I was relying on the IDE capabilities for text editing, but throughout my work experience, I realised one thing - IDEs come and go, while Vim remains. Often I had to switch IDEs and platforms, having to learn shortcuts again and again.
So I took time to master Vim and it turned out to be not so difficult. In fact, most of the Vim power can be unleashed with a very small subset of Vim commands.
I don't see much sense in mastering all the immense capabilities of Vim/IntelliJ/You name it. However, there's a subset that is very useful for me personally. I am using the Vim plugin in VScode and Jetbrains products and customized Vim as well. Below is the list of the commands/shortcuts I personally find useful for me.
To the best of my knowledge, those would work the same both in Vim plugins and Vim itself.
Disclaimer: this is a cheatsheet, not a tutorial, so it assumes the reader has some basic understanding of Vim.
General commands/shortcuts
Those are the very basics things, such as opening the file or escaping Vim.
Basics
| Action | Command |
|---|---|
| Exit w/o saving | :q! |
| Exit from all splits | qa |
| Exit and save | :wq! |
| Go to normal mode |
ESC or CTRL+C
|
| Repeat last change | . |
| Open file | :open {filename} |
| Execute terminal command | :!command |
Splits (tabs)
| Action | Command |
|---|---|
| Open file in vertical split | :vsp {filename} |
| Navigate between splits | CTRL+W (corresponding arrow) |
Visual mode
Visual mode is used to select different parts of texts
| Action | Command |
|---|---|
| go to the character-wise visual mode | v |
| go to the line-wise visual mode | V |
| to select "block" | CTRL+v |
Multiple cursors
Note: works in VSCode, doesn't work in GVIM
Add multiple cursors
- Select lines you want to change (using visual mode)
- type
I
Add cursor for word
- move cursor to the start of the word
- type
gbto add another cursor - repeat :) then use
c,ESCordor whatever
Navigating
Navigating by words
| Action | Command |
|---|---|
| next word | w |
| next WORD | W |
| previous word | b |
| previous WORD | B |
| next end of word | e |
| jump to matching bracket | % |
Navigating by paragraphs
| Action | Command |
|---|---|
| next paragraph | } |
| previous paragraph | { |
| go to a specific line | {line}gg |
Start/end of line and file
| Action | Command |
|---|---|
| start of the line | 0 |
| first non-space symbol of the line | ^ |
| end of the line | $ |
| go to the top of the file | gg |
| go to the end of the file | G |
Other
Important: go to the definition (this does not work in GVIM): gd
we can always append numbers, for example:
2w #go to the second word
4(arrow down) #go 4 lines down
Searching
| Action | Command |
|---|---|
| search forward | /{pattern} |
| search backward | ?{pattern} |
| continue search forward | n |
| continue search backward | N |
| find symbol on the line | f{symbol} |
Replacing
| Action | Command |
|---|---|
replace first occurrence of foo with bar
|
:s/foo/bar/ |
replace all occurrences of foo with bar in file |
:%s/foo/bar/g |
replace all occurrences of foo with bar in line |
:s/foo/bar/g |
| replace with empty string | :s/foo//g |
| replace with confirmation | :s/foo/bar/gc |
confirmation mode:
-
yreplace -
lreplace and quit -
nskip -
qquit -
areplace all
Note: search pattern can be regex!
:%s/^foo.*/Vim is the best/gc
Editing text
Insert mode
| Action | Command |
|---|---|
| Go to insert mode | i |
| Append text at the end of line | A |
| Add line below and go to insert mode | o |
| Add line above and go to insert mode | O |
| Remove last character typed | CTRL+H |
| Remove last word typed | CTRL+W |
| Remove last line typed | CTRL+U |
Editing commands
| Action | Command |
|---|---|
| copy (yanking) | y |
| delete | d |
| replace selected (word, line, whatever) | c |
| Copy whole line | Y |
| duplicate line |
YP or Yp
|
| delete the whole line | dd |
| delete paragraph | dp |
| delete until end of line | D |
| paste | p |
change (similar to d + i) |
c |
| add indentation | >> |
| remove indentation | << |
| format code | == |
Registers
- The unnamed register
"is where you copy and cut stuff to, when you don’t explicitly specify a register. The default register if you will. - The named registers
a-zare registers you can use explicitly to copy and cut text at will - The yank register
0stores the last thing you have yanked (copied) - The cut registers
1-9store the last 9 things you cut by using either the delete or the change command
| Action | Command |
|---|---|
| To copy to register | "{register name}y{motion} |
| To cut to register | "{register name}d{motion} |
| To paste from register | "{register name}p |
Inner/outer
change the text inside quotes: ci"
change the text inside brackets: ci)
change the text inside brackets: ci}
(you get the idea?)
Some general information
Editing commands are used in combination with navigation, just like that:
what to do (delete, change...)
/
/ how many times
/ /
v v
{operator}{count}{motion}
^
/
/
where to perform
the action
For example, delete two words: d2w
Undoing stuff
Undo: u
Top comments (0)