CST8207: Linux Operating Systems I

Lab Worksheet Six

The vi/vim text editor


This is Lab Worksheet 6 - not an Assignment

This Lab Worksheet contains some practical examples that will prepare you to complete your Assignments.
You do
not have to hand in this Lab Worksheet. Make sure you complete the separate Assignments on time.

1Before you get started - REMEMBER TO READ ALL THE WORDS

You must have an account on the Course Linux Server to do this lab. Log in to the server and use the shell. Review the Class Notes related to this worksheet as you work through it. Leave your work on the Linux server. Do not delete any work from the Linux server until the term is over and your marks are complete!

vi/vim : Hard to Learn - Easy to Use


1Exercise and practice in text editing - Read All The Words

1.1Section save1.txt

  1. Create a bash terminal window that is at least 80 columns wide by 24 lines long. Larger is good.

  2. Start the vim editor with this (new) file name as an argument: lab06.txt

  3. Turn on the vim showmode option so you know what mode you are in :set showmode
    (This is often enabled by default.) Also useful inside vim is: :set ruler

  4. Go into insert mode, enter this single line of text (mouse copy and paste) and save your work:

Royal Rhonda's repulsive, roaring rabbits ruined Randy's rutabagas

  1. Check your work with file, wc, and sum. You should see these results for your one-line text file:

[user@host ~]$ file lab06.txt ; wc lab06.txt ; sum lab06.txt

lab06.txt: ASCII text

1 8 67 lab06.txt

28356 1

  1. When you are successful, make a backup copy of your new one-line text file in save1.txt

1.2Section save2.txt

  1. Continue editing the lab06.txt file. Let's review some basic commands (see your reference card):

  2. With one command keystroke, go to the beginning (first character) of the line. That key is: ___

  3. Move across the line by next words, from left to right. That key is: ___

  4. Move back across the line by beginnings of words, from right to left. That key is: ___

  5. Move across the line by blank-delimited words left to right. That key is: ___

  6. Move back across the line by blank-delimited words right to left. Key used: ___

  7. Type one single character to go to the end of the line and simultaneously enter insert mode (one character: ___ ). Type a space and the word today after rutabagas. Do not add any punctuation yet. Remember to leave insert mode. Never stay in insert mode.

  8. Type one single character to go to the beginning of the line and simultaneously enter insert mode (one character: ___ ) Type the word Oh! at the beginning of the line (followed by a space) before Royal. Remember to leave insert mode. Never stay in insert mode.

  9. Save and check your work (two more words!) with wc and sum: 1 10 77 and 58465

  10. When you are successful, make a backup copy of your edited one-line text file in save2.txt

1.3Section save3.txt

  1. Continue editing the lab06.txt file.

  2. Move to the start of the line (one character: ___ ). Move forward to the first upper-case R (two characters: ___ ). Efficiently delete the word Royal (two characters: ___ ).

  3. Undo the previous deletion using one character: ___

  4. Redo the previous deletion using one (control) character: ___

  5. Add an exclamation point to the end of the line using only three characters. including the character used to get out of insert mode: ___ Remember to leave insert mode. Never stay in insert mode.

  6. Using only four characters, duplicate the first word in the line. Hint: Move to the beginning of the line (one character: ___ ). Yank the blank-delimited word into the cut buffer (two characters ___ ) and put the word before the cursor position (one character: ___ ). Make sure you put before not after.

  7. Save and check your work (ten words) with wc and sum: 1 10 76 and 35211

  8. When you are successful, make a copy of your edited one-line text file in save3.txt

1.4Section save4.txt

  1. Continue editing the lab06.txt file.

  2. Turn on line numbers using: :set number
    The line numbers appear inside vi/vim only; they will not be saved to the edited file.

  3. Using only three or four characters, duplicate the one line in the file 9 times; you should end up with 10 identical lines in the file. Hint: use one or two characters to yank the current line into the cut buffer: ___ then use a repeat count to put the buffer after the current line 9 times (one digit [the repeat count] and one letter: ___ ). Make sure you have 10 identical lines. Save the 10 identical lines and check your work with wc and sum: 10 100 760 and 52495

  4. When you are successful, make a backup copy of your edited 10-line text file in save4.txt

  5. Continue editing the lab06.txt file. Using only six characters, yank all 10 lines in the file (the whole file) then put it back 9 times, giving 100 lines total. Hint: two characters to go to the beginning (first line) of the file: ___ two characters to yank from here to end-of-file into the cut buffer ___ one digit and one character to put the cut buffer 9 times before the top line: ___ You can type control-G to show the current file and number of lines, to confirm that the file is 100 lines. Make sure you put before the current line when you put.

  6. Using three characters (two digits and a letter), go to line 70: ___

  7. Using one character, move to the top line on your screen (the top line on your screen is not the same as the beginning of the file): ___

  8. Type multiple k to move straight up, and watch the screen scroll down one line at a time.

  9. Using one character, move to the middle line on your screen: ___

  10. Using one character, move to the bottom line on your screen (the bottom line of your screen is not the same as the last line of the file): ___

  11. Type multiple j to move straight down, and watch the screen scroll up one line at a time.

  12. Use one character to move to the bottom line of the file (move to end-of-file): ___

  13. Type three characters plus [Enter] to search forward for the two-letter pattern: ru ___

  14. Type one character to repeat the search forward (in the same direction) for the next match: ___

  15. Move to find match number 50 in the file using two digits (repeat count) and one character: ___

  16. Type one character to repeat the search backward (reverse direction) for the previous match: ___

  17. Undo the most recent text change(s) using the undo character: ___ . Your file should be back to 10 lines again. (Use control-G to check!) Make sure the file has ten identical lines before continuing.

  18. Optionally, turn off line numbering if you don't like it (some people do): :set nonumber

