Lcode

Vim 101: Useful keybindings

@luisccode

30 September, 2020

What is vim?

Vim is a great terminal code editor focused on the modal editing philosophy. This means that it provides multiple modes and keybinding for each one that allows you to code very fast by using operators and motions, e.g: ci" (change inner quote) to change everything between the quotes or dw (delete word) to delete a word and you can combine those with numbers e.g: 3dw to delete the next three words. This is great because when you are coding, much of the time is spent traversing a document and vim has a lot of keybindings for move around the document and copy, paste, delete or update pieces of code very easily.

My favorites vim keybindings

These are some of my favorites vim keybindings and options that I've learned in the past three months, keep in mind that there are a lot of vim keybindings but in my opinion, these are some of the most useful:

:q - Quit

:q quit vim, :q! quit discarding changes, :qa quit all the documents and :qa! quit all the documents discarding changes.

:w - Write

:w write the current document, :wa write all the documents, and you can combine this for example: :wq to write and quit the current document or :wqa to write and quit all the documents.

u - Undo

u works like the regular ctrl-z, it undo changes.

i - Insert

i change vim to the insert mode.

v - Visual

v change vim to the visual mode.

j/k/l/h - Move

To move around the document you can use j/k/l/h to move down/up/right/left respectively. Also you can combine those with numbers e.g: 10j to move 10 lines down.

w - Word and b - Back

w and b are motions. w is used to jump to the next word and b to the previous.

d - Delete

dd delete the current line, 3dd delete 3 lines, dw delete the next word, db delete the previous word and dip delete inner paragraph. The delete operator move the content to the clipboard after delete it. Tip: if you want to delete a single character you can use x.

y - Yank

One of the most useful motions y copy text. yy copy the current line and yw copy the next word.

p - Paste

p paste content after the cursor and P paste content before the cursor. Tip: you can move a line by doing dd and then p paste it where you want.

c - Change

ciw change inner word, deletes the word and change vim to insert mode, cip to change inner paragraph and ci{ to change everything between the curly braces

a - Append

a puts vim into insert mode after the cursor and A puts vim into insert mode at the end of the line.

o - Open line

o open a new line below the cursor and O open a new line above the cursor.

r - Replace

r replace the current character. for example if you type rg will replace the current character with a "g". R let you replace characters until you change to the normal mode with <ESC>.

G and gg - Go

G go to the end of the file, g go to the beginning of the file, and [n]G go to the line number n, e.g: 10G go to the line number 10.

f - Find

f would find characters, for example if you type: fs will move your cursor to the next "s" character.

z

zz place the current line in the middle of the window, zt place the line at the top and zb place the line at the bottom.

That's all, I hope you find this article useful, if you like it, share it with your friends or let me know what you think in the comments below

Copyright ©2020 Luis Cortes. All Rights Reserved.