Vim Cheat Sheet – Basics

Linux Penguin

Vim is a very powerful text editor that provides a huge array of extended functionality over the standard editors included with Linux distros.  This Vim cheat sheet contains a common set of functions an every day user will most often use,  however it by no means covers everything. For full functionality, you should read the Vim documentation.

Editing a File

To begin editing a file in Vim, type:

vim /directory/path/filename   #This allows you to start editing a file from any location other than the folder you are in.
vim filename   #This will open filename from the directory you are in. Note: if the file does not exist, then a blank Vim window will still open.

You can also use the following commands from within Vim

:eReload the current file for editing. This is useful to re-edit the current file, when it has been changed outside of Vim.
:e!Edit the current file always. Discard any changes to the current buffer. This is useful if you want to start all over again.
:e {filename or /directory/path/filename}Edit {filename}.

 

Insert Mode

Insert mode is the mode you need to be in to add your text to the file you are editing. The most common way of entering Insert Mode is simply by typing an  i . However there are a few additional ways that also add lines or move the cursor to specific location.

aEnter insert mode after the character directly in front of the cursor.
AEnter insert mode at the end of the current line.
iEnter insert mode where the cursor is currently located.
IEnter insert mode before the first non-blank character on the current line.
REnter Insert mode but replace characters when typing rather than inserting
oBegin a new line below the cursor and enter insert mode, repeat [count] times.
OBegin a new line above the cursor and insert text, repeat [count] times.
:r {filename}Insert the filename below the cursor.
:r!{cmd}Execute {cmd} and insert its standard output below the cursor.
[Backspace]Deletes text in same manner as in a normal desktop application.
[Esc]Exit Insert mode.

 

Deleting Text

[Delete] – can also use xDelete characters one at a time from in front of the cursor.
XDelete characters one at a time before the cursor
d{selection}Delete text that has been selected. You can either select text with your mouse or using visual mode options.
ddDelete the current line entirely
DDelete the characters under the cursor until the end of the line (leaves the line blank)
{Visual}X or {Visual}DDelete the highlighted lines

 

Replacing or Changing Text

r{char}replace the character under the cursor with {char}.
~Switch case of the character under the cursor and move the cursor to the right. If a [count] is given, do that many characters.
~{motion}switch case of {motion} text.
{Visual}~Switch case of highlighted text

 

Copying and Moving Text

Copying and pasting text is relatively easy in Vim. The term “Yank” is used for referring to the copy function.

“{a-zA-Z0-9.%#:-“}Use register {a-zA-Z0-9.%#:-“} for next delete, yank or put (use uppercase character to append with delete and yank) ({.%#:} only work with put).
:regDisplay the contents of all numbered and named registers.
:reg {arg}Display the contents of the numbered and named registers that are mentioned in {arg}.
yyYank line.
YSame as yy.
:[number]yYank [number] lines.
pPut the text after the cursor [count] times.
PPut the text before the cursor [count] times.
gpJust like “p”, but leave the cursor just after the new text.
gPJust like “P”, but leave the cursor just after the new text.
:[linenumber]puPut the text after [linenumber] (default current line).
:[linenumber]pu!Put the text before [linenumber] (default current line).

 

Undo/Redo/Repeat

uUndo changes one at a time with each key press.
:uUndo one change.
[Ctrl] RRedo changes which were undone one at a time with each key press.
:oRedo one change which was undone.
UUndo all latest changes on current line.
.Repeat last change.

 

Moving Around

If you have arrow keys on your keyboard(most do these days), you can use these to navigate the basic up, down, left, right commands. However, there are other commands which Vim provides that will allow you to move more directly to where you want to be in file. In case you don’t have arrow keys, there are also substitutes for these.

hMove left one character at a time.
lMove right one character at a time.
kMove up one line at a time
jMove down one line at a time.
0Move to the first character of the line.
^Move to the first non-blank character of the line.
$Move to the end of the line.
g0When lines wrap, this will move to the first character of the screen line instead of the actual text line.
g^When lines wrap, this will move to the first non-blank character of the screen line instead of the actual text line.
g$When lines wrap, this will move to the last character of the screen line  instead of the actual text line.
f{char}Moving to the right, go to the first occurrence of the character entered.
F{char}Moving to the left, go to the first occurrence of the character entered.
;Repeat latest f{char} command on current line.
,Repeat latest F{char} command on current line.
GMove to the end of the file.
ggMove to the beginning of the file.
wMove forward one word at time. This does not count special characters as words, so it will just move you based on groups of letters and/or numbers that do not contain special characters.
WSimilar to w but includes special character in the grouping
eMove to the last character of the current word. This does not count special characters as words, so it will just move you based on groups of letters and/or numbers that do not contain special characters.
EMove to the last character of the current word.
bSame as w but moving backwards.
BSame as W but moving backwards.
geSame as e but moving backwards.
gESame as w but moving backwards.
(Move backwards one sentence at a time.
)Move forwards one sentence at a time.
{Move backwards one paragraph at a time.
}Move forwards one paragraph at a time.

 

Visual Mode – Selecting Text

The easiest way to select text to perform actions on is to enter visual mode with one of the commands below. You can then use motion commands to highlight the text you want to perform an action on.

vEnter Visual mode and select characters one at a time.
VEnter Visual mode and select one line at a time.
X or DDelete the highlighted lines
[Esc]exit Visual mode without making any changes.

 

Searching

/{pattern}Search forward for the occurrence of the entered {pattern}.
/{pattern}/{offset}Search forward for the occurrence of entered {pattern} and go {offset} lines down.
?{pattern}Search backward for the previous occurrence of the entered {pattern}.
?{pattern}?{offset}Search backward for the previous occurrence of the entered {pattern} and go {offset} lines up.
nRepeat the latest “/” or “?” [count] times.
NRepeat the latest “/” or “?” [count] times in the opposite direction.

 

Exit Vim

:qQuit Vim. This fails when changes have been made.
:q!Quit without writing.
:cqQuit always, without writing.
:wqWrite the current file and exit.
:wq {file}Write to {file}. Exit if not editing the last
ZZWrite current file, if modified, and exit.
ZQQuit current file and exit (same as “:q!”).