CST8207: Linux Operating Systems I
Lab Worksheet Six
The vi/vim text editor
This worksheet is part of an optional BONUS assignment. You must successfully complete and hand in this finished worksheet if you do the optional BONUS assignment. See the assignment for details.
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 – console terminal-based (non-GUI) text editor, standard version (vi may have limited features)
vim – vi text editor (as above) but an improved version (vim has full features that vi may not)
gvim – vim text editor - graphical version for X11 GUI (full features) - may not be installed
vimtutor – vim text editor interactive tutorial - type this command at any shell prompt
The Linux command vi is usually a link to the newer vim text editor. Some versions of Linux install a smaller, limited version of vi as the default editor and you have to explicitly ask for an upgrade to get the full vim version with all the great features. gvim (if available) starts an X11 GUI-based version of vim.
Some distributions (e.g. Fedora 12) install a smaller, less feature-filled version of vim as vi (e.g. /bin/vi), but then use system aliases to alias vi to a larger version of vim (e.g. /usr/bin/vim), so that you never really know which version you're getting. You can define your own alias to be sure.
You can find out which version of vim you're running using the vim :version and :help commands. (Under Fedora, the smaller vi version has the wrong help files installed. Use vim not vi on Fedora 12.)
The vi text editor is the standard editor available on most every Unix-derived system, including Linux, MacOSX, and BSD. It can be used on most any terminal, over the slowest of dial-up links. It is a pure console terminal-based program that needs no mouse or graphical display screen. No mouse!
Similar to learning to touch-type on a keyboard, the vim editor is difficult to learn but easy to use once you have some mastery of it. Until you master it, you will find using vi awkward. Once you master it, you will easily outperform anyone using a mouse-based editor such as Notepad. Learn the tool!
The single-character command keys used in vi have found their way into other programs. The bash shell supports a vi mode for editing command lines, and the less and more pager programs (used by the man command) use vi commands to move around the screen and search for text.
vi is incredibly powerful. Most students refuse to learn it well enough to get out of the awkward stage. They never master many vi commands. They go into vi insert mode and perform all text editing using the arrow and backspace keys, turning vi into a slower, mouseless version of Notepad. If you want a Unix/Linux job, learn the editor tool! If you want to use arrow keys, us the Pico or Nano editors instead.
vi Recommended Reference card – use the front only (Donald Binder):
vi / vim Cheat Sheets: http://cheat-sheets.org/#Vim
Online interactive web tutorial (untested): http://lifehacker.com/5844890/the-interactive-vim-tutorial-teaches-you-how-to-use-the-super+efficient-vim-text-editor
Home page: http://www.vim.org/
Vi Lovers Home Page is http://thomer.com/vi/vi.html
Work in your own HOME directory. You will be creating files in your own HOME directory.
Open the recommended vi reference card before you begin. All the commands needed for this lab are there, including "redo last change" which is control-R in vim (not available in old versions of vi).
Before you begin, complete the vimtutor tutorial program that teaches you vim basics.
The following text editing exercise requires absolute precision. You are system technicians; small errors in configuration files can disable systems. Accuracy is important. You can work on each section of this exercise repeatedly until you get it letter-perfect, then move on to the next section.
In the underlined spaces below, enter what you typed into vi/vim to make the given edit using the given number of vi/vim command characters (or fewer, if you can). Do not use any arrow keys.
If you make a mistake, simply type the vim undo command character repeatedly to undo your mistake(s). You can undo (and redo) multiple times to get the file back to a state you recognize.
You don't have to save and exit the editor to check your work, below. Use a second terminal so that you don't have to leave and re-enter vim every time. Save your work, then use the other terminal to run the file, wc, and sum commands shown below.
Do not use insert mode and the arrow keys to move around the text file. Use command mode and the motion commands - they are much faster once you learn them. Do not use the arrow keys!
When below it says "insert a word", it means the word and the space(s) around the word, not just the letters of the word. Make sure each word is separated from adjacent words with one space.
Inserting text always means "insert the text then return to command mode". Do not remain in insert mode. Always return to command mode after an edit, so you are ready for the next command.
Do not enter multiple spaces between words. Do not enter spaces at the start or end of lines.
If you want to see the extra spaces at the end of lines, enter: :set list
Turn off list view using: :set nolist
Do not enter extra blank lines, especially blank lines at the bottom of the file. No extra blank lines!
Save your work after each successful section, so that you can return to this point if you don't get the next section correct. Think of these save files as little snapshots of your editor session.
Create a bash terminal window that is at least 80 columns wide by 24 lines long. Larger is good.
Start the vim editor with this (new) file name as an argument: lab06.txt
on the vim
option so you know what mode you are in :set
(This is often enabled by default.) Also useful inside vim is: :set ruler
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
You must only mouse-paste text into vim when in insert mode. Pasting into command mode will run your mouse-pasted text as vim commands! Always mouse-paste into insert mode!
Case matters in everything in this exercise. Use one space only between each word.
There is no final punctuation yet. Those are ASCII apostrophes, not UTF-8 smart quotes.
There are no leading or trailing spaces on the one line.
There are no leading or trailing blank lines. The file should be exactly one line long.
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
When you are successful, make a backup copy of your new one-line text file in save1.txt and optionally make the save file read-only so that you don’t change it by mistake.
Continue editing the lab06.txt file. Let's review some basic commands (see your reference card):
With one command keystroke, go to the beginning (first character) of the line. That key is: ___
Move across the line by next words, from left to right. That key is: ___
Move back across the line by beginnings of words, from right to left. That key is: ___
Move across the line by blank-delimited words left to right. That key is: ___
Move back across the line by blank-delimited words right to left. Key used: ___
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.
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.
Save and check your work (two more words!) with wc and sum: 1 10 77 and 58465
When you are successful, make a backup copy of your edited one-line text file in save2.txt and optionally make the save file read-only so that you don’t change it by mistake.
Continue editing the lab06.txt file.
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: ___ ).
Undo the previous deletion using one character: ___
Redo the previous deletion using one (control) character: ___
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.
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.
Save and check your work (ten words) with wc and sum: 1 10 76 and 35211
When you are successful, make a copy of your edited one-line text file in save3.txt and make the save file read-only so that you don’t change it by mistake.
Continue editing the lab06.txt file.
Turn on line
numbers using: :set
The line numbers appear inside vi/vim only; they will not be saved to the edited file.
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
When you are successful, make a backup copy of your edited 10-line text file in save4.txt and optionally make the save file read-only so that you don’t change it by mistake.
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.
Using three characters (two digits and a letter), go to line 70: ___
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): ___
Type multiple k to move straight up, and watch the screen scroll down one line at a time.
Using one character, move to the middle line on your screen: ___
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): ___
Type multiple j to move straight down, and watch the screen scroll up one line at a time.
Use one character to move to the bottom line of the file (move to end-of-file): ___
Type three characters plus [Enter] to search forward for the two-letter pattern: ru ___
Type one character to repeat the search forward (in the same direction) for the next match: ___
Move to find match number 50 in the file using two digits (repeat count) and one character: ___
Type one character to repeat the search backward (reverse direction) for the previous match: ___
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.
Optionally, turn off line numbering if you don't like it (some people do): :set nonumber
Continue editing the 10-line lab06.txt file that contains 10 identical lines. (Same as save4.txt )
Go to line 8 (use one digit and one letter: ___ ). Move forward to the first lower-case r (two characters: ___ )
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.
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.
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: ___
Undo the last change so that roaring re-appears in line 6 (one character undo: ___ ).
Redo the last change so that ill re-appears in line 6 (one control-character redo: ___ ).
Undo the last change so that roaring re-appears again in line 6 (one character undo: ___ ).
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.
Save and check your work with wc and sum: 10 97 720 and 05436
When you are successful, make a backup copy of your edited 10-line text file in save5.txt and optionally make the save file read-only so that you don’t change it by mistake.
Continue editing the lab06.txt file.
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.)
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: ___ )
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.
Save and check your work with wc and sum: 10 93 692 and 64996
When you are successful, make a backup copy of your edited 10-line text file in save6.txt and optionally make the save file read-only so that you don’t change it by mistake.
Continue editing the lab06.txt file.
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 ___
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) ___
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) ___
Use the same four characters to make the same change to line 5.
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.
Save and check your work with wc and sum: 10 87 643 and 48878
When you are successful, make a backup copy of your edited 10-line text file in save7.txt and optionally make the save file read-only so that you don’t change it by mistake.
Continue editing the lab06.txt file.
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 ___
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.
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!
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: ___
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.
Move to the start of line three, the first line starting with Oh!
Use two characters to delete the first word (including the punctuation) on this line (line three): ___
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 ___
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.
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.
Save and check your work with wc and sum: 9 72 551 and 54341
When you are successful, make a backup copy of your edited 9-line text file in save8.txt and optionally make the save file read-only so that you don’t change it by mistake.
You have created one lab06.txt file and eight save[1-8].txt files in your HOME directory.
The lab06.txt and save8.txt files should be identical. (Use the wc and sum commands to make sure.)
Execute the following shell command lines to save your work in a new lab06 directory. You will create the directory, move all the files into the directory, make all the files read-only (mode 400), then create a hidden verification file with the output of two commands appended to it.
Make sure you get the typing exactly correct in these next five lines. (Use copy-and-paste to be sure!)
cd ; mkdir -p lab06
mv lab06.txt save?.txt lab06/
chmod 400 lab06/*
ls -dils lab06/* >lab06/.verify.txt
sum lab06/* >>lab06/.verify.txt
Note the use of >> append redirection on the sum command line. Note the use of a hidden file name for both the output redirection lines. This hidden name will not be matched by the shell GLOB patterns. Changing permissions with chmod will be covered fully toward the middle of the semester.
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.)
Follow the directions in the optional BONUS assignment to submit your work.
You must also submit this completed worksheet as part of the optional BONUS assignment. See the BONUS assignment for the details. Please read all the words.
The next page contains some optional advanced editing features.
These features are not part of the BONUS assignment.
This section shows off two of the more advanced and cool vi/vim editor features.
This section is optional. I hope it interests you in learning more about vi/vim
Return to your own HOME directory. Copy the save8.txt file from the previous section to the new file adv10.txt under HOME. The file should have a wc of 9 72 551 Do not edit the wrong file.
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.
In your HOME directory, edit the adv10.txt file (a copy of save8.txt) that contains nine lines.
Go to the first line of the file and type this four-character command followed by [Enter]: !Gwc
this sends all lines from the cursor to end-of-file into wc and replaces them with the output of wc
you should see the expected 9 72 551 output from wc
Undo the last change. (The output from wc disappears and the nine original lines return.)
Go to the first line of the file and type this five-character command followed by [Enter]: !Gsum
sends all lines from the cursor to end-of-file into sum and replaces them with the output of sum
you should see the expected 54341 output from sum
Undo the last change. (The output from sum disappears and the nine original lines return.)
Go to the last line of the file (line nine) and open for input a new blank line (line ten).
On the new blank line type this text: ls -li adv10.txt /etc/passwd
Send the current line into bash by typing this seven-character command: !!bash
when you push [Enter] this sends just the current line (line ten) into the bash shell program
bash executes the commands it is reading on standard input and the output goes into the editor
you should see the multi-line output of the ls command replacing line ten
Go to line one and execute the seven-character command: !Gsort What happens?
Go to line five and execute: !!date What happens? Try other commands!
Quit the editor without saving anything.
You can record any complex sequence of editor commands into a single-letter macro that you can re-execute.
In your HOME directory, edit the adv10.txt file (a copy of save8.txt) that contains nine lines.
Go to the beginning of the first line of the file.
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:
Move forward in this line to the fourth space: 4f (there is a space after the f command)
Append three asterisks and a space after the cursor (and always exit insert mode): a*** ESC
Close the editing macro by typing a single q that turns off the recording flag. Macro q is ready.
The first line now has three asterisks in it: ... CANDY'S *** RUTABAGAS ...
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.
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.
Go to the last line of the file (line nine) and open a new blank line (line ten).
On this blank line dump the macro contents by typing: "qp (that is one double quote to start)
Change the three asterisks to Fancy Macro and re-save the macro: "qyy (one double quote)
Go to any line and try the new macro. You can dump and modify any macro after you have recorded it.
©2013 Algonquin College
Shawn Unger, Todd Kelley, Ian Allen