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

VI Editor#

Standard visual text editor available on all Unix / Linux computer systems. It also comes with Mac OS/X, and is available for Windows.

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

Commands are case sensitive so that j and J are different commands.

Commands (run from command mode)#

Saving, exiting, and file commands#

ZZsave and quit
:qquit without saving (only works of file had not been edited)
:q!quit without saving (even if file had been changed - lose changes)
:wwrite (save) file but don't quit
:w newnamewrite (save) file to new file named newname
:wqwrite (save) and quit
:r filenameread (insert) file filename into the current buffer after the current line

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

returnfirst character of next line
+first character of next line
-first character on previous line
w word right
b back a word
0 beginning of line
$ end of line
G goto end-of-file
1G goto beginning of file
15G goto line 15 (basically you can preced G with any line number)
Hhome - top of screen
Mmiddle of screen
Llast line on screen

Screen movement (scrolling)#

^f forward a page
^b back a page
z returnscroll current line to top-of-screen
z.scroll current line to middle-of-screen
z-scroll current line to bottom-of-screen

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 over-type (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 current 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

(Remember that any command can be preceded 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 yank (copy) line
yw yank (copy) word
y$ yank (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
:e!undo all changes since last save
. repeat last text-changing command
:.= display current line number
:set autoindentturn on auto-indent mode
:set noautoindentturn off auto-indent mode
^Lredraw screen
! cmdrun cmd in shell and insert result into buffer


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 without named variables.
  2. Save the current file with :w
  3. Edit another file with :e newfile
  4. Paste as normal

Search and Replace (Substitute)#

In what follows:

s substitute
g global
% all lines
c confirm

Commands:#

:s/OLD/NEW/ substitute the first occurrence of OLD with NEW on current line
:s/OLD/NEW/g substitute all occurrences of OLD with NEW on current line
:%s/OLD/NEW/g substitute all occurrences of OLD with NEW on all lines
:50,100s/OLD/NEW/g substitute all occurrences of OLD with NEW on lines 50-100
:%s/OLD/NEW/gc substitute all occurrences of OLD with NEW on all lines with confirmations

Multiple Files#

Vi can edit multiple files. When initially calling up VI, it may be called with multiple file names on the command line. VI supports the following commands to handle multiple files:

:nnext file is switched to
:wsave the current file
:e newfileedit file named newfile

Add new attachment

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