Monday, 29 August 2011

VI Essentials

Sometimes you visit a client and you have got only access to the vi editor. While this editor can be frustrating to learn especially for Windows and Mac users I believe that learning or referring to the below commands might become handy to any IT professional.

Starting/Saving
vi [+n] <file_name>
Open filename positioning the cursor at line number n
:wq
Save the file to disk and quit
:q!
Quit without saving any changes


Moving Cursor
h (or Left arrow key)
Left
l  (or Right arrow key)
Right
k (or Up arrow key)
Up
j  (or Down arrow key)
Down
G
End of file
^
Beginning of current line
$
End of current line


Searching Text
/string
Search down for string
?string
Search up for string
n
Repeat last search
:%s/old-string/new-string/g
Search and replace old-string with new-string


Copying and Pasting Text
[n]yy
Copying n lines into buffer
p
Paste the buffer after the current line


Inserting and Deleting Text
i
Insert starting left of cursor
x
Delete single character
[n]dd
Delete n lines
u
Undo last change


Directives
:set nu / :set nonu
Turn on line numbering / Turn off line numbering
:set ic / :set noic
Ignore case when searching / Ignore case off