CIT050 Index > Assignment: vim (2)

Assignment: vim (2)

The most up to date version of this assignment is at Moodle.

Read everything before doing anything!

First: if you don’t have the vim cheat sheet, get it from the resources for this assignment (PDF format).

The following part is for your practice; I could look at the files afterward, but I would have no idea if you had done the correct commands! That doesn’t mean you should just skip those parts–the practice will help you for the questions at the end.

Practice

Do the following steps, using the files preamble.txt and lincoln.txt from the previous assignment:

  1. Type vim preamble.txt to start editing the file.

  2. Move the cursor to the last letter o in the word Posteriors in line 5, and delete the remainder of the word by typing dw.

  3. Now type i to enter insert mode, add ty and press ESC to finish correcting the word to Posterity .

  4. Move the cursor to the beginning of line 2, and press R to enter replace mode. Type more and press ESC.

  5. Move the cursor to the r in prefect and then press xp to swap the bad characters. (The x deletes the character, and p pastes it in after the cursor position.)

  6. Change all occurrences of united to United with this command:

    :1,$s/united/United/g
  7. Remove all occurrences of Tranquility at the beginning of a line with this command:

    :1,$s/^Tranquility//
  8. On all lines that contain the word of , change states to States with this command:

    :g/of/s/states/States/
  9. Write the file back to disk by typing :w [Note: you are NOT exiting vi!]

  10. Type :vi lincoln.txt to start editing that file.

  11. With one command, correct all occurrences of teh to the .

  12. Move to the line starting Fourscore and seven years ago

  13. Copy the next three lines plus the following blank line into the general-purpose buffer by typing 4Y (make sure you use a capital Y).

  14. Move up to the line starting Now we are engaged in a great and press P to paste the text before the text.

  15. Move back down to the lines you copied, and delete them with one command.

  16. Type :wq to write the file back to disk and quit vim.

When You Finish

Answer the following questions. Please use a plain text file rather than a word processor. Do not send me a .doc file! Name the file in the form lastname_firstname_vim2.txt Then upload the file to Moodle.

What commands would you use in vim to:

  1. Replace the first occurrence Fred with Sam on line 16 (last line mode)
  2. Replace all occurrences of Mary with Marta on lines 20 through 45 (last line mode)
  3. Delete to the end of the current line (command mode). If the cursor is in the middle of the line, this will delete everything to the right of the cursor, leaving information to the left of the cursor as it was.
  4. Yank five lines (command mode)
  5. Go to line 37 (last line mode)
  6. Put the yanked lines before line 37 (command mode)
  7. Read file info.txt into the Work buffer (last line mode)
  8. Tell vim not to wrap long lines (last line mode)
  9. Execute the date shell command (last line mode)
  10. Find the next occurrence word test , and it must be that word alone; don’t find testing or attest.