CertGrid CertGrid
Hands-on Lab·Red Hat Certified System Administrator

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

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.

Every command on this page runs on RHCSA-A01.
Server NameIP AddressOSRolesCPURAMHDD
RHCSA-A01192.168.0.31RHEL 10.0 (Coughlan)Practice node (graded) - spare /dev/sda2 Core4 GB50 GB + 15 GB

Before you start

  1. 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_64

    Five packages, and two of them matter. vim-minimal provides /usr/bin/vi and is installed on every RHEL system including the smallest. vim-enhanced provides /usr/bin/vim and 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/vim

    Both commands exist, and readlink -f on vi returns /usr/bin/vi - not a symlink to vim. 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 vim is missing entirely and only vi is 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 if vim-enhanced is 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/vi

    Expected resultWhich vim packages are present and what vi resolves to.

    Success conditionYou know which editor you actually have before you need it.

  2. 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 | | :wq or ZZ | write and quit | | :q! | quit, discarding changes | | dd | delete a line | | yy p | copy a line, paste it | | /text | search, n for 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 number and :set paste are 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 lineEOF

    Expected resultTwo files created without opening an editor at all.

    Success conditionYou can enter, leave and save in vim without guessing.

  3. Writing a file without an editor

    Often faster, and always scriptable:

    first line
    second line
    third line
    3 heredoc.txt

    cat > file <<'EOF' writes a block until the marker. Quoting the marker as 'EOF' stops the shell expanding $variables and backticks inside the block - which for a config file containing $ is essential, and forgetting it is how a template ends up with empty values.

    printf is the precise one: printf 'a\nb\nc\n' writes exactly three lines with exactly one trailing newline, where echo behaviour varies.

    For an exam task like "add a line to /etc/fstab", echo '...' | sudo tee -a /etc/fstab is 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.txtabc

    Expected resultA three-line file from a heredoc, and another from printf.

    Success conditionYou can produce a file correctly without entering insert mode.

  4. 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 line

    vim -es -c '%s/second/SECOND/g' -c 'wq' file - silent ex mode, one or more -c commands, then write and quit.

    Appending and deleting work the same way:

    first line
    SECOND line
    third line
    appended by ex
    SECOND line
    third line
    appended by ex

    g/^first/d deleted every line starting with first - the g command is vim's "do this to every line matching", and it is the same idea as sed.

    In practice sed -i is 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 -c is 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 line

    Expected resultThree scripted edits applied without an interactive session.

    Success conditionYou can edit a file from a script.

  5. The swap file

    Open a file in vim and a hidden companion appears:

    --- while the editor is open:
    .quick.txt.swp

    Kill the editor - a dropped connection, a closed terminal, a reboot - and the swap file stays:

    --- editor killed, swap file left behind:
    .quick.txt.swp

    From 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 -r explains 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: 10055

    modified: no is 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 said modified: yes, recover with vim -r quick.txt, check the result, then delete the swap file.

    The file is hidden and named ..swp, so ls will not show it and ls -a will. 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 appearing

    Expected resultA swap file appearing, surviving the kill, and being explained by vim -r.

    Success conditionA recovery prompt will not cost you five minutes.

  6. Characters you cannot see

    A config file that looks perfect and does not work is usually a whitespace problem. cat -A makes 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

    ^M before 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 becomes defaults\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 text

    You will not type a ^M yourself, 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 -A it - and file will 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 text

    Expected resultLine endings made visible, then a stray carriage return removed.

    Success conditionYou can find the invisible character breaking a config file.

Troubleshooting

Official sources