Basic VI Commands

VI, visual interface, is a screen editor for the standard UNIX operating system.  VI editor is for more advanced UNIX users. Vi editing commands can also be used to edit a unix command line by first pressing the Esc key and then the “-” key to retrieve the last line. Pressing the “-” key again will retrieve the next previous command.  Pico editor is easy to learn and great for novice users.

Some VI commands are:

Command Function
vi invokes vi editor mode
write and quit
write file
:w filename
write to another file
overwrite existing file
quite without writing
quit file (discarding edits)
a append after cursor
A append text to end of current line
c0 change to the beginning of line
cc change the entire line
cw change to the end of a word
C or c$ change to end of line
dd delete line
dw delete word
D deletes from the cursor position to the end of the line
esc terminate insert mode
h left, one space
H top line of screen
i insert text at current cursor position
I insert text at beginning of line
j down, one line
k up, one line
l right, one space
L last line of screen
n repeat previous search
N repeat search in opposite direction
o open line below current line
O open line above current line
p puts the text that is in the buffer after the cursor position
r replace one character
s substitute
S substitute entire line
u undo last change
w move to the next word
x delete character under cursor
y yank – copy text into the buffer
Ctrl/B Scroll backward one screen
Ctrl/F Scroll forward one screen
Ctrl/G display current line number
/text search for text
/ repeat forward search
backspace move back one character
return move down one line
. (period) repeat last command
~ (tilde) change the case of a letter
^ or 0 (zero) move to beginning of line
$ (dollar) move to end of line

Moving Text

Use d or y commands to put text into the buffer.  Then use the p command to insert it back into the document.

Global Replacement

:[beginning line number,ending line number]s/old/new/[g][c]

:s/Andy/John/ Change the first occurrence of “Andy” in the current line to “John”
:s/Andy/John/g Change every occurrence of “Andy” in the current line to “John”
:1,5s/Andy/John/ Change the first occurrence of “Andy” in lines 1-5 to “John”
:7,23s/Andy/John/g Change every occurrence of “Andy” in lines 7-23 to “John”
:1,$s/Andy/John/ Change the first occurrence of “Andy” in all lines of the file to “John”
:%s/Andy/John/g Change every occurrence of “Andy” to “John” in all lines.
:%s/Andy/John/gc Change every occurrence of “Andy” to “John” in all lines with confirmation.

Configuration file

The file .exrc in your home directory is used by the vi editor to load commands every time the editor is started. Set commands can be entered into this file to configure your vi session the same way each time.  These same commands can be entered in the vi editor if they have a preceding colon (:) character.

set number|nonumber Determines whether line numbers appear before each line
set showmode|noshowmode Determines if the editing mode displays on the bottom right of the screen
set ic|noic Determines if pattern searches should ignore case

Leave a Reply

Your email address will not be published. Required fields are marked *