Skip to content

Beginner · Linux and the shell · topic 6 of 57 · 30 min

Editing files with vim

Explanation

vim is the text editor found on almost every Linux server. It has modes: Normal mode for moving and commands, Insert mode for typing, and the : command line for saving and quitting. Learn i, Esc, :wq and :q! first, then a few editing keys.

Why vim?

Admins edit config files all the time. Servers usually have no graphical editor, but vi or vim is almost always there, including in rescue mode when nothing else works. On RHEL, the vim-minimal package gives the vi command, and vim-enhanced gives full vim with colours. nano may also be installed, but you cannot count on it, so learn the basics of vim.

Modes: the one idea to understand

Most editors type a letter when you press a key. vim does not, at first. It starts in Normal mode, where keys are commands: x deletes a character, dd deletes a line. To type text you switch to Insert mode. To save or quit you type a : command. Esc always takes you back to Normal mode.

Think of it like this: vim modes are like a car's gears. In Normal mode you are parked and can look around and rearrange things. Press i and you shift into drive: now every key moves text forward. Esc is the brake that brings you back to park. Pressing the accelerator in the wrong gear surprises you; so does typing in the wrong mode.
KeysModeWhat it does
i / a / oNormal → InsertInsert before the cursor / after it / on a new line below
Escany → NormalBack to Normal mode
:wcommandWrite (save)
:qcommandQuit (only if saved)
:wq or :xcommandSave and quit
:q!commandQuit and THROW AWAY changes
h j k l (or arrows)NormalLeft, down, up, right
gg / GNormalGo to the first / last line
:40commandGo to line 40
dd / yy / pNormalDelete (cut) a line / copy (yank) a line / paste
x / u / Ctrl+RNormalDelete a character / undo / redo
/word then nNormalSearch forward, then next match
:%s/old/new/gcommandReplace every "old" with "new" in the file

A first real edit

bash
$ sudo vim /etc/hosts
# inside vim:
#   G        go to the last line
#   o        open a new line below and enter Insert mode
#   type:    192.168.122.20  db01.example.com db01
#   Esc      back to Normal mode
#   :wq      save and quit
$ grep db01 /etc/hosts
192.168.122.20  db01.example.com db01

Handy settings

Type :set number to show line numbers, useful when an error says "line 40". :set paste before pasting stops vim from adding extra indentation (in newer vim this is often automatic). In YAML files (Ansible, later in this course) indentation must be spaces, never tabs; :set expandtab tabstop=2 shiftwidth=2 makes Tab insert two spaces. You can put lines like these in ~/.vimrc to keep them.

Common mistakes and fixes

  • Typing text in Normal mode, so letters run as commands and the file gets scrambled. Press Esc, then u several times to undo.
  • "E45: readonly option is set": you opened a root-owned file without sudo. Quit with :q!, then run sudo vim file. (Or, in a pinch, :w !sudo tee % saves through sudo.)
  • "E37: No write since last change": you tried :q with unsaved changes. Use :wq to save or :q! to discard.
  • A message about a swap file (.file.swp) when you open a file: vim crashed earlier or someone else is editing it. Read the message; choose (R)ecover or (D)elete only when you are sure nobody else is editing.
Tip: Run vimtutor once. It is a 30-minute lesson built into vim, and it is the fastest way to stop fearing the editor.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Editing files with vim · 1/7

vim

vim is the editor that is on almost every Linux server, even in rescue mode.

Prefer a video by a teacher? Find free videos on “vim basics for beginners linux modes save quit” on YouTube ↗

In real life: A 3 am fix in rescue mode

A hosting company in Chennai has a server that will not boot after someone added a wrong line to /etc/fstab. The on-call engineer boots into emergency mode. There is no nano and no graphical editor, only vi. He opens /etc/fstab, goes to the bad line, presses dd to delete it, saves with :wq, and reboots. The site is back in ten minutes.

bash
# vi /etc/fstab
#   /data      (search for the bad line)
#   dd         (delete it)
#   :wq        (save and quit)
# systemctl daemon-reload
# mount -a

Practice (0/3 done)

Task 1 · Question

You made changes in vim and want to quit WITHOUT saving. What do you type (from Normal mode)?

Task 2 · Question

You are in Normal mode. Which key starts typing text on a new line below the cursor?

Task 3 · Practice

Copy /etc/ssh/sshd_config to ~/sshd_test and practise in vim: show line numbers, search for PermitRootLogin, copy that line, paste it below, change the copy, undo once, then save and quit.

Quick quiz

Question 1

1.Which key returns vim to Normal mode?

Question 2

2.What does dd do in Normal mode?

Question 3

3.Which command replaces every "8080" with "8443" in the whole file?

Question 4

4.vim says "E45: readonly option is set" on /etc/hosts. Most likely cause?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

Which key returns vim to Normal mode?

Answer: Esc

Esc always goes back to Normal mode.

What does dd do in Normal mode?

Answer: Deletes (cuts) the current line

dd cuts the whole line; p pastes it.

Which command replaces every "8080" with "8443" in the whole file?

Answer: :%s/8080/8443/g

% means all lines; g means every match on each line.

vim says "E45: readonly option is set" on /etc/hosts. Most likely cause?

Answer: You opened a root-owned file as a normal user without sudo

Open it again with sudo vim /etc/hosts.

In vim, how do you save the file and quit?Related

Answer: Esc, then :wq and Enter

:wq writes and quits. :q! quits without saving. Ctrl+X exits nano, not vim.

From The Linux filesystem and everyday commands

Why use visudo instead of vim /etc/sudoers?Related

Answer: It checks the syntax and locks the file

A broken sudoers file can lock out all admins; visudo prevents saving errors.

From sudo and the sudoers file

Which command keeps showing new lines of a log file as they are written?Related

Answer: tail -f app.log

tail -f follows the file and prints new lines as they arrive.

From Linux basics for backend developers

What should you do after troubleshooting in permissive mode?Related

Answer: setenforce 1 and confirm the config says enforcing

Return to enforcing.

From SELinux: what it is and its modes

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. This is the last topic of “Linux and the shell”. The module exam comes next.
  2. Module 1 exam: Linux and the shell · 13 questions · 20 min, without help. 6 topics of this module still to finish first.
  3. Then the next module, Permissions, users and sudo. 57 topics left in the course before the final exam.

← Previous: Reading text: grep, less, pipes and redirection