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
.vimrcsuited 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 situation | Useful Vim skill |
|---|---|
Fix a directive in /etc/nginx/nginx.conf | /pattern navigation, ciw editing |
Comment out 10 lines of /etc/fstab for a test | Visual 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 line | Substitution with $ |
| Edit a CSV list copied into a temporary file | Text 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.
| Mode | How to enter it | What it lets you do |
|---|---|---|
| Normal | Esc (default mode at start) | Move, delete, copy, paste, repeat |
| Insert | i, a, o, I, A, O from normal | Type text as in a classic editor |
| Command | : from normal | Save, quit, substitute, open another file |
Enter insert mode
Several keys switch to insert mode, each with a useful nuance:
| Key | Where does it insert? |
|---|---|
i | Before the cursor |
a | After the cursor |
I | At the start of the current line |
A | At the end of the current line |
o | On a new line below |
O | On 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.
| Command | What it does |
|---|---|
vim file | Opens (or creates) the file |
:w | Saves (write) |
:q | Quits, refuses if changes are unsaved |
:wq or ZZ | Saves and quits |
:q! | Quits without saving (discards the changes) |
:w file | Saves under another name |
:e file | Opens 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.
| Shortcut | Movement |
|---|---|
h / j / k / l | Left / Down / Up / Right |
w | Start of the next word |
b | Start of the previous word |
e | End of the current word |
0 | Start of the line |
^ | First non-blank character of the line |
$ | End of the line |
gg | First line of the file |
G | Last line of the file |
25G or :25 | Go to line 25 |
Ctrl+d / Ctrl+u | Half 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.
| Operator | Verb | What it does |
|---|---|---|
d | delete | Deletes |
c | change | Deletes and switches to insert mode |
y | yank | Copies into Vim's clipboard |
> / < | indent | Shifts right / left |
Combine verb and motion
Vim's magic: everything composes. A few examples useful day to day:
| Command | Reading | Result |
|---|---|---|
dw | delete word | Deletes up to the next word |
d$ | delete to end of line | Deletes everything to the end |
dd | delete line | Deletes the whole line |
5dd | delete 5 lines | Deletes 5 lines |
yy | yank line | Copies the line |
cw | change word | Deletes the word and switches to insert mode |
c$ | change to end of line | Replaces the end of the line |
Copy, paste, undo
| Command | What it does |
|---|---|
yy | Copy the current line |
p | Paste after the cursor (or after the line) |
P | Paste before the cursor |
x | Delete the character under the cursor |
r{c} | Replace the character with {c} without entering insert mode |
u | Undo the last change |
Ctrl+r | Redo (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:
| Command | Result |
|---|---|
ciw | Deletes 80 and switches to insert mode, type 443, Esc → listen 443 default_server; |
daw | Deletes 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:
| Command | Result |
|---|---|
ci" | Replaces the content between quotes ("8080" → "" + insert mode) |
di" | Deletes only 8080 |
da" | Deletes "8080" (quotes included) |
The most useful delimiters:
| Delimiter | Target |
|---|---|
", ', ` | Content between quotes |
( or ) | Content between parentheses |
{ or } | Content between braces |
[ or ] | Content between brackets |
w | The word under the cursor |
p | The current paragraph |
Visual mode: select before acting
Visual mode adds a reassuring selection step: you see what will be affected before acting.
| Shortcut | Kind of selection |
|---|---|
v | Character by character |
V | Whole line |
Ctrl+v | Rectangular 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.
-
Put the cursor on the first line to comment out.
-
Press
Ctrl+vto enter visual block mode.-- VISUAL BLOCK --appears at the bottom. -
Move down with
4jto select 5 lines (current line + 4 below). -
Press
I(capital). Vim switches to insert mode at the start of the first line. -
Type
#(hash + space). -
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
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.
| Command | What it does |
|---|---|
/pattern | Searches pattern downwards |
?pattern | Searches pattern upwards |
n | Next occurrence |
N | Previous 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]| Command | What it does |
|---|---|
:s/foo/bar/ | Replaces the first occurrence on the current line |
:s/foo/bar/g | Replaces all occurrences on the line |
:%s/foo/bar/g | Replaces all occurrences in the whole file |
:%s/foo/bar/gc | Same with confirmation for each occurrence |
:5,20s/foo/bar/g | Replaces 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/gcVim 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 highlightingset number " Line numbersset showmatch " Highlights the matching parenthesis
" === Search ===set incsearch " Results as you typeset hlsearch " Highlights all occurrencesset ignorecase " Case-insensitiveset smartcase " Unless the search has a capital
" === Indentation ===set tabstop=4set shiftwidth=4set expandtab " Tab inserts spaces (not \t)set autoindent " Keeps the indentation
" === Comfort ===set encoding=utf-8set scrolloff=8 " Keeps 8 lines visible around the cursorCreate (or complete) ~/.vimrc:
vim ~/.vimrcAfter changing it, apply it without restarting:
:source ~/.vimrcBuilt-in help
Vim has very complete built-in documentation. Get into the habit of reading it directly.
| Command | What it shows |
|---|---|
:help | Help home page |
:help motion | Everything about motions |
:help text-objects | Everything about text objects |
:help :substitute | About substitution |
:helpgrep pattern | Searches 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.
| Symptom | Likely cause | Solution |
|---|---|---|
| Strange characters as you type | You left insert mode without knowing it | Esc then i to resume |
E45: 'readonly' option is set on :w | System file opened without sudo | :w !sudo tee % then :q! |
| Pasted text is indented in a staircase | Terminal without "bracketed paste": Vim applies auto-indentation to each pasted line | :set paste, paste, :set nopaste |
| No syntax highlighting | syntax on missing from .vimrc | Add syntax on |
| System copy-paste does not work | Vim without +clipboard | sudo apt install vim-gtk3 on Debian/Ubuntu |
| Odd characters in the terminal | Wrong encoding | set encoding=utf-8 in .vimrc |
| "The whole screen is selected" | You are in visual mode | Esc |
| Vim refuses to quit | Unsaved changes | :w to save or :q! to discard |
Key points
- Vim has 3 modes, normal, insert, command. When in doubt,
Escalways 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 withnafter a search, it lets you fix each occurrence selectively. - To substitute everywhere:
:%s/old/new/gc, always add the%for the whole file and thecto 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
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
Review
(0/0)Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Search and replace in a file
A new full quiz with random questions
Work again only on the questions you missed