Skip to content
Français
Account: not signed in, sync available My learning
Select theme
Français

Vim for system administration (certification)

By updated

Improve this page

You regularly edit configuration files on Linux servers, /etc/ssh/sshd_config, /etc/fstab, /etc/nginx/nginx.conf, and you want to gain speed and precision. Vim (Vi IMproved) is found on almost every server, at least as a reduced version started by the vi command, and it is one of the skills expected for the LFCS and RHCSA certifications. This guide takes you from opening a file to mass substitution, with a minimal .vimrc suited to administration. The advanced features (splits, macros, plugins) are covered in the advanced Vim guide.

What you will learn

  • Understand Vim's 3 modes, normal, insert, command, and avoid the classic mistakes
  • Open, edit, save and quit a file cleanly
  • Move efficiently with hjkl, words, lines, start and end of file
  • Edit with operators and motions (dd, dw, yy, ciw, di"), Vim's "language"
  • Search and substitute in a file or over a range of lines
  • Set up a minimal .vimrc suited to administration

In which context?

On a Linux server, you often have no choice: the editor available over SSH is Vim (or vi, which is actually Vim on most modern distributions). Knowing how to use it efficiently avoids costly mistakes: a badly saved sshd_config file can lock you out of the server.

Real situationUseful Vim skill
Fix a directive in /etc/nginx/nginx.conf/pattern navigation, ciw editing
Comment out 10 lines of /etc/fstab for a testVisual block mode Ctrl+v
Replace every occurrence of a port in a file:%s/8080/9090/g
Add an option at the end of each configuration lineSubstitution with $
Edit a CSV list copied into a temporary fileText objects ci", di,

Prerequisites: knowing the basics of vi (the 3 modes, hjkl, :w, :q) helps a lot. If these notions mean nothing to you, start with the vi guide: 15 minutes are enough to acquire these reflexes.

Vim's 3 modes

The first thing that puzzles people with Vim is that it works in modes. In a classic editor, you type and the text is added. In Vim, keys mean different things depending on the active mode. Understanding this mechanism is the key to never feeling lost again.

ModeHow to enter itWhat it lets you do
NormalEsc (default mode at start)Move, delete, copy, paste, repeat
Inserti, a, o, I, A, O from normalType text as in a classic editor
Command: from normalSave, quit, substitute, open another file

Enter insert mode

Several keys switch to insert mode, each with a useful nuance:

KeyWhere does it insert?
iBefore the cursor
aAfter the cursor
IAt the start of the current line
AAt the end of the current line
oOn a new line below
OOn a new line above

Picking the right key saves time. If you want to add something at the end of a line, no need to go all the way with the arrows and then type a: A puts you there directly in insert mode.

Open, save, quit

This is the first thing to master: do not lose your work and do not get stuck in the editor.

CommandWhat it does
vim fileOpens (or creates) the file
:wSaves (write)
:qQuits, refuses if changes are unsaved
:wq or ZZSaves and quits
:q!Quits without saving (discards the changes)
:w fileSaves under another name
:e fileOpens another file in the current session

Move around a file

In normal mode, keys become movement commands. Forget the keyboard arrows: hjkl are faster because your hands stay on the home row.

ShortcutMovement
h / j / k / lLeft / Down / Up / Right
wStart of the next word
bStart of the previous word
eEnd of the current word
0Start of the line
^First non-blank character of the line
$End of the line
ggFirst line of the file
GLast line of the file
25G or :25Go to line 25
Ctrl+d / Ctrl+uHalf a page down / up
%Matching parenthesis/brace

Edit: operators and motions

Vim works like a language with verbs (operators) and objects (motions). Understanding this logic avoids memorising dozens of separate shortcuts.

The verbs (operators)

An operator does nothing on its own: it waits for a complement that tells it what to act on. Remember these four first, they cover most of the editing of a configuration file. The most useful day to day is c (change), because it chains deletion and switching to insert mode in one keystroke.

OperatorVerbWhat it does
ddeleteDeletes
cchangeDeletes and switches to insert mode
yyankCopies into Vim's clipboard
> / <indentShifts right / left

Combine verb and motion

Vim's magic: everything composes. A few examples useful day to day:

CommandReadingResult
dwdelete wordDeletes up to the next word
d$delete to end of lineDeletes everything to the end
dddelete lineDeletes the whole line
5dddelete 5 linesDeletes 5 lines
yyyank lineCopies the line
cwchange wordDeletes the word and switches to insert mode
c$change to end of lineReplaces the end of the line

Copy, paste, undo

CommandWhat it does
yyCopy the current line
pPaste after the cursor (or after the line)
PPaste before the cursor
xDelete the character under the cursor
r{c}Replace the character with {c} without entering insert mode
uUndo the last change
Ctrl+rRedo (the opposite of u)
.Repeat the last change

Text objects: act on logical structures

Text objects make Vim particularly powerful on configuration files. Instead of aiming at a position, you aim at a structure: the content between quotes, between parentheses, a whole word.

Syntax: operator + i or a + delimiter.

  • i = inner (content without the delimiters)
  • a = around (content + delimiters)

Take a line of /etc/nginx/nginx.conf:

listen 80 default_server;

Cursor on 80, you type:

CommandResult
ciwDeletes 80 and switches to insert mode, type 443, Esc → listen 443 default_server;
dawDeletes 80 (word + space)
C (capital)Deletes from the position to the end of the line and switches to insert mode

For a JSON file { "port": "8080", "host": "localhost" }, cursor on 8080:

CommandResult
ci"Replaces the content between quotes ("8080" → "" + insert mode)
di"Deletes only 8080
da"Deletes "8080" (quotes included)

The most useful delimiters:

DelimiterTarget
", ', `Content between quotes
( or )Content between parentheses
{ or }Content between braces
[ or ]Content between brackets
wThe word under the cursor
pThe current paragraph

Visual mode: select before acting

Visual mode adds a reassuring selection step: you see what will be affected before acting.

ShortcutKind of selection
vCharacter by character
VWhole line
Ctrl+vRectangular block (columns)

Once selected, press an action: y (copy), d (cut), c (replace), > (indent), < (unindent), Esc (cancel the selection).

Practical example: comment out 5 configuration lines

Scenario: disable 5 lines in /etc/fstab by adding # at the start of each.

  1. Put the cursor on the first line to comment out.

  2. Press Ctrl+v to enter visual block mode. -- VISUAL BLOCK -- appears at the bottom.

  3. Move down with 4j to select 5 lines (current line + 4 below).

  4. Press I (capital). Vim switches to insert mode at the start of the first line.

  5. Type # (hash + space).

  6. Press Esc. The # is applied automatically at the start of the 5 lines.

To uncomment: Ctrl+v, select the # column over the 5 lines with 4j then l, press d.

Search and substitute

In a 200-line configuration file, finding a parameter or renaming a value everywhere without mistakes is essential.

Search is your fastest way to reach a directive in a 200-line file. Two reflexes to acquire: /pattern then n to go through the occurrences downwards, and * when the cursor is already on the word you are looking for, which saves typing it again.

CommandWhat it does
/patternSearches pattern downwards
?patternSearches pattern upwards
nNext occurrence
NPrevious occurrence
*Searches the word under the cursor (downwards)
#Searches the word under the cursor (upwards)

Substitute

The syntax is close to sed:

:[range]s/old/new/[flags]
CommandWhat it does
:s/foo/bar/Replaces the first occurrence on the current line
:s/foo/bar/gReplaces all occurrences on the line
:%s/foo/bar/gReplaces all occurrences in the whole file
:%s/foo/bar/gcSame with confirmation for each occurrence
:5,20s/foo/bar/gReplaces between lines 5 and 20 only

Example: rename a port in a configuration file

You have a file where listen 8080 appears 4 times. To switch everything to 9090 with confirmation:

:%s/8080/9090/gc

Vim highlights each occurrence and asks you y (yes), n (no), a (all), q (quit), l (this one then quit).

A minimal .vimrc for administration

The ~/.vimrc file is read when Vim starts. Here is a minimal configuration, enough for most administration tasks and in line with what certifications expect:

" === Appearance ===
syntax on " Syntax highlighting
set number " Line numbers
set showmatch " Highlights the matching parenthesis
" === Search ===
set incsearch " Results as you type
set hlsearch " Highlights all occurrences
set ignorecase " Case-insensitive
set smartcase " Unless the search has a capital
" === Indentation ===
set tabstop=4
set shiftwidth=4
set expandtab " Tab inserts spaces (not \t)
set autoindent " Keeps the indentation
" === Comfort ===
set encoding=utf-8
set scrolloff=8 " Keeps 8 lines visible around the cursor

Create (or complete) ~/.vimrc:

Terminal window
vim ~/.vimrc

After changing it, apply it without restarting:

:source ~/.vimrc

Built-in help

Vim has very complete built-in documentation. Get into the habit of reading it directly.

CommandWhat it shows
:helpHelp home page
:help motionEverything about motions
:help text-objectsEverything about text objects
:help :substituteAbout substitution
:helpgrep patternSearches the whole documentation

In the help: Ctrl+] follows a link, Ctrl+o goes back, :q closes.

