Computer Science

The vi/vim text editor

· vi – console terminal-based (non-GUI) text editor, standard version (limited features)

· vim  vi text editor – improved version (full features)

· gvim  vim text editor – graphical version (full features) – may not be installed

· vimtutor  vim text editor interactive tutorial – type 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 a GUI-based version of vim.

· Some distributions 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 :version and :help commands.

vi/vim : Hard to Learn – Easy to Use

· 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.

· 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 #1 (Donald Binder):

· Front: http://teaching.idallen.com/cst8207/12w/notes/vi_refcard_front.pdf

· Back: http://teaching.idallen.com/cst8207/12w/notes/vi_refcard_back.pdf

· vi Reference card #2 (Laurent Gregoire) in multiple formats and languages:

· http://tnerual.eriogerg.free.fr/vim.html

Online interactive web tutorial (untested): http://www.openvim.com/

Vi Lovers Home Page is http://thomer.com/vi/vi.html

· Exercise and practice in text editing – Read All The Words

· Do this lab as an ordinary, non-root user. Do not run as root. Work in your own 1411Lab directory.

· Open the recommended vi reference card before you begin. All the commands needed for this lab are there, except for “redo last command” 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 filewc, 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.

1.1 Section save1.txt

1. Using PuTTY, open your Boole account.

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

3. Turn on showmode (if necessary) so you know what mode you are in :set showmode

Also useful 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

• 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.
5. Check your work with filewc, and sum. You should see these results for your one-line text file:
[user@host ~]$ file lab04.txt ; wc lab04.txt ; sum lab04.txt
lab04.txt: ASCII text
1 8 67 lab04.txt
28356 1
6. When you are successful, make a backup copy of your new one-line text file in save1.txt

cp lab04.txt save1.txt

1.2 Section save2.txt

1. Continue editing the lab04.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 sum1 10 77 and 58465

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

1.3 Section save3.txt

1. Continue editing the lab04.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 sum1 10 76 and 35211

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

1.4 Section save4.txt

1. Continue editing the lab04.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 sum10 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 lab04.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 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.5 Section save5.txt

1. Continue editing the 10-line lab04.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 illHint: 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 HowHint: 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 sum10 97 720 and 05436

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

1.6 Section save6.txt

1. Continue editing the lab04.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 sum10 93 692 and 64996

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

1.7 Section save7.txt

1. Continue editing the lab04.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 7Hint: 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 57, 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 sum10 87 643 and 48878

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

1.8 Section save8.txt

1. Continue editing the lab04.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 ___

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 sum9 72 551 and 54341

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

1.9 Hand-in Section – nine file name lines of output

1. Execute the following two shell command lines and paste the nine lines of output into this page, replacing all the red the sample output below. You should cut-and-paste these two command lines into a bash terminal window so that you get the typing exactly correct.

list=” lab04.txt save?.txt “

for f in $list ; do echo “$(sum $f) $(ls -li $f)” ; done

Replace all this red output with your correct nine lines of output from the above two command lines.

12345 1 12345 -rw-rw-r–. 1 user user 12345 2011-11-20 00:00 lab05.txt
12345 1 12345 -rw-rw-r–. 1 user user 12345 2011-11-20 00:00 save1.txt
12345 1 12345 -rw-rw-r–. 1 user user 12345 2011-11-20 00:00 save2.txt
12345 1 12345 -rw-rw-r–. 1 user user 12345 2011-11-20 00:00 save3.txt
12345 1 12345 -rw-rw-r–. 1 user user 12345 2011-11-20 00:00 save4.txt
12345 1 12345 -rw-rw-r–. 1 user user 12345 2011-11-20 00:00 save5.txt
12345 1 12345 -rw-rw-r–. 1 user user 12345 2011-11-20 00:00 save6.txt
12345 1 12345 -rw-rw-r–. 1 user user 12345 2011-11-20 00:00 save7.txt
12345 1 12345 -rw-rw-r–. 1 user user 12345 2011-11-20 00:00 save8.txt

Order now and get 10% discount on all orders above $50 now!!The professional are ready and willing handle your assignment.

ORDER NOW »»