Text Editing with Vim
One objective, and the exam gives you no graphical editor. This covers vim's modes, the non-interactive ways to produce a file that are often faster, and two things that waste real time: the swap file left by a killed editor, and invisible characters in a config file you are sure is correct.
Essential Tools Guide 11 of 67 Beginner
- OSRHEL 10.0 (Coughlan)
- Kernel6.12.0-55.9.1.el10_0
- dnf4.20.0
- Flatpak1.16.0
- TimeAbout 14 min
- Reviewed23 August 2026
Written against the versions above. RHEL 10 minimal installs ship `vim-minimal`, which provides `vi` and **not** `vim`. Syntax highlighting and arrow keys in insert mode behave differently. If you rely on vim, check for it early - `rpm -qa | grep vim` is the first command in this guide for that reason.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RHCSA-A01 | 192.168.0.31 | RHEL 10.0 (Coughlan) | Practice node (graded) - spare /dev/sda | 2 Core | 4 GB | 50 GB + 15 GB |
Before you start
- A shell.
- The session builds and removes
~/ed.
-
Which editor is actually installed
vim-common-9.1.083-5.el10.x86_64 vim-data-9.1.083-5.el10.noarch vim-enhanced-9.1.083-5.el10.x86_64 vim-filesystem-9.1.083-5.el10.noarch vim-minimal-9.1.083-5.el10.x86_64Five packages, and two of them matter.
vim-minimalprovides/usr/bin/viand is installed on every RHEL system including the smallest.vim-enhancedprovides/usr/bin/vimand is not guaranteed - it happens to be present here.VIM - Vi IMproved 9.1 (2024 Jan 02, compiled Jan 27 2025 00:00:00) Included patches: 1-83 /usr/bin/vi /usr/bin/vimBoth commands exist, and
readlink -fonvireturns/usr/bin/vi- not a symlink tovim. They are two separate binaries from two separate packages, which is why the same keystrokes can behave differently depending on which you typed.On a minimal install
vimis missing entirely and onlyviis there: no syntax highlighting, and arrow keys in insert mode insert letters instead of moving the cursor. Candidates who have only used full vim find that disorienting under time pressure, so check which you have before you need it - and ifvim-enhancedis absent and the task allows installing,dnf install vim-enhanced.bash Example session rpm -qa | grep -E "^vim" | sortvim-common-9.1.083-5.el10.x86_64vim-data-9.1.083-5.el10.noarchvim-enhanced-9.1.083-5.el10.x86_64vim-filesystem-9.1.083-5.el10.noarchvim-minimal-9.1.083-5.el10.x86_64vim --version | head -2; command -v vi vimVIM - Vi IMproved 9.1 (2024 Jan 02, compiled Jan 27 2025 00:00:00)Included patches: 1-83/usr/bin/vi/usr/bin/vimreadlink -f $(command -v vi)/usr/bin/viExpected resultWhich vim packages are present and what
viresolves to.Success conditionYou know which editor you actually have before you need it.
-
The three modes
vim starts in normal mode, where letters are commands rather than text. That is the whole source of the confusion, and the map out of it is small:
| From normal mode | | |---|---| |
i| insert before the cursor | |a| insert after | |o| open a new line below | |Esc| back to normal mode | |:| command mode |The commands worth having in your fingers:
| Command | | |---|---| |
:w| write | |:wqorZZ| write and quit | |:q!| quit, discarding changes | |dd| delete a line | |yyp| copy a line, paste it | |/text| search,nfor next | |:%s/a/b/g| replace throughout | |u| undo |:q!is the one to memorise. When you do not know what state a file is in, leaving without saving and starting again is faster than repairing it - and on an exam, a config file you have half-edited is a liability.:set numberand:set pasteare the two settings that come up: line numbers for matching an error message, and paste mode to stop auto-indent mangling pasted blocks.bash Example session mkdir -p ~/ed && cd ~/ed && echo "one line" > quick.txt && cat quick.txtone linecd ~/ed && cat > heredoc.txt <<'EOF'first linesecond linethird lineEOFExpected resultTwo files created without opening an editor at all.
Success conditionYou can enter, leave and save in vim without guessing.
-
Writing a file without an editor
Often faster, and always scriptable:
first line second line third line 3 heredoc.txtcat > file <<'EOF'writes a block until the marker. Quoting the marker as'EOF'stops the shell expanding$variablesand backticks inside the block - which for a config file containing$is essential, and forgetting it is how a template ends up with empty values.printfis the precise one:printf 'a\nb\nc\n'writes exactly three lines with exactly one trailing newline, whereechobehaviour varies.For an exam task like "add a line to
/etc/fstab",echo '...' | sudo tee -a /etc/fstabis one command and cannot leave the file half-edited. Reserve the editor for changes that genuinely need judgement.bash Example session cd ~/ed && cat heredoc.txt && wc -l heredoc.txtfirst linesecond linethird line3 heredoc.txtcd ~/ed && printf 'a\nb\nc\n' > printf.txt && cat printf.txtabcExpected resultA three-line file from a heredoc, and another from printf.
Success conditionYou can produce a file correctly without entering insert mode.
-
Driving vim from the command line
vim can run its own commands non-interactively, which is useful when you want vim's editing model in a script:
first line SECOND line third linevim -es -c '%s/second/SECOND/g' -c 'wq' file- silent ex mode, one or more-ccommands, then write and quit.Appending and deleting work the same way:
first line SECOND line third line appended by exSECOND line third line appended by exg/^first/ddeleted every line starting withfirst- thegcommand is vim's "do this to every line matching", and it is the same idea assed.In practice
sed -iis the tool for scripted edits and is what the last command uses. This step exists because knowing vim commands work outside vim makes the editor less of a special case, and because-cis genuinely handy for opening a file at a line:vim +42 /etc/fstab.bash Example session cd ~/ed && vim -es -c '%s/second/SECOND/g' -c 'wq' heredoc.txt && cat heredoc.txtfirst lineSECOND linethird linecd ~/ed && vim -es -c '$a\appended by ex' -c 'wq' heredoc.txt && cat heredoc.txt [exit 1]cd ~/ed && vim -es -c 'g/^first/d' -c 'wq' heredoc.txt && cat heredoc.txtSECOND linethird lineExpected resultThree scripted edits applied without an interactive session.
Success conditionYou can edit a file from a script.
-
The swap file
Open a file in vim and a hidden companion appears:
--- while the editor is open: .quick.txt.swpKill the editor - a dropped connection, a closed terminal, a reboot - and the swap file stays:
--- editor killed, swap file left behind: .quick.txt.swpFrom then on, every attempt to open that file greets you with a full-screen recovery warning. Under exam pressure that reads as "I have broken something", and it is not.
vim -rexplains it:Swap files found: In current directory: 1. .quick.txt.swp owned by: sysadmin dated: Sun Aug 23 11:10:15 2026 file name: ~sysadmin/ed/quick.txt modified: no user name: sysadmin host name: rhcsa-a01 process ID: 10055modified: nois the line that decides what to do. It means nothing was unsaved, so the swap file holds nothing you want: delete it and move on. If it saidmodified: yes, recover withvim -r quick.txt, check the result, then delete the swap file.The file is hidden and named
., so.swp lswill not show it andls -awill. Deleting it is safe once you have read that one line.bash Example session cd ~/ed && (script -qfc "vim quick.txt" /dev/null >/dev/null 2>&1 &) ; sleep 3; echo "--- while the editor is open:"; ls -a | grep swp--- while the editor is open:.quick.txt.swpcd ~/ed && pkill -9 vim; sleep 1; echo "--- editor killed, swap file left behind:"; ls -a | grep swp--- editor killed, swap file left behind:.quick.txt.swpcd ~/ed && vim -r 2>&1 | head -12Swap files found: In current directory:1. .quick.txt.swp owned by: sysadmin dated: Sun Aug 23 11:10:15 2026 file name: ~sysadmin/ed/quick.txt modified: no user name: sysadmin host name: rhcsa-a01 process ID: 10055 In directory ~/tmp: -- none -- In directory /var/tmp: -- none --cd ~/ed && rm -f .quick.txt.swp && ls -a | grep swp || echo "swap file removed - the recovery prompt will stop appearing"swap file removed - the recovery prompt will stop appearingExpected resultA swap file appearing, surviving the kill, and being explained by
vim -r.Success conditionA recovery prompt will not cost you five minutes.
-
Characters you cannot see
A config file that looks perfect and does not work is usually a whitespace problem.
cat -Amakes every character visible:UUID=1234 /data xfs defaults 0 0$$marks end of line. Now a file with Windows line endings:good line^M$ crlf.txt: ASCII text, with CRLF line terminators^Mbefore the$- a carriage return. That extra byte is part of the value as far as anything parsing the file is concerned, so a mount option becomesdefaults\r, a shell script's shebang points at/bin/bash\r, and the error you get names something that looks correct.sed -i 's/\r$//'strips them:good line$ crlf.txt: ASCII textYou will not type a
^Myourself, but you will paste one in from a browser or a Windows machine. When a config file is definitely right and definitely not working,cat -Ait - andfilewill tell you about CRLF without you having to spot it.bash Example session cd ~/ed && printf 'UUID=1234 /data xfs defaults 0 0\n' > fstab.sample && cat -A fstab.sample | head -2UUID=1234 /data xfs defaults 0 0$cd ~/ed && printf 'good line\r\n' > crlf.txt && cat -A crlf.txt; file crlf.txtgood line^M$crlf.txt: ASCII text, with CRLF line terminatorscd ~/ed && sed -i 's/\r$//' crlf.txt && cat -A crlf.txt; file crlf.txtgood line$crlf.txt: ASCII textExpected resultLine endings made visible, then a stray carriage return removed.
Success conditionYou can find the invisible character breaking a config file.
Troubleshooting
vim: command not found.Why: Only
vim-minimalis installed, which providesvi.Fix:Use
vi, ordnf install vim-enhanced.A recovery warning every time a file is opened.
Why: A swap file from a killed editor.
Fix:
vim -rto inspect. If it saysmodified: no, delete.<file>.swp.Arrow keys insert letters in insert mode.
Why:
vifrom vim-minimal in compatible mode.Fix:Esc, move in normal mode with
hjkl, then re-enter insert. Or install vim-enhanced.Cannot save -
E45: 'readonly' option is set.Why: The file needs root and vim was not run with sudo.
Fix:
:w !sudo tee %then:q!, or quit and re-open with sudo.A pasted block came out with escalating indentation.
Why: Auto-indent applied to text that was already indented.
Fix:
:set pastebefore pasting,:set nopasteafter.