1.5Section save5.txt

  1. Continue editing the 10-line lab06.txt file that contains 10 identical lines. (Same as save4.txt )

  2. Go to line 8 (use one digit and one letter: ___ ). Move forward to the first lower-case r (two characters: ___ )

  3. Now use six characters to change the word repulsive to ill Hint: use two command characters to change a word and enter insert mode, type three letters "ill", type one mode-change character: ___ . Remember to leave insert mode. Never stay in insert mode.

  4. Move forward to the next lower-case r on this line (two characters [a repeat move is just one character if you know how!]: ___ ) and type one single character to repeat the last text-changing command (one character: ___ ). Repeating the change will also change the word roaring to ill on this line (line 8). You can save a lot of typing using the repeat command.

  5. Using only eight command characters, go to line 6 and replace the same two words with ill. Hint: go to line 6 takes two characters: ___ move forward to r takes two characters (only one if you know how): ___ repeat the last text change takes one character: ___ move forward takes two characters (only one if you know how): ___ repeat the last text change takes one character: ___

  6. Undo the last change so that roaring re-appears in line 6 (one character undo: ___ ).

  7. Redo the last change so that ill re-appears in line 6 (one control-character redo: ___ ).

  8. Undo the last change so that roaring re-appears again in line 6 (one character undo: ___ ).

  9. Use nine characters to go line 4 and change everything from the start of the line forward to the first comma (inclusive) to the word How. Hint: go to line 4 takes two characters ___ change text forward to comma takes three characters (a change command followed by a forward motion to a comma - it goes into insert mode) ___ typing How takes three characters and exit insert mode takes one character.

  10. Save and check your work with wc and sum: 10 97 720 and 05436

  11. When you are successful, make a backup copy of your edited 10-line text file in save5.txt

1.6Section save6.txt

  1. Continue editing the lab06.txt file.

  2. Using five characters (or less), toggle upper/lower case on every character on line two. Hint: go to line two takes two characters: ___ toggling 99 characters (more than the length of line two) takes two digits and one character: ___ Most of the line will now be upper-case letters. (There is also a three-character way to make this same change that works for any line length. One of the reference sheets has it.)

  3. Use four characters to delete the first four blank-separated words on this line (line two). Hint: one character takes you to the beginning of the line ___ deleting four blank-separated words takes one digit and two characters: ___ )

  4. Use four characters to replace the only lower-case letter in this line (line two) with upper-case C so that it says CANDY'S Hint: use two characters to move forward to the r: ___ use two characters to replace the single r with C: ___ Make sure all six words on this line (line two) are now upper-case.

  5. Save and check your work with wc and sum: 10 93 692 and 64996

  6. When you are successful, make a backup copy of your edited 10-line text file in save6.txt

1.7Section save7.txt

  1. Continue editing the lab06.txt file.

  2. Use four characters to go to the last line of the file (move to end of file) and delete everything from the comma to the end of the line. Hint: one character to move to end-of-file ___ two characters to move forward to the character comma ___ one character to delete from the cursor to end-of-line ___

  3. Use three characters to make the same change to the line above (to line 9). Hint: one character to go straight up one line ___ one character to move right one character (onto the comma) ___ one character to repeat the last text change (that deletes to end-of-line) ___

  4. Use four characters to make the same change to line 7. Hint: two characters to go straight up two lines ___ one character to move right one character (onto the comma) ___ one character to repeat the last text change (that deletes to end-of-line) ___

  5. Use the same four characters to make the same change to line 5.

  6. Use two or three characters to undo the last three text changes, restoring the deleted text at the end of lines 5, 7, and 9: ___ (You can repeat the undo command three times, or use a digit 3 followed by the undo command letter to repeat undo three times.) The text remains missing from line 10 only.

  7. Save and check your work with wc and sum: 10 87 643 and 48878

  8. When you are successful, make a backup copy of your edited 10-line text file in save7.txt

