This is version . It is not the current version, and thus it cannot be edited.
Back to current version   Restore this version

VI Editor#

The VI editor has three main modes:

  • command mode
  • edit mode (insert or overtype text)
  • ex mode

When you first enter VI you are in command mode.

Edit mode is used to insert or overwrite new text. Command mode is used for almost everything else. Knowing which mode you are in at any point is critical. You can always hit the Esc key to go from edit mode to command mode. Hitting Esc when you are in command mode does nothing so if you are confused just hit the Esc key and you'll know you are in command mode.

Edit mode -> command mode = Esc

Command mode -> ex mode = :

Commands can be proceeded with a number. This causes that command to be executed the specified number of times. For example 5dd will delete 5 lines.

^ means control, so ^f means control-f

Command are case sensitive so thet j and J are different commands.

Commands (run from command mode)#

Saving and exiting#

ZZsave and quit
:qquit without saving (only works of file had not been edited)
:q!quit without saving (even if file had ben changed - lose changes)
:wsave file but don't quit

Cursor movement#

(The letters hjkl on the keyboard form a sort of arrow keys)

h left one character
j down one character
k up one character
l right one character

w word right
b back a word
0 beginning of line
$ end of line
^f forward a page
^b back a page
G goto end-of-file
1G goto beginning of file
15G goto line 15 (basically you can preceed G with any line number)

Text editing (all text entry except r ends by hitting the Esc key)#

i inserting text starting before the cursor position
a append text after the cursor position
r replace a single character
R replace or overtype (doesn't require Esc key to end)
A append starting at the end-of-line
o add text after the current line (open a line)
O add text before the current line
cw change word
S replace the cuuent line with entered text

Deletion#

x delete one character
dd delete current line
dw delete word
d$ delete to end-of-line
D delete to end-of-line

(Remeber that any command can be preceeded by a number so things line 5dd means delete 5 lines, 7x means delete 7 characters)

Searching#

/xxxxx<return-key> search forward for xxxxx
?xxxxx<return-key> search backwards for xxxxx
n repeat previous search

Cutting, copying and pasting#

Cutting is done with the delete commands listed above.

yy copy line
yw copy word
y$ copy to end-of-line
p Put or paste. Basically it pastes the last line (or group of lines) after the current position.
kp Useful after a delete if you didn't really want to delete but just copy the text. The kp puts it back where it was but remembers what was deleted.

Remember that these commands can be proceeded with a number to get a group of words or a group of lines.

Misc#

J join the next line to the end of the current line
u undo last edit
:.= display current line number

Advanced Commands#

Using variables (or named buffers)#

Copying and deleting text into variables so that you can have several pieces of text you are moving around. It's like being able to delete or copy text into a variable that you name, and then being able to pasted text that was put into a variable.

Variable names are always single, lower-case letters so that you can only have 26 variables.

In what follows X will be used to signify the variable name you are using. So, for example when you see X in a command below you will really put in your variable name (a through z).

All copy "y" and delete "d" commands can be proceeded by "X
That is double quote and your variable name. So, for example

"wdd would delete the current line and put it in the variable named "w".
"v5dd would delete the next 5 lines and put it into the variable named "v"
"r3yw would copy 3 words and put them in the variable named "r"
"vp would paste the contents of the variable named "v"

Marking positions#

Marking a position allows you to save your current position into a variable. You can later cause the system to return to that exact position.

mX save the current position in variable named at X
`X return to position saved at variable named X

for example:

mh save my current position in variable named "h"
`h return to position saved in variable named "h"

Copying text between two files#

  1. Use any of the delete or copy (yank) commands with or wothout named variables.
  2. Save the current file with :w
  3. Edit another file with :e newfile
  4. Paste as normal

Search and Replace#

In what follows:

s substitute
g global
% all lines
c confirm

Commands:#

:s/OLD/NEW/ replace the first occurance of OLD with NEW on current line
:s/OLD/NEW/g replace all occurances of OLD with NEW on current line
:%s/OLD/NEW/g replace all occurances of OLD with NEW on all lines
:50,100s/OLD/NEW/g replace all occurances of OLD with NEW on lines 50-100
:%s/OLD/NEW/gc replace all occurances of OLD with NEW on all lines with confirmations

Add new attachment

Only authorized users are allowed to upload new attachments.
« This particular version was published on 18-Sep-2012 15:46 by BlakeMcBride.