DEV Community

Cover image for The Vim Reference
Richard
Richard

Posted on • Edited on

The Vim Reference

Recently I decided to learn vim after hearing how much Ben Awad loves it. I though maybe it could boost my productivity too. I am still getting use to it, but right now it seems pretty fun.

Now to the reference. Here are some useful commands and what they do:

Using Vim

  • Open Vim: vim filename
  • Closing Vim:
    • :wq -> save and quit
    • :q -> quit and not save
    • :w -> save and not quit

Movements

  • Hitting gg will take you to the top of file
  • G takes you to the bottom
  • } and { navigate code blocks
  • o adding a new line and put into insert mode
  • O adding a new line above and put into insert mode
  • w will jump to next word b will take you back
  • :number will take you to line number
  • 0 will take you to the start of the line
  • 0 + w or ^ will take you to the first word of a line
  • h j k l to move up down left and right
  • a move over by 1 and set insert mode
  • A jump to end and set insert mode

Editing

  • Hitting "i" in command mode lets you insert
  • Hitting shift + i will set insert mode at the beginning of the line
  • Then esc puts you back into command mode
  • Hitting dd will delete a line and copy to clip board
  • Hitting d + } will delete a whole block of code
  • Hitting 10 dd will delete 10 lines
  • Hitting u will undo
  • Hitting ctrl + r will redo
  • Hitting yy will copy a line
  • p to paste bellow P to paste above
  • x remove a letter where the cursor is
  • number x remove number letters before cursor
  • ~ swap the case of the letter prefix with number for multiple characters
  • . redo

  • $ will take you to the end of a line

  • W will jump spaces and B reverses that

  • f + char find and place cursor on char

  • t + charfind and place cursor before char

  • % take you to and from closing brackets

  • d + % remove code between brackets

  • c + w change mode and word will remove a word and set insert mode

  • D remove everthing from cursor onwards

  • c + t + char edit from cursor to char

  • C change whole line

  • d + t + char delete from cursor to char

  • * take you to occurances of the word where your cursor is

  • ; take you to next instance after using a find command

  • r + char replace letter at cursor with char prefix with number for multiple replaces

  • R allow to replace lots of letters into replace mode

  • /word highlight all occurances of word

Macros

  • Press q to start recording then select a hotkey. Now run some commands and hit q to end. Then hit @ hotkey to replay a command. Prefix @ with a number to replace that many times

Visual Mode: V

  • Select text with movement commands then you can hit:
    • d to delete
    • After selected > or < to indent code.
  • shift + v selects the whole line and puts you into visual mode.
  • v + move select from cursor while move

Visual Block Mode

ctrl + v enter visual block mode. Now you can select columns and hit r to replace things.
In visual block mode hitting shift i will allow you to type and then after you hit esc the text will appear in all places selected.


Thank you for referring to this guide, I hope you found it useful.


Photo by Bich Tran from Pexels

Top comments (2)

Collapse
 
leob profile image
leob • Edited

You forgot one alternative key combo for "save and quite", it is:

shift-Z-Z

(two key presses of Z while keeping Shift pressed) - it's my favorite over :wq

A few others:

:q! to quite and discard changes
control-G to display the line number and status

and my favorite settings inside .vimrc:

set ignorecase
set ruler
set hlsearch

Collapse
 
merichard123 profile image
Richard

That's awesome! Thanks!