So you want to be a hipster, I mean be efficient and learn vim keybindings?
As with everything, the beginning will be hard, but all it takes is practice and muscle memory.
Getting help
You can’t learn this by simply reading. Make sure to practice often and use the available manuals and tutorials, they are actually quite good.
Use vimtutor as an excellent beginner tutorial.
vimtutor
Read up on a specific topic.
# Seriously, you can get help about everything:help word:help WORD:help d:help $
Normal mode
Navigation
Description
Key
Move left
h
Move right
l
Move up
k
Move down
j
Move word forward
w
Move word backward
b
Move word end
e
Move WORD forward
W
Move WORD backward
B
Move to end of WORD
E
Move to end of line
$
Move to beginning of line
0
Move to first non empty character of line
^
Move to specific character on line
f + character f. jump to the next dot on the line
Move to previous character on line
F + character F. jumps to the previous dot on the line
Move sentence up
(
Move sentence down
)
Move paragraph up
{
Move paragraph down
}
Move half page up
ctrl + u
Move half page down
ctrl + d
Move full page up
ctrl + u
Move full page down
ctrl + b
Move to start of page
gg
Move to end of page
G
Move to next curly braces, square brackets or parenthesis
%
Move to the top of the screen
H
Move to the middle of the screen
M
Move to the bottom of the screen
L
Info
Keys can be prefixed with an number. For example 20 k moves you up 20 times.
Info
WORD is not the same as word.
WORD is always delimited by whitespace.
ansible.builtin.import_role: is a single WORD.
word is delimited by non-keyword characters.
All of these are separate words:
ansible
.
builtin
.
import_role
:
Manipulating text
Description
Key
Copy full line
yy
Paste full line
p
Undo
u
Undo all changes on a line
U
Redo
ctrl + r
Dele full line
dd
Delete a word
d + i + w
Delete a WORD
d + i + W
Delete paragraph
d + i + p
Delete sentence
d + i + s
Delete up to specific character
d + t + character dt{ deletes up to next {
Change word
c + i + w
Change whole line
C
Repeat previous operation
.
Search forward for character
/ + character /atlantis moves to first iteration of the word atlantis from the cursor
Iterate forward
n
Iterate backward
N
Search forward for word under cursor
*
Search backword for word under cursor
#
Create markpoint
m + character ma creates markpoint named a
Move to markpoint
` + char `a moves to markpoint a
Move to previous location
` + `
Move to last edited location
` + .
Join next 3 lines together
3 + J
Make word uppercase
g + U + w
Make world lowercase
g + u + w
Make line lowercase
g + u + u
Make line uppercase
g + U + U
Increment a number
ctrl + a
Decrement a number
ctrl + x
Replace character on current position
r + character r + x replaces the current character with x
Replace test with develop in whole file
:s/test/develop/g
Replace ‘initial’ with ‘replacement’ in the 10 lines below the current line
:.,.+10s/initial/replacement/g
Replace ‘foo’ with ‘bar’ between line 50 and 75
:50,75s/foo/bar/g
Move to previous location in jump list
ctrl + o
Move to next location in jump list
ctrl + i
Show jump list
:jumplist :h jump-motions for more details
Folding
Description
Key
Fold all code blocks
z + M
Unfold all code blocks
z + R
Fold individual code block
z + c
Unfold individual code block
z + o
Advanced
Description
Key
Start recording
q + character q + a stored recording as a
Stop recording
q
Apply recording
@ + character @ + a applies recording a 15 + @ + a applies macro a 15 times
Enable relative line numbering
:set relativenumber
Switching modes
Description
Key
Insert mode before cursor
i
Insert mode after cursor
a
Insert mode at beginning of line
I
Insert mode at end of line
A
Insert mode after current line
o
Insert mode before current line
O
Change word and switch to insert mode
c + w
Change full line and sitch to insert mode
c + c
Insert mode
Visual mode
Command mode
Description
Key
Sort and keep unique entries
:sort u
Sort
:sort
Sort reversed order
:sort!
Load external file to cursor position
:read filename :read /tmp/x.txt reads the file and places the contents at cursor position
Load output from STDOUT to cursor position
:read !command :read !ls -ltr parses the output of ls -ltr to the current cursor position
Substitute on line
:s/old/new/g
Subtitute on file
:%s/old/new/g
Substitute between line numbers
:#,#s/old/new/g :10,20s/hi/hello/g substitutes hi to hello between line 10 and 20
Run commands
:!command :!ls -ltr runs the ls -ltr command
Write to specific file
:w FILENAME :w /tmp/test.txt
Registers
Vim uses a set of registers. You can use these register using the " key.
Info
If you do not have a system (+) register, make sure to install vim-gtk3.
apt install vim-gtk3
Description
Key
List all register
:reg
Use a specific register
” + register + operation "+y yanks the current line to the + register, which is the clipboard "3p pastes the text stored in register 3
I’ve attempted to start using vim bindings for a while now. The initial experience is extremely bad because I am 30 times slower with vim bindings than without. Much of that can be blamed on the fact that I just wanted to switch over completely in a big bang approach.
This time, I’ll try a different approach. Learn and master a small set of vim motions until I am proficient with them before attempting to learn more.
The first set of motions
Focus on moving the cursor via jkhl.
^ and $ to move to beginning and end of line.
Use g and G modifiers (dG,vgg).
Move back and forth between words and WORDS using b,B,w,W.
a and A to start inserting text.
Insert text at current position or beginning of line via i and I.
Using G to jump to specific locations (16G to move to line 16).
Utilize the c modifier to replace content. (ci" replaces everything between quotes).
The second set of motions
f,F
t,T
a(
a{
Navigate to previous location of jump list with Ctrl + i.
Navigate to next location of jump list with Ctrl + o.
Neovim is extremely customizable. So much that it might look very daunting in the beginning. Luckily there are tools like LazyVim that can help you to get a decent set up out of the box.