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.
- Copy the file ~cis137/flatpara.text
username@gort ~ $
cp ~cis137/flatpara.text .
(Did you notice the trailing period? You need it.)
If you are not using gort you can get the file
at:
http://uml.lt.tucson.az.us/hl2.2006-spring/files/flatpara.text.
The file is a paragraph from the book Flatland by Edwin A. Abbott.
- Bring up the file in
vi
.
username@gort ~ $
vi flatpara.text
- Insert
your name, the
current date, and TABER CIS137 as the first line of
the file.
- 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.
- With the "
:
" command move to line 11. :11<cr>
.
- 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.
- 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.
- 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.
- Go to line 4 of the file with a ":4". Once you have done this,
forward search
for "land" with the "/" command "/land".
The display should now highlight the first "/land"
found. Repeat the search twice with two "n" commands. Each time you type an
"n", the highlight and the cursor will advance. At this point take note
of the line that the cursor is on. Go back to line
four (4), the blank line, and
on this line place (insert) the line number that has the third "/land". The "
?
"
is used to search backwards. An "N
" searches again, but in the
other direction.
- Swap all adjacent vowels and enclose them in parentheses.
The command to do this.
:5,10s/\([aeiou]\)\([aeiou]\)/(\2\1)/g
This needs an explanation. The "5,10s
" indicates what area of the file.
Between the first two forward slashes "/
" are two regular
expressions. They are marked off with "\(
" and "\)
".
They both specify vowels, [aeiou]
.
These regular expressions are referred to later
by "\1
" and "\2
".
The "g
" at the end is for global, all of the occurrences on a line.
You may want to copy this command from the web page.
- Exit the
vi
editor with the command "ZZ
" (Upper case).
This will save your file.
-
Print out your resulting file.
username@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
Please turn your lab to Louis Taber or to Pima Community
College employee
in room A-115 of the Santa Rita Building.
Ask them to place it in the dark blue folder in
Louis Taber's mailbox.
vi
Text Editor
Starting | username@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: Louis Taber, ltaber at uml dot lt dot Tucson dot AZ dot us (520) 206-6850
My new web Home site in Cleveland, OH
The Pima Community College web site