ITSC 1307 Fundamentals of UNIX

advertisement
ITSC 1307 Fundamentals of UNIX
Instructor: Prof. Michael P. Harris, CCNA, CCAI
VI_Quick-Notes.doc
(Rev. 02.11.26)
Quick Notes: The VI editor (the basics)
Many editors are available for the UNIX system. Vi is a popular text editor on the
UNIX system with which you can create and edit files. Vi differs from many of the
standard UNIX editors (like ed or sed) in that it offers full screen editing yet still has
both a command mode and a text insertion/edit mode.
To create or edit a file you type "vi filename" at the UNIX prompt. This puts you into
the vi editor's command mode. To create text you need to be in the text insertion
mode. Tapping the letters a(append), i(insert), or o(open) will put you there. You
can now type in your text. To leave the text insertion mode tap the [ESC] key.
In the command mode you can move through the document, delete and correct typos,
retrieve another document or quit and save the text. To write (save) your work and quit
enter a ":" (colon), which will move you to the bottom of the screen, and then you type
"wq". This is equal to write (the text to a file and) quit. You can also quit and not save
the file by tapping [ESC] and typing ":q!". This will leave vi without saving the file.
You will probably prefer your word processor for most of your writing but you will find
vi invaluable for editing files in UNIX. The following is a cheat sheet for the vi editor.
To start using vi with a file called filename enter: vi filename.
Cursor Movement:
Arrow keys move the cursor up, down, left, or right if your terminal configuration is set
up properly. If the arrow keys do not work use the alternate keyboard commands:
[Enter]
- (minus)
0 (zero)
$ (dollar)
move
move
move
move
cursor
cursor
cursor
cursor
down
up
to the beginning of the line
to the end of the line
h
j
k
l
move
move
move
move
cursor
cursor
cursor
cursor
left
down
up
right
^F
^B
^D
^U
^R
scroll Forward a full screen
scroll Backward a full screen
scroll Down half a screen
scroll Up half a screen
clear and Redraw the screen
(* see your keyboard layout *)
(note: ^ means to hold down the <CTRL> key)
Quick Notes: The VI editor (the basics)
G
nG -or- :n
M
H
L
w
b
Page 2
Goto the end of the file
position the cursor at (Goto) line n
position the cursor at the middle of the screen
position the cursor at the top of the screen
position the cursor at the bottom of the screen
move cursor to the beginning of the next word
move cursor backward to the beginning of a word
To enter vi text insertion/edit mode:
a
append new text after the current character
i
insert new text before the current character
o
open a new line after the current line
A
Append new text to the end of the line
O
Open a line before the current line
R
oveRtype characters
Vi editing commands:
x
delete the current character
nx
delete n characters
rc
replace the current character with character c
dd
delete the current line (into the clipboard)
ndd
delete n lines (into the clipboard)
yy
yank (copy) the current line into the clipboard
nyy
yank (copy into the clipboard) n lines
p
paste clipboard text after the current line
Vi search commands:
/string
search forward for character string
?string
search backward for character string
Vi write (save) and quit commands:
:w name
write buffer contents to a file called name
:wq
write changes made to the file and quit
:q
quit (prompt for changes)
:q!
forcefully quit vi not writing any changes
Copying/Moving a block of text in vi:
1. Position the cursor at the beginning line of a block of text.
2. Type nyy to yank (copy) or ndd (delete) n lines. (ex: 10yy will yank 10 lines)
3. Position cursor at the line that precedes where the block of text is to be copied.
4. Press p to paste the clipboard contents after the current line.
Help with vi:
1. For online help on vi, at your UNIX prompt type "man vi".
2. To start the CAI online course for vi type "learn vi".
Download