Vim cheatsheet

Vim cheatsheet

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

ActionCommand
Exit w/o saving:q!
Exit from all splitsqa
Exit and save:wq!
Go to normal modeESC or CTRL+C
Repeat last change.
Open file:open {filename}
Execute terminal command:!command

Splits (tabs)

ActionCommand
Open file in vertical split:vsp {filename}
Navigate between splitsCTRL+W (corresponding arrow)

Visual mode

Visual mode is used to select different parts of texts

ActionCommand
go to the character-wise visual modev
go to the line-wise visual modeV
to select "block"CTRL+v

Multiple cursors

Note: works in VSCode, doesn't work in GVIM

Add multiple cursors

  1. Select lines you want to change (using visual mode)
  2. type I

Add cursor for word

  1. move cursor to the start of the word
  2. type gb to add another cursor
  3. repeat :) then use c, ESC or d or whatever

Navigating

Vim allows lots of advanced navigation features.

Navigating by words

ActionCommand
next wordw
next WORDW
previous wordb
previous WORDB
next end of worde
jump to matching bracket%

Navigating by paragraphs

ActionCommand
next paragraph}
previous paragraph{
go to a specific line{line}gg

Start/end of line and file

ActionCommand
start of the line0
first non-space symbol of the line^
end of the line$
go to the top of the filegg
go to the end of the fileG

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

ActionCommand
search forward/{pattern}
search backward?{pattern}
continue search forwardn
continue search backwardN
find symbol on the linef{symbol}

Replacing

ActionCommand
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:

  • y replace
  • l replace and quit
  • n skip
  • q quit
  • a replace all

Note: search pattern can be regex!

:%s/^foo.*/Vim is the best/gc

Editing text

One of the biggest differences between Vim and traditional text editors is "mode" system. Let's demystify editing text in Vim.

Insert mode

ActionCommand
Go to insert modei
Append text at the end of lineA
Add line below and go to insert modeo
Add line above and go to insert modeO
Remove last character typedCTRL+H
Remove last word typedCTRL+W
Remove last line typedCTRL+U

Editing commands

ActionCommand
copy (yanking)y
deleted
replace selected (word, line, whatever)c
Copy whole lineY
duplicate lineYP or Yp
delete the whole linedd
delete paragraphdp
delete until end of lineD
pastep
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-z are registers you can use explicitly to copy and cut text at will
  • The yank register 0 stores the last thing you have yanked (copied)
  • The cut registers 1-9 store the last 9 things you cut by using either the delete or the change command
ActionCommand
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

PS

I hope you enjoy my blog. Did you know that I have YouTube channel as well? On this channel I explain:

1) Developers how to write cleaner and more testable code. TDD, Clean Code, XP, Agile, these sorts of things. 2) SDET and TA Engineers - how to create efficient automated testing. Test automation, test strategy, DevOps. 3) Junior developers - the fundamentals they need to know to build a successful career. Data Structures and Algorithms, career progression and interviewing tips.

If it sounds interesting - please subscribe here: youtube.com/channel/UCkJRvCK2-l2gCuPT9-HUG7A