4.7 Editor 3 - emacs4 Labs4.5 First Shell Script4.6 Editor - vi

4.6 Editor - vi

In this lab we will look at some of the features of the vi text editor. This can be a very good text editor for touch typists. The only special key that is needed is the "escape" key. For a full description of the vi text editor look at the manual pages.
  1. Copy the file ~csc137/flatpara.text
    gort:~>  cp ~csc137/flatpara.text .
    
    or ftp the file from ftp://lt.tucson.az.us/pub/ltaber/flatpara.text. The file is a paragraph from the book Flatland by Edwin A. Abbott.
  2. Bring up the file in vi.
    vi flatpara.text
    
  3. Insert your name and TABER CIS137as the first line of the file.
  4. Number each line in the file. Number the line with your name as 1. Place a space after each number. You should have 19 numbered lines. This needs to be placed into the file by hand. You may want to use the command :set number to check your numbers. In some versions of vi you will need to get out of insert mode with the esc key to move from one line to the next.
  5. With the ":" command move to line 11. :11<cr>.
  6. Move to the end of the line with the "$" in command mode. Delete the ";" with the x command. Move to the beginning of the line with a "0" (zero). Replace the "1" with a "4" using the "r" command. If you are on an X system, try positioning the cursor with the mouse.
  7. On lines 15 through the end of the file change all of the "t"s to "**". Use the ":" command ":15,$s/t/**/g". In this command "t" is a regular expression.
  8. Read the manual pages of ed or ex. If ed is not on your system try ex. This is where regular expressions may be documented. Read the section on regular expressions. On gort man uses less to help the user view the manual pages. less uses regular expressions for searching, like vi. So the "/" and "?" work for forward and backward searches. You may need to look at the vi manual pages. You can use regular expressions in many UNIX commands. We will come back to regular expressions in several future labs.
  9. Go to line 4 of the file with a ":4". Forward search for "land" with the "/" command "/land". Repeat the search twice with two "n" commands. To line four (4), the blank line, add the line number that the cursor is on. The "?" is used to search backwards. An "N" searches again, but in the other direction.
  10. Replace on lines 5 through 10 all single letters between two vowels with
    1. A pound sign "#"
    2. The first vowel
    3. The letter in the center
    4. A "$"
    5. A second copy of the letter
    6. the second vowel and
    7. A second "#"!
    Now for the command to do this.
    :5,10s/\([aeiou]\)\([a-z]\)\([aeiou]\)/#\1\2$\2\3#/g
    
    This needs an explanation. The "5,10s" indicates what area of the file. Between the first two forward slashes "/" are three regular expressions. They are marked off with "\(" and "\)". The first and last specify vowels, [aeiou]. The middle one, [a-z] is for all lower case letters. These regular expressions are referred to later by "\1", "\2", and "\3". The "g" at the end is for global, all of the occurrences on a line.
  11. Exit the vi editor with the command "ZZ" (Upper case). This will save your file.
  12. Print out your resulting file.
    gort:~>  lpr flatpara.text
    
Turn in your output from step 4.6 marked as follows:
your-name
Lab 4.6: vi Part 2
TABER CIS137
Place the lab in the instructor hand-in box in BUS R6E, the "terminal room".

VI Text Editor
Starting gort:~> vi file start edit
Stopping ZZ save edits and exit
  wq save edits and exit
  q! abandon changes
  w save changes,
  ~ continue editing
Refresh screen ^L Clear and redraw screen
Modes ESC forces Command mode
  ESC : forces ed/ex mode
Cursor use the cursor keys if set up.
  k, j, l, h up, down, right, left
  0 start of line
  ^ start of line
  $ end of line
  M middle of screen
  H beginning of screen
  L Last line of screen
  ^U up half a page
  ^D down half pages
  ^F forward full page
  ^B back full page
  line numberCR go to specified line
Text i insert before
  a append after
  r replace current character
  x delete current character
  dd delete line
  p put (insert) last delete
    or yank
  P put before current character
  yy yank to clipboard one line
Search /textCR look forward in text
  ?textCR look backwards in text
ed commands :ed-commandCR ex or ed commands
  :set number show line numbers
  :set nonumber do not show line numbers
  :setshow all show all set-able items
setup file .exrc initialization file

vi ":" commands
Command format In command mode type a ":"  
  A ":" and the cursor will be  
  at the bottom of the screen  
  waiting for a command.  
  First 0, 1, or 2 decimal  
  line addresses or . (current  
  line) or $ (last line), then a  
  command letter, usually lower  
  case then, optional trailing  
  arguments (and commands).  
  Followed by a carriage return.  
   
Escaping from The escape key  
   
Stopping vi :w write file
  :q exit - no write
  :Q abandon edits
Delete lines :d delete current line
  :2d delete line 2
  :5,8dp delete lines 5-8 and
    print current line
Un-do :u un-do last command
  :U un-do last line
File commands :f filename sets file name
  :! shell-command Insert shells output
Substitute :s/abc/xyz/ change, in the
    current line, the
    first abc to xyz.
  :1,$s/abc/xyz/g change, all "abc"s
    in file to "xyz"
move text :5,8m9 move lines 5-8 to
    after line 9
copy text :1,3t8 copy lines 1-3 to
    after line 8

Instructor: ltaber@pima.edu ** My new Home at GeoApps in Tucson ** The Pima College Site ** The Mad Dr. G.'s home page on phred.

4.7 Editor 3 - emacs4 Labs4.5 First Shell Script4.6 Editor - vi