Vim cheatsheet for sysadmins
- Request help
- → All mini tutorials
- → All English blog posts
- → All German blog posts
- → Lab Environments
- → FreeBSD-based resources
On this page:
- How to enter or switch to Vim NORMAL, INSERT, VISUAL, or command mode?
- Can I work with more than one window in Vim?
- How to move the cursor in Vim?
- How to search and replace in Vim?
- How to edit or delete characters and lines in Vim?
- Which special things can you do with Vim?
- How to remap function keys in Vim for efficient HTML editing?
- Example .vimrc configuration file for Vim
This cheatsheet assumes that you spent 60 minutes of your life for Vimtutor. You may also use vimtutor as a command or from within Vim :help tutor. The beauty of Vim stems from the fact that you can edit text files incredibly fast without using a mouse.
How to enter or switch to Vim NORMAL, INSERT, VISUAL, or command mode?
- You always start in NORMAL mode. From here:
- In NORMAL mode, you can repeat your last action, e.g. replacing a character, anywhere in the file by typing a single dot. This is a dot: .
- Typing a colon : takes you to command mode which is directly followed by a command, without a space in between. Example: :help
- Typing v takes you into VISUAL mode which allows you to highlight/select text for further processing. Select text in VISUAL mode by moving the cursor. A capital V not only takes you into VISUAL mode but also highlights/selsects the full line where the cursor is located.
- You can type text in INSERT mode. You may enter it the following ways:
- Typing i or a takes you into INSERT mode in front of or after the current cursor position, respectively.
- Use their capital equivalents I and A to enter INSERT mode at the beginning and the end of the line, respectively, where the cursor is located.
- Typing O (the capital letter, not the number) inserts an empty line above the cursor position and takes you into INSERT mode. A lowercase o does the same, but below the cursor position.
- Go back to NORMAL mode by pressing the ESC key.
Can I work with more than one window in Vim?
You may use split windows or tabs.
How to split a window in Vim horizontally or vertically?
:split your_file.txt and :vnew other_file.txt, respectively.
How to resize a split window in Vim horizontally or vertically?
CTRL+w and then + or - (horizontal split) or < or > (vertical split).
How can I switch between my split windows in Vim?
CTRL+ww, that's two times the letter w, pressed quickly.
My Vim split windows and/or tabs seem to overlap and the file I want to see is somehow hidden. What can I do?
CTRL-o and CTRL-i will bring the Vim window you want back to the front.
How to use tabs in Vim?
:tabnew and then switch between your tabs using gt or gT. Close all tabs except the active one using :tabo.
My tab or split window in Vim is empty, how to open a file?
:e and specify a path to a file or use tab to autocomplete.
How to move the cursor in Vim?
Most of the time the arrow keys work intuitively. If not, you can navigate using the hjkl keys in NORMAL mode.
How to go to the first line of a file in Vim NORMAL mode?
gg
How to go to the last line of a file in Vim NORMAL mode?
G
How to go to the first line on the currently visible Vim window in NORMAL mode?
H
How to go to the middle on the currently visible Vim window in NORMAL mode?
M
How to go to the last line on the currently visible Vim window in NORMAL mode?
L
How to jump to a line in Vim NORMAL mode?
15G for line 15.
How to jump a certain number of lines in Vim NORMAL mode?
23- moves 23 lines upwards. 42+ moves 42 lines downwards.
How to jump to the beginning of the line above in Vim NORMAL mode?
- (that's a hyphen).
How to jump to a line using Vim command mode?
:15 and hit Enter
How to jump a certain number of lines in Vim command mode?
:-15 would bring you 15 lines backwards. Use :+15 if you want to go forwards.
How to move to the first letter of the next word in Vim NORMAL mode?
w
How to move to the last letter of the next word in Vim NORMAL mode?
e
How to move to the first letter of the previous word in Vim NORMAL mode?
b
How to repeat the cursor movements in Vim NORMAL mode multiple times?
6e would jump to the end of a word 6 times.
How to move the cursor to the beginning or the end of a line in Vim NORMAL mode?
0 as in zero moves the cursor to the beginning of the line. $ moves to the end.
How to jump between parentheses, brackets, or braces pairs in Vim NORMAL mode?
Use % when the cursor is on the opening or closing character of the pair.
How to search and replace in Vim?
Note that Vim requires a backslash \ as an escape charcter if you need to match one of the following characters.
& ` ^ $ . * [ ~ ) + / \ :
# depending on your system you sometimes also need to escape a space
How to search a string forwards or backwards Vim NORMAL mode?
/searchterm and ?searchterm, respectively.
How to jump to the next or previous search result after a search in Vim?
n and N, respectively.
How to search the word at the current cursor position in Vim NORMAL mode?
Type #.
How to remove the highlighting of Vim search results?
:noh from Vim NORMAL mode and hit Enter.
How to search for multiple words in Vim NORMAL mode?
/searchterm\|secondsearchterm for OR-searching. Find the exact sequence of words by escaping whitespace: /exact\ sequence\ of\ words.
How to ignore case when searching in Vim?
:set ic and reset using :set noic.
How to search and replace in the full file or only between a selection of lines from Vim NORMAL mode?
:%s/old/new/g and :42,1337s/old/new/g, respectively.
Note that Vim's search and replace syntax is mostly the same as the sed command's syntax (sed command examples).
How to get prompted for confirmation for each potential replacement individually when I search in Vim?
:%s/old/new/gc and :42,1337s/old/new/gc, for example.
How to remove duplicate lines in Vim?
Select using VISUAL mode or define lines: :42,1337!uniq
How to capitalise a VISUAL mode selection in Vim?
Select, then U for uppercase or u for lowercase. To swap all casing in a VISUAL mode selection, press ~ (tilde).
How to use search and replace to capitalise the first letter of each word in a VISUAL mode selection in Vim?
s/\<./\u&/g
\< matches the start of a word
. matches the first character of a word
\u tells Vim to uppercase the following character in the substitution string (&)
& means substitute whatever was matched on the left-hand side
g means substitute all matches, not only the first
How to edit or delete characters and lines in Vim?
How to remove or replace a single character in Vim NORMAL mode?
x and r, respectively. In the latter case the replacement character must be entered next. If text was selected using VISUAL mode and r is used, then the selected text will be replaced by the next character you enter.
How to replace consecutive characters from the position of the cursor in Vim NORMAL mode?
R and start typing. You may consider this REPLACE mode.
How to delete the line of the cursor position in Vim NORMAL mode?
dd
How to use d for delete and a Vim movement key to remove a part of a text?
3$d would delete from the cursor position up to the rest of the line plus two additional lines. You can combine all movements such as 0 (the number, not the capital letter) for "go to the beginning of the line" with many actions such as d for delete.
How to indent a line (NORMAL mode) or a highlighted section (VISUAL mode) in Vim?
> or >>, respectively. Works in the opposite direction as well.
How to delete lines above and below the current cursor position in NORMAL mode?
:+3d and :-2d delete the 3rd line below and 2nd line above current cursor position, respectively.
:-5,-d removes all 5 lines above current cursor.
:+,+5d removes all 5 lines below current cursor.
How to copy a full line or only the character of the cursor position in Vim NORMAL mode?
Y and y, respectively.
How to paste in Vim NORMAL mode?
p pastes what you copied (or deleted) right to the current cursor position (append). A full line in the buffer will be inserted below the line auf the current cursor position.
P pastes what you copied (or deleted) left to the current cursor position (insert).
How to undo the changes I just made in Vim in NORMAL mode?
u to undo and :redo or CTRL+R for the opposite. This is similar to CTRL+Z and CTRL+Y in many graphical environments.
How to insert the same character at the beginning of multiple lines in Vim, e.g. to outcomment multiple lines of Python code?
Highlight the lines in question using v (VISUAL mode), then type :%norm i# (to insert a single "#" at the beginning of the line). Note that there is no space between the "i" and the "#".
Removing the first 18 characters of a line would be accomplished using :%norm 18x.
Remove the trailing 35 characters of a line (this is actually search and replace): s/.\{35}$//.
Which special things can you do with Vim?
How to open a terminal from within Vim?
Use :term.
How to remote-edit files using Vim and SCP?
In this example it is assumend that connection details are configured in ~/.ssh/config. Also note the double slash between the SSH connection details and the path to the remote location (the standard double slash for the protocol is not meant here.
vim scp://taken_from_Host_ssh_config_section//absolute/path/to/file.txt
# also works from within Vim
:tabnew scp://taken_from_Host_ssh_config_section//absolute/path/to/file.txt
How to show Vim history and reuse the Vim commands I previously used?
q: from NORMAL mode. What you see then behaves like a split window. Select the command you wish to execute again and hit Enter.
How to execute a shell command in Vim, e.g. to try out a small Python script without leaving Vim?
:!clear && python your_miniscript.py
How to save highlighted text in Vim to a file?
Highlight using VISUAL mode, then :w /path/to/newfile.txt.
How to insert the full content of a file to Vim?
:r ~/path/to/file.txt
How to retrieve STDOUT and insert it into Vim?
For example, :r !ls would insert STDOUT of the ls command.
:r !tail --lines=20 ~/.vimrc would insert the last 20 lines of your .vimrc.
How to show a list of available Vim commands?
: and TAB will show you a list (autocomplete behaviour).
How to convert unformatted JSON into pretty JSON in Vim?
:%!jq converts unformatted JSON into beautiful JSON according to this Mastodon user. Note that jq is a shell command.
How to remap function keys in Vim for efficient HTML editing?
This blog post explains the basics of key remapping in Vim.
For editing HTML files you could add the following to your .vimrc in order to give your function keys F1-F8 a new meaning. You should be able to identify the elements of the remapping syntax easily.
nnoremap <F1> <Esc>i<h1></h1><Esc>o<p></p><Esc>o<h2></h2>
nnoremap <F2> <Esc>i<h2></h2>
nnoremap <F3> <Esc>i<h3></h3>
nnoremap <F4> <Esc>i<p></p>
nnoremap <F5> <Esc>i<b></b>
nnoremap <F6> <Esc>i<pre><code><Esc>o</pre></code>
nnoremap <F7> <Esc>i<mark style="background-color: #FF0000; color: white">
nnoremap <F8> <Esc>i<a href="#anchor">jump to <div id="anchor"></a>
Note that your terminal emulator might override these settings with its own shortcuts. Also, better keep it simple with the remappings.
Example .vimrc configuration file for Vim
This file goes into a user's $HOME directory. Note that a blinking cursor in Vim is not necessarily something you need to set up in .vimrc. The cursor behaviour is taken from the terminal you use. For instance, xfce4-terminal has a section in its preferences where you can set a blinking cursor and this will persist when you use Vim.
" this is a comment, use exactly ONE double quote for that
" comments work only at the beginning of a line in .vimrc
" disable compatibility with vi and enable Vim's full features
set nocompatible
" prevent that right-clicks drop you into VISUAL mode
" NOTE: it is intended that there is nothing behind the equals sign
set mouse=
" convert tabs into 4 spaces
set autoindent expandtab tabstop=4 shiftwidth=4 softtabstop=4
" the following should enable syntax highlighting
syntax on
" show line numbers
set number
" show line numbers, but relative to the cursor position
set relativenumber
" show a horizontal and/or a vertical line to identify your cursor position
set cursorline
set cursorcolumn
" highlight search results whithin a file and/or do that in an incremental way
set hlsearch
set incsearch
" consider the following to ignore case when searching
" set ic
" indicate when you entered INSERT or VISUAL mode
set showmode
" show corresponding part of a parentheses, brackets or braces when moving the cursor over one
set showmatch
set history=1000
" path expansion
set wildmenu
set wildmode=list:longest
set wildignore=*.docx,*.jpg,*.gif,*.pyc,*.exe,*.flv,*.img,*.xlsx
" status line tweaks
set statusline=
set statusline+=\ %F\ %M\ %Y\ %R
set statusline+=%=
set statusline+=\ ascii:\ %b\ hex:\ 0x%B\ row:\ %l\ col:\ %c\ percent:\%p%%
set laststatus=2
vim.org and Vim's internal :help section is full with ideas what you could add to .vimrc.