Bookmark this page

Guided Exercise: Practice: Edit a File with vim

In this lab, you will edit a file using vim.

Resources
Machines: desktopX
Files: /usr/share/doc/vim-common-*/README.txt

Outcomes

A copy of the vim README.txt which has been edited according to the instructions in this practice exercise.

N/A

  1. Log into your desktopX system as student and open a terminal.

  2. Create a copy of the file /usr/share/doc/vim-common-*/README.txt in your home directory.

    1. [student@desktopX ~]$ cp /usr/share/doc/vim-common-*/README.txt .
  3. Open /home/student/README.txt in vim.

    1. [student@desktopX ~]$ vim README.txt
  4. Jump to the section titled MAIN AUTHOR, then put your cursor on the A in AUTHOR.

    1. From command mode, type the following, then press Enter. This will jump to the first occurrence of the text:

      /MAIN AUTHOR
    2. Press w to move the cursor one word to the right; this should put you on the A in AUTHOR.

  5. Change this occurrence of the word AUTHOR to ROCKSTAR.

    1. From command mode, type cw to change the word under the cursor.

    2. Type ROCKSTAR.

    3. Press Esc to return to command mode.

  6. Undo your previous edit.

    1. Press u to undo your last edit.

  7. Redo (i.e., undo your undo) your last edit.

    1. Press Ctrl+r to redo your last undo.

  8. Using visual mode, make a copy of the INSTALLATION paragraph (including header) and place it at the end of the file.

    1. Move the cursor to the start of the INSTALLATION section by searching for ^INSTALLATION. From command mode, type:

      /^INSTALLATION
    2. Enter visual line mode by pressing V.

    3. Move the cursor to the end of section by typing 3}. This will move the cursor three paragraphs down, selecting our entire section. (The heading counts as a paragraph.)

    4. Press y to yank (copy) the selected lines to the unnamed buffer.

    5. Move the cursor to the end of the document by pressing G.

    6. Put (paste) the unnamed buffer below the current line by pressing p.

  9. In the entire document, replace each occurrence of README with PLEASE_READ_ME.

    1. From command mode, type the following:

      :%s/README/PLEASE_READ_ME/g
      • The : enters Ex mode.

      • % indicates that we want to work on every line in the document.

      • s/README/PLEASE_READ_ME/ is the search and replace command.

      • The trailing g indicates that this replace operation can be performed more then once per line.

  10. Exit without saving your changes.

    1. From Command mode type :q!.

      The : enters ex mode, the q indicates we want to quit, and the ! tells vim to force the quit, since we have unsaved changes.

  11. Clean up by removing your README.txt.

    1. [student@desktopX ~]$ rm README.txt
Revision: rh134-7-63a207e