Linux6 min read

Vim survival guide for Linux admins

linuxvimeditorcli

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 i to 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 cursor
  • a — insert after cursor
  • o — open new line below and insert
  • O — 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

  • h j k l — left, down, up, right (or just use arrow keys)
  • w — jump to next word
  • b — jump to previous word
  • 0 — beginning of line
  • $ — end of line
  • gg — beginning of file
  • G — end of file
  • :42 — go to line 42

Searching

  • /pattern — search forward for “pattern”
  • ?pattern — search backward
  • n — next match
  • N — previous match

Search and replace:

:%s/old/new/g
  • % — whole file
  • s — substitute
  • g — all occurrences on each line (not just the first)

To confirm each replacement:

:%s/old/new/gc

Deleting

  • x — delete character under cursor
  • dd — delete entire line
  • dw — delete word
  • d$ — delete to end of line
  • 3dd — delete 3 lines

Copying and pasting

  • yy — copy (yank) current line
  • 3yy — copy 3 lines
  • yw — copy word
  • p — paste after cursor
  • P — paste before cursor

Undo and redo

  • u — undo
  • Ctrl+r — redo

Visual mode (selecting text)

  • v — character selection
  • V — line selection
  • Ctrl+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
  1. Find the line: /server_name
  2. Press i to enter insert mode
  3. Make your changes
  4. Press Esc
  5. Save and quit: :wq

Comment out a block:

  1. Ctrl+v for block selection
  2. Select the lines with j/k
  3. I to insert at beginning
  4. Type #
  5. Press Esc — the # appears on all selected lines

Delete everything and start fresh:

  1. gg to go to the beginning
  2. dG to delete to the end of the file

Emergency exit

If you’re stuck in vim and can’t figure out how to exit:

  1. Press Esc multiple times
  2. Type :q!
  3. 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