Beginner · Linux and the shell · topic 6 of 57 · 30 min
Editing files with vim
Explanation
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.
| Keys | Mode | What it does |
|---|---|---|
| i / a / o | Normal → Insert | Insert before the cursor / after it / on a new line below |
| Esc | any → Normal | Back to Normal mode |
| :w | command | Write (save) |
| :q | command | Quit (only if saved) |
| :wq or :x | command | Save and quit |
| :q! | command | Quit and THROW AWAY changes |
| h j k l (or arrows) | Normal | Left, down, up, right |
| gg / G | Normal | Go to the first / last line |
| :40 | command | Go to line 40 |
| dd / yy / p | Normal | Delete (cut) a line / copy (yank) a line / paste |
| x / u / Ctrl+R | Normal | Delete a character / undo / redo |
| /word then n | Normal | Search forward, then next match |
| :%s/old/new/g | command | Replace every "old" with "new" in the file |
A first real edit
$ 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 db01Handy 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.
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 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.
# vi /etc/fstab
# /data (search for the bad line)
# dd (delete it)
# :wq (save and quit)
# systemctl daemon-reload
# mount -aPractice (0/3 done)
You made changes in vim and want to quit WITHOUT saving. What do you type (from Normal mode)?
You are in Normal mode. Which key starts typing text on a new line below the cursor?
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Working with files: ls, cp, mv, rm, links and findLinux and the shell · same module
- The Linux filesystem and everyday commandsDevOps: Git, Docker, Kubernetes and CI/CD · Linux for DevOps
- sudo and the sudoers filePermissions, users and sudo
- Linux basics for backend developersBackend Development · Deployment and scaling
- SELinux: what it is and its modesSELinux and SSH
- Installing Go and the go commandGo Programming (backend and cloud) · Getting started with Go
Questions people ask about this
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.
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.
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.
What should you do after troubleshooting in permissive mode?Related
Answer: setenforce 1 and confirm the config says enforcing
Return to enforcing.
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
- This is the last topic of “Linux and the shell”. The module exam comes next.
- Module 1 exam: Linux and the shell · 13 questions · 20 min, without help. 6 topics of this module still to finish first.
- Then the next module, Permissions, users and sudo. 57 topics left in the course before the final exam.