The vi editor
Last updated: Sunday 3rd April 2022, 13:08 PT, AD
The vi editor
-------------
vi is a modal editor, meaning it functions in using modes.
vi has three modes:
------------------
Command mode
- where editing commands such as dd to delete the current line are entered.
Pressing the Esc key turns on command mode.
Input mode
- where text can be entered into any line of text.
Input mode is turned on (the only time text entry is permitted)
by giving the a, o or i commands in command mode.
Status line mode
- is used to enter long commands at the bottom of the screen.
To enter status mode, press Esc followed by :
then type further instructions such as
wq to write (save) the current file and quit.
Short vi Command List
---------------------
vi filename open the vi editor with filename
Cursor ops Up, Down, Left < Right arrow keys as required
:w Write
:wq Write and quit
:q Quit without saving if no changes have been made
:q! Quit without save, even after change to file
Esc a Insert characters to right
Esc A Insert characters at end-of-line
Esc i Insert left of cursor
Esc I Insert at beginning of line
Esc o Insert line below cursor
Esc O Insert line above cursor
Esc x Delete text at cursor
Esc dw Delete word right
Esc DD Delete line
Esc D Delete part of line to right of cursor
For a full command list type man vi
Search/Replace
--------------
To search in vi first make sure you are in command mode (press Esc).
Press Esc and then press / followed by your search phrase.
To do a case insensitive search,
first go into command mode and set case insensitive mode. :set ic
Replacing text can be done one of many ways.
To replace all text in the file use :%s/old/new/g
If you only want to replace text on a block of text you can use
:10,20s/old/new/g to replace all text between lines 10 to 20.
In addition, the g at the end of the line
tells vi to replace all occurrences in the line, not just the first one.
Cut/Paste/Buffers
-----------------
There are two basic methods to cut and paste in vi.
You can use the global buffer, for example,
by pressing YY to "Yank" the current line into the global buffer.
Then you can paste below the current line using p.
You could, of course, add a number of lines to yank into the buffer.
30Y will Yank 30 lines into the buffer.
Using named buffers or registers allows you to have more then one in memory at a time.
To yank into the buffer A, we would use "a10Y to grab 10 lines into buffer A.
Then to paste them, a simple "ap would paste them into the document.
Buffers are not case sensitive so A is the same as 'a'.
vi calling vi (for text input) without file-specification
vi fn calling vi to edit a file fn
vi -R fn calling vi in read-only mode on file fn
1.example: show/read file .exrc
vi -R .exrc
2.example: edit all files corresponding to filenames *.da??
vi *.da??
This is a small file called 'vi.prac'.
This is the second and last line.
^
^
^
^
^
^
^
^
^
^
^
^
^
^
^
^
"vi.prac" 2 lines 103 characters
A typical vi screen
vi modes
command
Normal and initial state.
[ESC] cancels partial command
insert entered by the following commands: a, A, i, I, o, O, c, C, s, S, R.
Terminates with [ESC] (or ^C).
status line entered by :, /, ? or !.
Input is read and echoed at the bottom of the screen.
Commands executed by [RETURN] or [ESC], terminated by ^C.
Entering and leaving vi
vi file edit file
vi +n file edit starting at line n
vi + file edit starting at end
vi +/RE/ file edit starting at RE
view file read only mode
ZZ exit from vi, saving changes (same as :wq)
^Z stop vi process, for later resumption
File manipulation
The following are all status line mode commands,
so must be preceded by a colon.
w save changes, continue editing
wq save and quit
q quit
q! quit, discarding changes
w file write to file
r file insert from file (at cursor position)
e file edit file
e! re-edit current file, discarding changes
w! file overwrite file
! command execute shell command, then return
f show current file and line
Some simple commands
The following are examples of some compound commands,
using the operators listed later.
dw delete word
dd delete line
ndd delete n lines
de delete word leaving punctuation
xp transpose characters
cw text [ESC] change word to text
Positioning within the file
^F forward one 'page'
^B back one 'page'
^D scroll down half 'page'
^U scroll up half 'page'
nG go to line n (last line default)
/RE/ go to next occurrence of RE
% find matching bracket
Marking
`` return to previous cursor position
mx mark position with x
`x go to mark x
Line positioning
j next line, same column (same as down arrow)
k previous line, same column (same as up arrow)
H top line of window (home)
M middle line of window
L last line of window
+ next line, at first non-white character
- previous line, at first non-white character
[RETURN] same as +
Character positioning
l move cursor right
h move cursor left
0 beginning of line
$ end of line
^ first non-white in line
[SPACE] forward (same as right arrow)
fx find x forwards in current line
Fx find x backwards in current line
; repeat last find command forwards
: repeat last find command backwards
n| go to column n
Words, sentences, paragraphs
w forward to start of next word (delimited by non-alphanumeric character)
b back to start of last word
e forward to end of next word
W as w, with word delimited by blank only
B as b, with word delimited by blank only
E as e, with word delimited by blank only
) forward to start of next sentence
( Back to start of next sentence
} Forward to start of next sentence
{ Back to start of last sentence
Corrections during insert
H erase last character (or your usual delete key)
W erase last word
[ESC] ends insert; back to command mode
\ escape character
C ends insert
Insert and replace commands
a append after cursor
i insert before cursor
A append at end of line
I insert before first non-blank
o open line below current line
O open line above current line
[ESC] ends insert; back to command mode
rx replace single character with x
R string [ESC] replace characters by string
Operators
The following can be doubled to apply to a line
and also preceded by a number to indicate a number of lines.
They can be combined with positional commands
(e.g.d$ to delete to end of line.)
d delete
c change
y yank
Miscellaneous operations
x delete character
X delete character to left of cursor
C change rest of line (same as c$)
D delete rest of line (same as d$)
J join lines
i [CR] [ESC] split line at cursor position
Y yank (paste) lines
Yank and put
p put back after cursor
P put back before cursor
"xp put from buffer x
"xy yank to buffer x
"xd delete to buffer x
Undo, redo and retrieve
u undo last change
U restore current line
. repeat last command
"np retrieve nth last delete
Copying Text with Yank
If you want to make a duplicate copy of existing text, you can use the yank command:
yw copies a word into the buffer
4yw copies 4 words
yy copies one line into the buffer
4yy will copy 3 lines
Once the desired text is yanked,
place the cursor in the spot in which you wish to insert the text
then use the put command to insert the contents of the buffer:
p for line below
P for line above
manipulate the behaviour of vi
/usr/bin/vi editor
/etc/d.exrc (default) resource-file with editor settings
$HOME/.exrc additive resource-file
****************************************
********************************************************************************************************