Vim survival guide for Linux admins
Vim is the default editor on most Linux servers. You don’t need to love it, but you need to be able to open a file, make changes, and save them. This guide covers the minimum to be productive.
Opening a file
vim /etc/nginx/nginx.conf
You’re now in Normal mode. You can’t type yet. This is where most people panic.
The three modes
- Normal mode — navigate, delete, copy. This is where you start.
- Insert mode — actually type text. Press
ito enter. - Command mode — save, quit, search. Press
:to enter.
Press Esc to get back to Normal mode from anywhere. When in doubt, press Esc.
Getting into Insert mode
i— insert before cursora— insert after cursoro— open new line below and insertO— open new line above and insert
i is all you need to start. Type your changes.
Saving and quitting
From Normal mode:
:w— save (write):q— quit:wq— save and quit:q!— quit without saving (discard changes)ZZ— save and quit (shortcut for:wq)
If you can’t quit, press Esc first, then type :q!.
Basic navigation
hjkl— left, down, up, right (or just use arrow keys)w— jump to next wordb— jump to previous word0— beginning of line$— end of linegg— beginning of fileG— end of file:42— go to line 42
Searching
/pattern— search forward for “pattern”?pattern— search backwardn— next matchN— previous match
Search and replace:
:%s/old/new/g
%— whole files— substituteg— all occurrences on each line (not just the first)
To confirm each replacement:
:%s/old/new/gc
Deleting
x— delete character under cursordd— delete entire linedw— delete wordd$— delete to end of line3dd— delete 3 lines
Copying and pasting
yy— copy (yank) current line3yy— copy 3 linesyw— copy wordp— paste after cursorP— paste before cursor
Undo and redo
u— undoCtrl+r— redo
Visual mode (selecting text)
v— character selectionV— line selectionCtrl+v— block selection
Select text, then d to delete, y to copy, or p to replace.
Practical scenarios
Edit a config file:
vim /etc/nginx/sites-available/mysite
- Find the line:
/server_name - Press
ito enter insert mode - Make your changes
- Press
Esc - Save and quit:
:wq
Comment out a block:
Ctrl+vfor block selection- Select the lines with
j/k Ito insert at beginning- Type
# - Press
Esc— the#appears on all selected lines
Delete everything and start fresh:
ggto go to the beginningdGto delete to the end of the file
Emergency exit
If you’re stuck in vim and can’t figure out how to exit:
- Press
Escmultiple times - Type
:q! - Press
Enter
This discards all changes and exits. Use it when you’re lost.
A simple .vimrc
Create ~/.vimrc for better defaults:
set number " show line numbers
set relativenumber " relative line numbers
set tabstop=4 " tab width
set shiftwidth=4 " indent width
set expandtab " spaces instead of tabs
set autoindent " auto indent new lines
set hlsearch " highlight search results
set ignorecase " case-insensitive search
set smartcase " ...unless search has capitals
syntax on " syntax highlighting
Common mistakes
Editing in insert mode when you should be in normal mode. Get in, make the change, get out. Don’t stay in insert mode for navigation.
Not saving before quitting. :q! discards everything. Use :wq or :w first.
Using arrow keys in a broken terminal. If arrow keys insert characters instead of moving, you’re probably in a weird mode. Press Esc, then use hjkl.
Remarks
Vim has a steep learning curve but the minimum viable knowledge is small: i to insert, Esc to normal, :wq to save and quit, dd to delete a line, u to undo. Learn those five things and you can survive any Linux server. Everything else is convenience that you pick up over time.
Related Posts
Linux memory management: free, vmstat, and /proc
Understanding how Linux uses memory. Reading free correctly, using vmstat, and what /proc/meminfo actually tells you.
tmux: terminal multiplexing made simple
Using tmux to manage terminal sessions. Panes, windows, and why your SSH sessions should survive disconnects.
Managing SSL certificates with Let's Encrypt
Getting free SSL certificates with Let's Encrypt and certbot. Installation, renewal, and troubleshooting.