Troubleshooting

Most blockages with Vim come from a misidentified mode or from a file opened without the necessary permissions. Before looking further, press Esc to go back to normal mode: that alone solves half the situations in the table below.

SymptomLikely causeSolution
Strange characters as you typeYou left insert mode without knowing itEsc then i to resume
E45: 'readonly' option is set on :wSystem file opened without sudo:w !sudo tee % then :q!
Pasted text is indented in a staircaseTerminal without "bracketed paste": Vim applies auto-indentation to each pasted line:set paste, paste, :set nopaste
No syntax highlightingsyntax on missing from .vimrcAdd syntax on
System copy-paste does not workVim without +clipboardsudo apt install vim-gtk3 on Debian/Ubuntu
Odd characters in the terminalWrong encodingset encoding=utf-8 in .vimrc
"The whole screen is selected"You are in visual modeEsc
Vim refuses to quitUnsaved changes:w to save or :q! to discard

Key points

  • Vim has 3 modes, normal, insert, command. When in doubt, Esc always takes you back to normal mode.
  • Operators + motions form a language: d + w = delete word. Understand the logic and you will no longer have to memorise each shortcut separately.
  • Text objects (ciw, ci", di() are the most useful on configuration files: they act on logical structures.
  • The . command repeats the last change: combined with n after a search, it lets you fix each occurrence selectively.
  • To substitute everywhere: :%s/old/new/gc, always add the % for the whole file and the c to confirm before each risky replacement.
  • To edit a system file, open it with sudo vim. If you forgot: :w !sudo tee % saves the day.
  • A minimal .vimrc (line numbers, highlighting, indentation, search) is enough for administration; the full configuration with plugins and macros belongs to the advanced Vim guide.
  • Practise with vimtutor: 30 minutes that pay off in the timed LFCS and RHCSA exams.

Knowledge check

Check that the essentials of this guide stuck. The questions cover only what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

6 questions
6 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer

Is this project useful to you?

The guides, courses, quizzes and labs stay free. If you use them regularly, you can contribute to their upkeep and development.

Become a supporting member (new tab) One-time support (new tab) Learn more

Follow the project on LinkedIn