Vim Editor Guide: How to Use vim for Beginners

Vim is installed on virtually every Linux server. You will eventually find yourself needing to edit a config file remotely with no other editor available. Learning vim is not optional for sysadmins — it is essential. The good news: you only need to know about 20 commands to be productive. This guide gets you there.

Opening and Closing Files

vim filename.txt        # Open (or create) a file
vim +42 filename.txt    # Open at line 42
vim -R filename.txt     # Open read-only
vim /etc/nginx/nginx.conf

The Three Modes

Vim is modal — it behaves differently depending on which mode you are in:

  • Normal mode — the default. Navigate and run commands. Press Esc to return here.
  • Insert mode — type text. Enter with i, a, o.
  • Command mode — run ex commands. Enter with : from Normal mode.

The most important thing to know: press Esc when confused. It always takes you back to Normal mode.

Entering Insert Mode

i    # Insert before cursor
a    # Append after cursor
I    # Insert at beginning of line
A    # Append at end of line
o    # Open new line below
O    # Open new line above

Saving and Quitting

From Normal mode, press : to enter Command mode:

:w           # Save (write)
:wq          # Save and quit
:q           # Quit (fails if unsaved changes)
:q!          # Quit and discard changes (force)
:w !sudo tee %   # Save as root when you forgot sudo

The last one is a lifesaver when you edit a system file without sudo.

Navigation (Normal Mode)

h j k l      # Left, Down, Up, Right (or arrow keys)
w            # Jump forward one word
b            # Jump backward one word
0            # Go to start of line
$            # Go to end of line
gg           # Go to first line of file
G            # Go to last line of file
42G          # Go to line 42
Ctrl+f       # Page down
Ctrl+b       # Page up

Searching

/pattern     # Search forward
?pattern     # Search backward
n            # Next match
N            # Previous match
*            # Search for word under cursor

Editing Commands (Normal Mode)

x            # Delete character under cursor
dd           # Delete (cut) entire line
5dd          # Delete 5 lines
yy           # Yank (copy) line
5yy          # Copy 5 lines
p            # Paste after cursor
P            # Paste before cursor
u            # Undo
Ctrl+r       # Redo
.            # Repeat last change

Search and Replace

:s/old/new/          # Replace first occurrence on current line
:s/old/new/g         # Replace all on current line
:%s/old/new/g        # Replace all in file
:%s/old/new/gc       # Replace all with confirmation
:%s/foo/bar/gi       # Case-insensitive replace all

Working with Multiple Files

vim file1 file2      # Open multiple files
:n                   # Next file
:prev                # Previous file
:e /etc/hosts        # Open another file
:sp filename         # Split horizontally
:vsp filename        # Split vertically
Ctrl+w w             # Switch between splits
Ctrl+w q             # Close current split

Visual Mode — Select Text

v            # Visual mode (character selection)
V            # Visual line mode
Ctrl+v       # Visual block mode (column selection)

After selecting text: d to delete, y to copy, > to indent, < to unindent.

Useful Settings

Set these in Command mode for the current session, or add to ~/.vimrc to make them permanent:

:set number          # Show line numbers
:set relativenumber  # Relative line numbers
:set autoindent      # Auto-indent new lines
:set tabstop=4       # Tab width = 4 spaces
:set expandtab       # Use spaces instead of tabs
:set hlsearch        # Highlight search results
:set ignorecase      # Case-insensitive search
:set syntax on       # Syntax highlighting

A Minimal .vimrc for Sysadmins

cat > ~/.vimrc << EOF
set number
set autoindent
set tabstop=2
set expandtab
set hlsearch
set ignorecase
set smartcase
syntax on
EOF

Quick Reference Card

  • Open: vim file | Save: :w | Quit: :q!
  • Insert: i | Back to Normal: Esc
  • Search: /term | Replace all: :%s/old/new/g
  • Copy line: yy | Paste: p | Delete line: dd | Undo: u
  • Go to line: 42G | Top: gg | Bottom: G

Summary

Vim has a reputation for being hard to learn, but the basics you need for sysadmin work — open, navigate, edit, save, quit — take about 30 minutes of practice. Spend an hour with vimtutor (run it in your terminal) and you will be comfortable with the fundamentals. From there, vim rewards every hour you invest in it.