What you really need to know when starting to learn Vim motions.
Vim motions are awesome and work almost everywhere. I share here an extract and condensed version of what the most important commands are to get you productive as fast a possible. This document is not here to teach you all of Vim's magic (because I don't know them too =)) but rather to present you the 10-20 most valuable commands which made my journey in Vim a steep learning curve. From this basics presented in this document you can start exploring more and more and refine your habits with even more awesomeness.
Start with as few as possible, but be effective at the same time.
You can use VS Code with a Vim extension or better neovim
to follow the below
summary. Many text based softwares have a vim-mode, or plugins.
Ctrl+c or Esc
Gets you into Normal
mode from anywhere. Press this when you entered wrong
command sequences or other mistakes.
Tip
Remap Caps Lock to Ctrl or Esc to ease finger strain. You can do this with:
- MacOS: In System Settings (Keyboard -> Modifiers Keys) or with Karabiner-Elements.
- Windows: PowerToys or dual-key-remap.
Go to normal mode (see above) and press u to undo the last action. To redo press Ctrl+r.
Vim is a modal editor. It has three important modes:
Normal
: Jump around and use keybindings (default).Insert
: Write text.Visual
: Select lines/blocks of text
You can switch any mode from the normal mode using the following keys:
- i/a :
Insert
- v :
Visual
mode (normal selection). - Shift + v :
Line Visual
mode (line selections). - Ctrl + v :
Block Visual
mode (block selections).
In normal mode, you can press : to enter Command
mode. You won't
use this much at the beginning. If you accidentally end up in Command
mode,
press Esc or Ctrl+c to go back into Normal
mode.
To move your cursor in the buffer you have to learn the following keys. This takes some time but you will memorize them after 2-3 days.
- h and l : Moves one character to the left/right in the current buffer.
- j and k : Moves line down/up in the current buffer.
Lots CLI tools etc. also use these mappings and its best to stick to them. It is also more efficient as they keep your fingers on the home row.
Note
To battle squeeze these mappings into your mechanical cortex I
recommend ThePrimagean's plugin
game in nvim
to quickly memorize or play
this game.
The next most important command is to insert at the beginning (skipped white space) or the end with
- I : Insert at the beginning of the line.
- A : Insert at the end of the line (append).
To move faster on a line you should memorize:
-
w or b : Jump to the next/previous starting word.
-
W and B : Jump to the next/previous starting word (with punctuation).
-
Ctrl+d and Ctrl+u: Move cursor down/up 1/2 page.
Copy (yank) and delete are vim operators: they change text objects.
Doubling the operator key applies it to the line (a text object).
- yy : Copy the current line.
- dd : Delete the current line.
- p : Paste the clipboard after the cursor.
- P : Paste the clipboard before the cursor.
- o : Insert on a new line below the current one.
- O : Insert on a new line above the current one.
This command seems useless since you have Backspace, however
correcting small mistakes is easier like this because you will move around with
your cursor in Normal
mode and then when pressing x or s
will directly delete/substitute the current character:
- x : Delete the current character in
Normal
mode or the selection inVisual
mode. - s : Substitute the current character in
Normal
mode or the selection inVisual
mode.
To select lines go to Visual
mode with v or Block Visual
mode
with Ctrl+v. When the selection has been done, you can
copy/delete or change/substitute it with the commands already learned.
This is where the fun truly begins! Vim allows to combine motions and operators to form "sentences". Sentences have the following structure:
[count]<operator>[count]<motion>
For example to delete two words: d2w
Tip
You do not need to master a ton of these, see the next sections for the useful ones.
Note
The first count in the structure above is used to run the whole sentence multiple times
"Below" are some other useful sentences you want to remember:
Change Inside/Around:
- ca" : Change (c) around quotes
"
found from the current cursor position. - ci" : Change (c) inside quotes
"
found from the current cursor position.
Yank Inside/Around:
- ya" : Copy (y) around quotes
"
found from the current cursor position. - yi" : Copy (y) inside quotes
"
found from the current cursor position.
Delete Inside/Around:
- da" : Delete around quotes
"
found from the current cursor position. - di" : Delete inside quotes
"
found from the current cursor position.
Copy/Delete/Change till Character:
- ct? : Change the text selection till the next character
?
. - yt? : Copy the text selection till the next character
?
. - dT? : Delete the text selection till the previous character
?
.
Select Inside/Around/Till Character:
- va" : Select the text around quotes
"
found from the current cursor position. - vi" : Select the text inside quotes
"
found from the current cursor position. - vt? : Select the text till the next character
?
. - vT? : Select the text till the previous character
?
.
Copy/Delete/Change Till End:
- c$ : Change till end of line.
- c0 : Change till beginning of line.
- y$ : Yank till end of line.
- y0 : Yank till beginning of line.
- d$ : Delete till end of line.
- d0 : Delete till beginning of line.
Note
Yeah, you guessed it, you can use also other things in place of "
.
Try to use ( or [ or { or ` or
' etc. to easily change around/inside this characters. Also you can
use w or W to do the action inside/around a word with and
without spaces.
- gg : Jump to start of the file.
- GG : Jump to end of the file.
- >> : Add indentation on the current line.
- << : Remove indentation on the current line.
- == : Format current line.
In Block Visual
mode (Ctrl+V): Select some lines, then
use
- I : Insert on all lines. Once done, press Esc to make it apply on all lines.
- C : Change the selection on all lines. Once done, press Esc to make it apply on all lines.
Vim keeps a list of your jumps within or between buffers. You can jump back and forth in you jump history with Ctrl+i/Ctrl+o.
When programming, this is very useful in conjunction with gd
(go to definition
== Ctrl+click in VSCode)
That's basically it. These are the top commands I use daily. There is obviously much more on the Vim motions side. Now, start practicing it, once learned you will not change using it, trust me. =)
- NVim IDE setup is complicated
- i.e. just use VSCode or Zed.
- Non-
nvim
editors: Keybinding logic not 100% consistent but you get most of it!
Some editors with different scope: