Vi and Vim Quick References: A One Page Cheat Sheet

Channel: cheat-sheets
Abstract: map for mapping a key in command mode to a group of commands. Example$s/str1/str2/gcsearch and substitute str1 by str2 from the current line to the en

Have you ever felt slow working with Vi/Vim?

Vim is a free and open-source, screen-based text editor program inspired by Vi. It may be difficult for newcomers and even long-term users to remember all the different commands. This one-page cheat sheet will help you get back to work quickly and improve your velocity while working in Vi/Vim: Vi & Vim Cheat Sheet in PDF format.

This post covers the core essentials Vi & Vim cheat sheet, including brief references of Vi insert mode, edit commands, ex commands, navigation, search and substitute, etc. The cheat sheet is also available in an easy-to-print pdf and jpg format. You may also want to check my post on the Differences Between Vi and Vim.

? You have to use the ESC (escape key) to get into the vi command mode. You will need to press the RETURN Key for executing a command starting with the ponctuation character : or / or ?. Use CTRL+c to cancel a command. Use . to repeat your last command and :!cmd to execute a shell command (where cmd is the command to execute).

Insert & Replaceiinsert text before the cursorainsert text after the cursorIinsert text at the beginning of the current lineAinsert text at the end of the current lineoinsert text in a new line below the cursorOinsert text in a new line above the cursorrreplace a character at the cursor positionRreplace characters starting at the cursor positionCursor Motionh or move cursor leftl or move cursor rightk or move cursor upj or move cursor downGgoto the end of filenG or :ngoto the line number n0move to the beginning of the line$move to the end of the lineCTRL+fmove one screen view forwardCTRL+bmove one screen view backwardEx Commands:qquit current open file:q!force to quit without saving changes:wsave file:w filesave file as file:wq!force to save file then quit:x,yw filewrite from line x to line y into file:w » fileappend buffer to file:e fileedit another file:e! fileedit another file without saving current changes:r fileinsert file content at the current cursor position:nedit next file in vi arguments file listCTRL+Gdisplay current file name and positionSearch & Substitute/stringsearch forward for string?stringsearch backward for stringnrepeat the last search:,$s/str1/str2/gcsearch and substitute str1 by str2 from the current line to the end of file. A line number can be specified before the comma 「,」. The 「c」 ask for confirmation before each substitution:%s/str1/str2/greplace all str1 by str2 in all the file without prompting for confirmation:%s/str1/str2/replace str1 by str2 on the first occurrence in each line of the fileUndo, Delete, and Copyuundo last changeCTRL+rredo last undo changemAset mark A to current position. Marks can be the letter [a-z] and [A-Z]y’Ayank (copy) from current line to mark Ad’Adelete from current line to mark APput the buffer content before the cursorpput the buffer content after the cursorxdelete character at cursor positiondWdelete first word after cursor positiond$ or Ddelete from cursor position to end of linedddelete current lineJjoin current line with following lineGoing Further
  • :map for mapping a key in command mode to a group of commands. Example: :map de :1,$d^M will delete all lines when using the :de command.
  • :set to define or show your editor options. Example: :set number will make vi display line numbers, and 「:set all」 will display all current vi options.
  • :ab to define a text abbreviation in insert mode. Example: :ab VIM Vi Improved will auto-complete 「VIM」 in insert mode for the phrase 「Vi Improved」.

Ref From: shell-tips

Related articles