Log inSign up

A practical Vim workflow for server configuration files

Open a file with vim filename, press i to start typing, press Esc to return to normal mode, then type :wq and Enter to save and quit (or :q! to leave without saving). Install the full editor with sudo apt install vim on Ubuntu and Debian or sudo dnf install vim-enhanced on Rocky Linux and AlmaLinux.

Topic: Linux command line · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Which Vim package should you install?

Install the full Vim package, because the preinstalled vi on many images is a reduced build without syntax colouring or reliable undo history. Ubuntu and Debian ship vim-tiny; Rocky Linux 9 and AlmaLinux 9 ship vim-minimal.

# Ubuntu 24.04 / 22.04, Debian 12 / 13
sudo apt update && sudo apt install -y vim
apt policy vim
# Rocky Linux 9 / AlmaLinux 9
sudo dnf install -y vim-enhanced
dnf info vim-enhanced | grep -i version
vim --version | head -n 2

Ubuntu 24.04 and Debian 13 carry the 9.1 series, while Ubuntu 22.04, Debian 12 and the EL9 systems carry 8.2 or 9.0 builds. All commands on this page work across those versions.

What are Vim modes and why do they matter?

Vim starts in normal mode, where keys are commands instead of text. That surprises new users, but it is what lets you edit without reaching for the mouse or arrow keys. The modes you need:

  • Normal: move, delete, copy, paste. Press Esc to return here from anywhere.
  • Insert: type text. Enter it with i (before cursor), a (after cursor), o (new line below) or O (new line above).
  • Visual: select text with v (characters), V (lines) or Ctrl+v (block).
  • Command-line: type : for commands such as save, quit or replace.

The bottom line shows -- INSERT -- or -- VISUAL -- when you are not in normal mode.

How do you save and quit?

From normal mode, type one of these and press Enter:

  • :w: save, keep editing.
  • :wq or :x (or ZZ without the colon): save and quit.
  • :q: quit when nothing changed.
  • :q!: quit and throw away changes.
  • :w ~/copy.conf: save a copy under another name.

If you opened a root file without sudo and cannot save, write it through tee instead of losing the work: :w !sudo tee % > /dev/null, then :q!. The cleaner habit is sudoedit, covered below.

How do you move around quickly?

Motions are single keys in normal mode, and most accept a count in front, so 5j moves five lines down.

  • h j k l: left, down, up, right.
  • w and b: next and previous word; e to the end of a word.
  • 0, ^ and $: start of line, first non-blank, end of line.
  • gg and G: top and bottom of the file; 42G or :42 jumps to line 42.
  • Ctrl+d and Ctrl+u: half a page down and up.
  • %: jump to the matching bracket, very handy in nginx or JSON files.

To open a file directly at a line reported in an error, run vim +42 /etc/nginx/nginx.conf.

How do you delete, copy, paste and undo?

Vim combines an operator with a motion: d deletes, y copies (yank), c changes, and the motion says how far. So dw deletes a word and y$ copies to the end of the line.

  • dd: delete (cut) the line; 3dd deletes three lines.
  • yy: copy the line; p pastes below, P above.
  • x: delete one character.
  • u: undo; Ctrl+r: redo.
  • .: repeat the last change, which turns many repetitive edits into one key.

To comment out several lines in a shell config, press Ctrl+v, move down to select the first column, press I, type # and press Esc; the character appears on every selected line.

How do you search and replace?

Type /word and Enter to search forward, ?word to search back, then n and N for the next and previous match. Pressing * on a word searches for that word directly.

:%s/old.example.net/app.example.net/g
:%s/^#\?Port 22$/Port 2222/c
:10,30s/http:/https:/g
:noh

The % means the whole file, g replaces every hit on a line, and c asks before each change. A line range such as 10,30 limits the edit. :noh clears the search highlight. If you change the SSH port, also open it in your firewall first, as explained in firewall rules.

How do you edit system files without running Vim as root?

Use sudoedit. It copies the target to a temporary file, opens it in Vim as your normal user with your own vimrc, and writes the result back as root when you quit. Plugins and shell escapes never get root rights that way.

echo 'export SUDO_EDITOR=vim EDITOR=vim VISUAL=vim' >> ~/.bashrc
source ~/.bashrc
sudoedit /etc/ssh/sshd_config
sudo sshd -t && echo "config syntax ok"

Reload the service only after the syntax test passes (ssh on Ubuntu and Debian, sshd on Rocky and Alma). Keep the current SSH session open until a fresh login works; see SSH keys and hardening.

What belongs in a small server vimrc?

A short ~/.vimrc makes config work calmer without plugins:

syntax on
filetype plugin indent on
set number
set ruler
set incsearch hlsearch ignorecase smartcase
set expandtab tabstop=4 shiftwidth=4
set undofile undodir=~/.vim/undo
set mouse=
autocmd FileType yaml setlocal tabstop=2 shiftwidth=2

Create the undo folder with mkdir -p ~/.vim/undo so undo history survives after you close a file. set mouse= keeps your terminal copy and paste working normally over SSH. Before pasting a block of YAML, run :set paste to stop auto-indent from shifting lines, and :set nopaste afterwards.

What do common Vim warnings mean?

  • "Found a swap file": another session is editing the file or Vim crashed earlier. Press R to recover, check the result, then delete the .swp file. If someone else is editing, quit and wait.
  • E45 readonly option is set: you opened a file you cannot write. Use :w !sudo tee % once, then switch to sudoedit next time.
  • Keys type letters like A B C D: the terminal type is wrong. Check echo $TERM; xterm-256color works on most clients.
  • Stuck and nothing works: press Esc twice, then type :q! and Enter.

For a guided practice session run vimtutor, which installs with the full Vim package.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
S1-2more than enough for terminal editing work1 vCPU, 2 GB RAM, 55 GB SSD$6.00/mo$72.00 for 12 months

Questions about Edit files with Vim

Is vi the same as Vim?

On these systems vi usually launches a small Vim build in compatible mode. Installing the full package gives you syntax colours, persistent undo and the complete help system under :help.

How do I show line numbers only for one session?

Type :set number while editing and :set nonumber to hide them again. :set relativenumber shows distances, which pairs well with counts like 5j.

Can Vim compare two config files?

Yes. vimdiff old.conf new.conf shows them side by side with changes highlighted. Use ]c and [c to jump between differences.

Should I learn Vim or nano first?

nano is quicker to start with. Vim pays off once you edit many files or repeat the same changes often. Both are covered in our guides, starting with nano.

How do I open Vim help on a topic?

Type :help followed by the subject, for example :help :substitute. Close the help window with :q.