4 of 4

Working with TextLesson 4 of 4

Creating and editing files with vim

Create and change configuration files in vim, the editor that is always installed, with nano as a fallback.

  • vim
  • vi
  • nano
  • vimtutor
  • :wq
  • :set number

40 min5 exercises

After this lesson you can

  • Move, insert, change, delete, copy and paste in vim without leaving the keyboard home row.
  • Search and replace, undo and redo, and save or quit with or without saving.
  • Fix a mistake in a root-owned file safely with sudoedit or sudo vim.

The idea

vi (really vim) is installed on every Linux system you will meet, including a rescue shell, so you should be able to edit with it. It has modes. In normal mode keys are commands; in insert mode keys type text; in command-line mode (after :) you type commands such as save and quit. Esc always returns to normal mode.

Key (normal mode)Action
i, a, oInsert before the cursor, after it, on a new line below
h j k l or arrowsMove left, down, up, right
w, b, 0, $Next word, previous word, start, end of line
gg, G, 12GFirst line, last line, line 12
x, dd, dwDelete a character, a line, a word
yy, p, PCopy a line, paste after, paste before
u, Ctrl+RUndo, redo
/text, n, NSearch forward, next, previous
:w, :q, :wq, :q!Save, quit, save and quit, quit without saving
:%s/old/new/gReplace everywhere
:set numberShow line numbers

If you are lost, press Esc twice and type :q!. Run vimtutor once: it is a half-hour interactive lesson. On RHEL the vi command is a minimal vim; sudo dnf install vim-enhanced adds the full vim with colour and extra features.

Try it

vim ~/lab/notes.txt

Press i, type the text, press Esc, then type :wq and Enter. Back in the shell:

cat ~/lab/notes.txt
one
two
three

Editing a root-owned file

Do not run an editor as root unless you have to: a mistake there has no safety net. sudoedit copies the file, lets you edit the copy with your own editor and settings, then writes it back as root.

export EDITOR=vim
sudoedit /etc/hosts
getent hosts labserver
192.0.2.10      labserver

Going further

nano is also installed on RHEL: type, Ctrl+O then Enter to save, Ctrl+X to exit, and it prints the key list at the bottom. To make vim your default editor for sudoedit, crontab -e and git, put export EDITOR=vim in ~/.bashrc. :set paste before pasting avoids staircase indentation, and :w !sudo tee % saves a root-owned file you opened without sudo.

Practice

0 of 5 done

Do these on your own system. They get harder. Check each result, then tick it off.

  1. Warm-up

    Create, save and quit

    Open a new file ~/lab/notes.txt in vim, type three lines (one, two, three), save and quit. Confirm the content from the shell.

    Check your result

    cat ~/lab/notes.txt prints the three lines.

    Hint

    Press i to insert, type, Esc to leave insert mode, then :wq and Enter.

    Solution
    vim ~/lab/notes.txt
    # i  one<Enter>two<Enter>three  Esc  :wq
    cat ~/lab/notes.txt
  2. Practice

    Delete, copy and paste lines

    Open notes.txt. Delete line 2, undo it, copy line 1 and paste it after line 3 so the file reads one two three one. Save.

    Check your result

    cat ~/lab/notes.txt prints one, two, three, one.

    Hint

    dd deletes a line, u undoes, yy yanks (copies) a line, p pastes below the cursor, gg and G jump to top and bottom.

    Solution
    vim ~/lab/notes.txt
    # jdd u   gg yy  G p   :wq
  3. Practice

    Replace across a file

    Copy /etc/hosts to ~/lab/hosts.copy. In vim, replace every localhost with LOCAL in the copy, but first count how many lines contain it. Save.

    Check your result

    grep -c LOCAL ~/lab/hosts.copy equals the earlier count of lines with localhost (grep -c localhost /etc/hosts).

    Hint

    :%s/old/new/g replaces everywhere. Add c at the end to confirm each one.

    Solution
    cp /etc/hosts ~/lab/hosts.copy
    grep -c localhost ~/lab/hosts.copy
    vim ~/lab/hosts.copy
    # :%s/localhost/LOCAL/g   :wq
    grep -c LOCAL ~/lab/hosts.copy
  4. Practice

    Edit a system file safely

    Add the line 192.0.2.10 labserver to /etc/hosts as a normal user with sudo, using an editor that works on a temporary copy. Confirm the name resolves.

    Check your result

    getent hosts labserver prints 192.0.2.10 labserver.

    Hint

    sudoedit /etc/hosts (or sudo -e) edits a copy and writes it back as root. The editor comes from $EDITOR.

    Solution
    export EDITOR=vim
    sudoedit /etc/hosts
    # G  o  192.0.2.10 labserver  Esc  :wq
    getent hosts labserver
  5. Challenge

    Quit without saving, then recover

    Open notes.txt, make a mess (delete every line), quit without saving, and show that the file is unchanged. Then reopen it, change two to 2 using search and replace on that line only, and show the final file.

    Check your result

    After the abandoned session cat still shows the old text. After the second session only the two line changed.

    Hint

    :q! quits without saving. :s/two/2/ replaces on the current line only.

    Solution
    vim ~/lab/notes.txt
    # ggdG  :q!
    cat ~/lab/notes.txt
    vim ~/lab/notes.txt
    # /two  Enter  :s/two/2/  :wq
    cat ~/lab/notes.txt