Sooner or later you’ll SSH into a server where Vim (or plain vi) is the only editor — or a command like visudo, crontab -e or git commit drops you into it. Vim is powerful, but its modes confuse newcomers. This guide gives you everything you need to edit config files confidently.

The one thing to understand: modes

  • Normal mode — the default. Keys are commands (move, delete, copy). Typing letters here does not insert text.
  • Insert mode — type text like in any editor. Enter it with i; leave it with Esc.
  • Command-line mode — press : in normal mode to type commands like save and quit.
  • Visual mode — v to select text.

Lost? Press Esc (twice if unsure). You’re back in normal mode.

Open, save, quit

vim /etc/nginx/nginx.conf
sudo vim /etc/ssh/sshd_config      # files owned by root
Command Action
:w Save
:q Quit (fails if there are unsaved changes)
:wq or :x or ZZ Save and quit
:q! Quit without saving
:w !sudo tee % Save a root-owned file you opened without sudo

The famous “how do I exit Vim”: Esc, then :q! + Enter to discard, or :wq + Enter to save.

Entering insert mode

Key Inserts
i Before the cursor
a After the cursor
I / A At the start / end of the line
o / O On a new line below / above

Moving around (normal mode)

Key Moves
h j k l Left, down, up, right (arrow keys work too)
w / b Next / previous word
0 / $ Start / end of line
gg / G First / last line
42G or :42 Line 42
Ctrl+d / Ctrl+u Half a page down / up
% Matching bracket

Error messages often give a line number — nginx: [emerg] … in /etc/nginx/sites-enabled/site:37 → open the file and type :37.

Editing (normal mode)

Key Action
x Delete character
dd Delete (cut) line
5dd Delete 5 lines
dw Delete to end of word
D Delete to end of line
yy Copy (yank) line
p / P Paste below / above
u Undo
Ctrl+r Redo
. Repeat last change
r + char Replace one character
>> / << Indent / unindent line
Command Action
/text Search forward
?text Search backward
n / N Next / previous match
* Search for the word under the cursor

Search and replace

:s/old/new/          " first match on the current line
:s/old/new/g         " all matches on the current line
:%s/old/new/g        " whole file
:%s/old/new/gc       " whole file, confirm each
:10,20s/old/new/g    " lines 10–20

Same syntax as sed.

Visual mode: select, then act

  • v — select characters, V — whole lines, Ctrl+v — a block (columns).
  • Move to extend the selection, then d (cut), y (copy), > (indent).

Comment out several lines: Ctrl+v, select down with j, press I, type #, press Esc.

Pasting from your clipboard without a mess

Pasting into a terminal Vim with auto-indent enabled can produce staircase indentation. Before pasting:

:set paste

Paste (switch to insert mode first), then :set nopaste. Recent Vim versions handle this automatically in most terminals (“bracketed paste”).

Useful extras

:set number          " line numbers
:set nonumber
:syntax on           " syntax highlighting
:e other.conf        " open another file
:sp file / :vsp file " split windows; Ctrl+w w to switch
:!nginx -t           " run a shell command without leaving Vim

A minimal ~/.vimrc

syntax on
set number
set expandtab tabstop=4 shiftwidth=4
set autoindent
set hlsearch incsearch
set ignorecase smartcase

On servers you sudo vim, root uses /root/.vimrc.

Swap file warnings

E325: ATTENTION Found a swap file means Vim crashed earlier, or someone else is editing the file right now. If you’re sure nobody else is: choose (R)ecover to restore unsaved changes, or (D)elete it. Swap files live next to the file as .filename.swp.

Prefer something simpler?

Nano shows its shortcuts on screen and needs no modes — see the nano editor guide. To make Nano the default for visudo, crontab -e and git: export EDITOR=nano in your ~/.bashrc, or sudo update-alternatives --config editor.

Practice tip: run vimtutor — a 30-minute interactive tutorial installed with Vim.

  • Vim
  • Linux
  • Command Line