1.8Section save8.txt

  1. Continue editing the lab06.txt file.

  2. Use five characters to go to line three and delete the first 69 characters, leaving only the word today! Hint: two characters go to line three ___ then use two digits (the repeat count) and one character to delete 69 characters moving to the right ___

  3. You are currently on line three. Insert the word Not (and a space) at the start of line three, in front of today! Remember to leave insert mode. Never stay in insert mode.

  4. Use three characters to exchange lines three and four. Hint: two characters delete the current line three (into the cut buffer; line four moves up and becomes the new current line three) ___ one character puts the cut buffer after the current line ___ Line four, containing the contents of the put cut buffer, becomes the current line: Not today!

  5. Starting where you are on line four, use two characters to join line four onto the end of line three. Hint: use one character to move straight up to line three and one character to join the two lines: ___

  6. Move line one down to be line three, so that all the lines starting with Oh! are together. Hint: move to line one; delete line one into the cut buffer; move down one line (to line two) and put the cut buffer after the current line. Line one is now line three. All lines starting with Oh! are together.

  7. Move to the start of line three, the first line starting with Oh!

  8. Use two characters to delete the first word (including the punctuation) on this line (line three): ___

  9. Use three characters to move to the next line (line four) and delete the first two words. Hint: one character moves straight down to the same place on the line below ___ and you can repeat the last text change (delete a word) twice using a digit followed by one character to repeat the last text-changing command ___

  10. Use just two characters to delete the first two words on the next line (line five) ___ . Hint: one character moves straight down to the same place on the line below and one character repeats the last text change, which was to delete two words.

  11. Use just three characters to skip a line and delete the first two words on the next line (line seven). Hint: two characters move straight down two lines ___ one character repeats the last change (which was to delete two words) ___ You can save a lot of typing using the repeat last change command.

  12. Save and check your work with wc and sum: 9 72 551 and 54341

  13. When you are successful, make a backup copy of your edited 9-line text file in save8.txt

1.9Verification Section

You have created one lab06.txt file and eight save[1-8].txt files. Execute the following shell command lines to record your work. Make sure you get the typing exactly correct in these lines:

cd ; mkdir lab06

mv lab06.txt save?.txt lab06/

ls -dils lab06/* >lab06/.verify.txt

sum lab06/* >>lab06/.verify.txt

Note the use of a hidden file name for the output redirection. This hidden name will not be matched by the shell GLOB patterns.

Check to ensure that you have 9+9=18 lines of output in the .verify.txt file. (Recall the command that counts lines inside a file. Use it to count the lines in your .verify.txt file. There should be 18 lines.)

Compare the output of the above ls -dils command (without redirection) with the output of this command:

find lab06 -ls

You will recall that the man page for find documents that the find -ls option produces "ls -dils" format output, and the above shows that this is true.

The next page contains some optional advanced editing features.

2Advanced Editing Section (optional)

2.1Send any number of lines to an external command

You can send any number of lines from your editor buffer into the standard input of any command. The command may process those lines and the output of the command will replace the lines in the editor buffer.

  1. Edit the adv10.txt file (a copy of lab06.txt) that contains nine lines.

  2. Go to the first line of the file and type this four-character command followed by [Enter]: !Gwc

  1. Undo the last change. (The output from wc disappears and the nine original lines return.)

  2. Go to the first line of the file and type this five-character command followed by [Enter]: !Gsum

  1. Undo the last change. (The output from sum disappears and the nine original lines return.)

  2. Go to the last line of the file (line nine) and open a new blank line (line ten).

  3. On the new blank line type this text: ls -li lab06.txt save?.txt

  4. Send the current line into bash by typing this seven-character command: !!bash

  1. Go to line one and execute the seven-character command: !Gsort What happens?

  2. Go to line five and execute: !!date What happens? Try other commands!

  3. Quit the editor without saving anything.

2.2Record any sequence of edit commands in a macro

You can record any complex sequence of editor commands into a single-letter macro that you can re-execute.

  1. Edit the adv10.txt file (a copy of lab06.txt) that contains nine lines.

  2. Go to the beginning of the top line of the file.

  3. Record a macro named q by typing qq that will turn on the recording flag in the bottom line status bar. You are now recording all the commands you are using into buffer q. Do these commands:

  1. The first line now has three asterisks in it: ... CANDY'S *** RUTABAGAS ...

  2. Move to the beginning of any other line in the document and re-execute this macro by typing: @q

    Note how the macro executes the same editor commands and makes the same change to this line.

  3. Move to the second space in any line in the document and re-execute the macro: @q

    Note how the macro makes the same change to the sixth space, because it moves forward four spaces.

  4. Go to the last line of the file (line nine) and open a new blank line (line ten).

  5. On this blank line dump the macro contents by typing: "qp (that is a single double quote to start)

  6. Change the three asterisks to Fancy Macro and re-save the macro: "qyy (one double quote)

  7. Go to any line and try the new macro. You can dump and modify any macro after you have recorded it.

Page 7 of 7

©2013 Algonquin College

Shawn Unger, Todd Kelley, Ian Allen

worksheet06.odt

Version 14 02/15/2013, 06:30