VI Editor: Moving The Cursor
VI Editor: Moving The Cursor
There are two main modes to the vi editor: text insertion mode and vi command mode. Unlike
most other editors, the vi editor does not start you out in text insertion mode; you start in the vi
command mode. You can move back and forth between the two using the escape key.
vi command mode will allow you to move around in your text, enter the text insertion mode,
replace text, move/copy blocks of text, etc. There is also a large group of commands that can be
entered from the colon (:) prompt, such as: leaving the vi editor (with or without saving your
text), searching for text strings, copying blocks to text, etc. Please note: You do not hit the
return key after you enter a vi command.
text insertion mode will only allow you to insert text. There are several ways to get into text
insertion mode from the vi editor, depending on where you want to start inserting text. The only
way to get out of text insertion mode and back into command mode is by use of the “esc” key.
Please note: While you are in text insertion mode you cannot move up/down lines to insert text.
If you need to add text to another location than you cursor is located, you must leave text
insertion mode (“esc”), move to the new location, and re-enter text insertion mode.
Deleting Text
1
Line Move and Copy Commands :set nu display line numbers
:set nonu remove line numbers
yy yank line to buffer
yw yank word to buffer Saving Text and/or Leaving vi
dd delete line to buffer
dw delete word to buffer :w write
p put buffer below cursor :wq write and quit
P put buffer above cursor :x write and quit
“cy yank buffer to c :q quit
“cp put buffer c :q! quit and ignore changes
ZZ write if changed and quit
Searching for a Character String :r filename read in file filename
:w filename write to file filename
/string forward search for string :n,m w filename write lines n through m
?string backward search for string to filename
n repeat last search in same direction
N repeat last search in reverse direction Moving a Section of Text: Cut and Paste
Global search and replace a: Move the cursor to the beginning of the
block to be moved
:g/string1/s//string2/g b: Use ndd to delete n lines.
replaces all occurrences c: Move the cursor to the place you want to
of string1 with string2 insert the cut text.
d: Use p to paste below the cursor or P to paste
Miscellaneous Commands above the cursor.
Repeating a Command
Undoing Edits