Vim Cheatsheet
Vim is a customizable command-line based text editor. It has modes that allow you to interact with it in different ways most crucially insert mode to inset text and command mode to, well, enter commands. Vim provides powerful commands that allow you to search and manipulate text extremely efficiently. It also keeps track of your usage in a viminfo file which allows you to, among other things, use the up arrow key to access your command history. This Vim cheatsheet uses the text from Alice’s Adventures in Wonderland to demonstrate concepts and it’s intended to serve as a tutorial to get started with Vim as well as a reference for experienced users.
Insert and Delete
By default, Vim opens in normal mode which is useful for switching between modes. This section shows how to enter insert mode. When your done inserting things, you can get back to normal mode with the escape key (Esc
).
Insert before (left of) the cursor
i
Append / Insert to the right of the cursor
a
Capital I
or A
are used to insert at the start or end of the current line respectively.
Open line / Insert on a new line below the cursor
o
Insert on a new line above the cursor
O
Undo
u
Redo
Ctrl+r
Selecting, Copying, and Pasting
Delete / Cut character
x
Cut line
dd
Combine cut with the directional keys e.g. j
for down and a number for quantity, say, this line and the 2 lines below it.
d2j
If you omit the number, one is assumed so to cut this line and the 1 above
dk
You can also combine d
(cut) with l
or h
for characters to the left or right or w
and b
for the number of words forwards or backwards to cut.
All of this works the exact same way for y
(copy/yank).
Paste
p
for pasting after the cursor and P
for pasting before.
Visual Modes
The visual modes can be used for selecting lines or blocks and manipulating them in bulk.
Visual mode
v
Enter visual line mode
V
Enter visual block mode. This is super useful to edit multiple lines at once. For example if you want to insert or delete a column of spaces or text.
Ctrl+v
You can then select with the directional keys, discussed in the Navigation section below. Some of the most common operations to perform on the selected text are the following:
I
to insert the same text on each linec
to change the content in the selectionr
to replace the selected content with a single characters
to substitute the selected text i.e. delete it and put you into insert mode
Navigation
The arrow keys work as expected for up
, down
, left
, and right
or in normal mode k
, j
, h
, and l
Go to the start of the file vim only (not vi)
gg
Go to the end of the file
G
Line number e.g. line 1
:<line number>
:1
Start of line
^
or 0
End of line
$
Forward one word
w
Back by one word
b
End of current word
e
Back and forward by a paragraph
{
and }
Page down
Ctrl+f
Half page up
Ctrl+u
Search forward e.g. “Miss Alice”
/<search term>
/Miss Alice
- Use
\c
anywhere in the string to match case insensitive e.g.
/\cmiss alice
- Go to next and previous matches (loops back to first match)
n
andN
Search backward works the same way as forward but with a question mark command e.g.
?Miss Alice
Searching in vim uses regular expressions so you can search for patterns, rather than simple strings.
Search and Replace
You can substitute an expression with the :s
command so to have a tea party with a mad programmer, we can substitute “Hatter” for “Programmer”. However search and replace also works with regular expressions to replace more complex patterns.
:s/Hatter/Programmer/
This replaces the first instance of “Hatter” with “Programmer” on the current line as shown below:
<`In THAT direction,' the Cat said, waving its right paw round, <`lives a Hatter: and in THAT direction,' waving the other paw, <`lives a March Hare. Visit either you like: they're both mad.' --- >`In THAT direction,' the Cat said, waving its right paw round, >`lives a Programmer: and in THAT direction,' waving the other paw, >`lives a March Hare. Visit either you like: they're both mad.'
- You can also use the global (
g
) flag to change every instance - Use the
c
flag to ask for confirmation for each substitution - Select the range of lines to perform the operation on e.g.
Substitute every case-insensitive “cat” for the string “dog” from the current line (.
– current line – is implied so is omitted) to the end of the file, asking for confirmation for each change:
:,$s/\ccat/dog/gc
Substitute every “Alice” for “Deborah” throughout the entire file i.e. change the story to Deborah’s Adventures in Wonderland:
%
is the range of the entire file aka1,$
- We’re omitting the
c
flag to yolo it (no confirmation prompts)
:%s/Alice/Deborah/g
`Wake up, Deborah dear!' said her sister; `Why, what a long sleep you've had!'
Opening and Closing Files
Open a new, named file
vim new_file.txt
Launch Vim and open an unnamed file
vim
From within Vim, you can use the :e
command to open a file.
:e </path/to/file>
You can split the window so you can open multiple files in a single vim instance
:split
or:sp
for short:vsplit
or:vsp
for short – to split vertically:sp </path/to/file>
:vsp </path/to/file>
Switch between windows
Ctrl+w
(x2)
You can share buffers between files within the same vim instance allowing you to copy and paste between files etc.
Open a file using Vim’s file manager
:E
The file manager can also be used to manage files from within Vim
d
Create a new directory%
Create a new fileEnter
Open selected file/directoryD
Delete selected file/directory-
Navigate to parent directorys
Cycle through sort sequenceS
Edit sort sequence:q
Close the file manager
Open a file in binary mode (Won’t append a newline at the end of file).
vim -b <binary_file>
Remove a newline at the end of a file
:set noeol
Save and quit (combine the write and quit commands)
:wq
or ZZ
Quit without saving – discarding changes (force quit)
:q!
Quit a file where you have made no changes
:q
and :qa
to quit all windows
Vim Configuration
Vim can be configured in a system-wide context, as well as per user using the vimrc files.
Set Tabs to Four Spaces
filetype plugin indent on set tabstop=4 set shiftwidth=4 set expandtab
You can then replace all the tabs in an existing file with the :retab
command.
Syntax Highlighting
Turn on basic syntax highlighting, if it’s not on by default with the following configuration.
syntax on
Vim’s in-built help
This cheatsheet covers many of the most common operations but there’s far more still to discover. You can get help with a feature using the :h
command, short for :help
e.g. for the help file on range
:h range
Or for help with the :help
command
:h help
When used without any argument, the :h
command displays the main Vim help file.
You now know everything you need to edit efficiently with vim and where to go to find out more!
References
- https://docs.oracle.com/cd/E19253-01/806-7612/editorvi-43/index.html
- https://stackoverflow.com/questions/41670493/visual-block-edit-in-vim
- https://stackoverflow.com/questions/2287440/how-to-do-case-insensitive-search-in-vim
- https://stackoverflow.com/questions/7598133/how-to-search-and-replace-globally-starting-from-the-cursor-position-and-wrappi
- https://stackoverflow.com/questions/4620672/copy-and-paste-content-from-one-file-to-another-file-in-vi
- https://superuser.com/questions/486532/how-to-open-files-in-vertically-horizontal-split-windows-in-vim-from-the-command
- https://stackoverflow.com/questions/426963/replace-tabs-with-spaces-in-vim
- https://stackoverflow.com/questions/234564/tab-key-4-spaces-and-auto-indent-after-curly-braces-in-vim
- https://stackoverflow.com/questions/11272501/enable-vim-syntax-highlighting-by-default
- https://en.wikipedia.org/wiki/Vim_(text_editor)