0% found this document useful (0 votes)
14 views

Vi Commands

This document provides an overview of basic commands for the vi text editor in UNIX environments. It explains the two main modes of vi: command mode and insert mode. It also lists and describes important commands for moving the cursor, screen manipulation, adding/changing/deleting text, cutting and pasting, searching, and saving/reading files.

Uploaded by

klauzman
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Vi Commands

This document provides an overview of basic commands for the vi text editor in UNIX environments. It explains the two main modes of vi: command mode and insert mode. It also lists and describes important commands for moving the cursor, screen manipulation, adding/changing/deleting text, cutting and pasting, searching, and saving/reading files.

Uploaded by

klauzman
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Basic vi Commands

What is ?
The deIault editor that comes with the UNIX operating system is called (sual editor).
|Alternate editors Ior UNIX environments include pico and emacs, a product oI GNU.|
The UNIX vi editor is a Iull screen editor and has two modes oI operation:
Command mode commands which cause action to be taken on the Iile, and 1.
Insert mode in which entered text is inserted into the Iile. 2.
In the command mode, every character typed is a command that does something to the text Iile
being edited; a character typed in the command mode may even cause the editor to enter the
insert mode. In the insert mode, every character typed is added to the text in the Iile; pressing the
() key turns oII the Insert mode.
While there are a number oI commands, just a handIul oI these is usually suIIicient Ior
beginning users. To assist such users, this Web page contains a sampling oI basic
commands. The most basic and useIul commands are marked with an asterisk ( or star) in the
tables below. With practice, these commands should become automatic.
Both UNIX and case-sensitive. Be sure not to use a capital letter in place oI a
lowercase letter; the results will not be what you expect.
To Get Into and Out Of

To use vi on a Iile, type in vi filename. II the Iile named filename exists, then the Iirst page
(or screen) oI the Iile will be displayed; iI the Iile does not exist, then an empty Iile and screen are
created into which you may enter text.

vi filename edit starting at line 1

vi -r filename recover that was being edited when svstem crashed

Usually the new or modiIied Iile is saved when you leave . However, it is also possible to quit
without saving the Iile.
The cursor moves to bottom oI screen whenever a colon (:) is typed. This type oI
command is completed by hitting the <Return> (or <Enter>) key.
Basic vi Commands http://www.cs.colostate.edu/helpdocs/vi.html
1 oI 5 3/22/2011 9:18 AM

:x<Return> quit , writing out modified file to file named in original invocation

:wq<Return> quit , writing out modified file to file named in original invocation

:q<Return>
quit (or exit)

:q!<Return> quit even though latest changes have not been saved for this call
Moving the Cursor
Unlike many oI the PC and MacIntosh editors, the mouse does not move the cursor within the
vi editor screen (or window). You must use the the key commands listed below. On some UNIX
platIorms, the arrow keys may be used as well; however, since vi was designed with the Qwerty
keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange eIIects
in vi and should be avoided.
II you go back and Iorth between a PC environment and a UNIX environment, you may Iind that
this dissimilarity in methods Ior cursor movement is the most Irustrating diIIerence between the
two.
In the table below, the symbol ^ beIore a letter means that the <Ctrl> key should be held down
while the letter key is pressed.


" down-arrow]
move cursor down one line
` " up-arrow] move cursor up one line
`
<Backspace>
" left-arrow]
move cursor left one character
`

" right-arrow]
move cursor right one character
` '() move cursor to start of current line (the one with the cursor)

move cursor to end of current line

move cursor to beginning of next word

move cursor back to beginning of preceding word

:0<Return> !" move cursor to first line in file

:n<Return> " move cursor to line

:$<Return> G
move cursor to last line in file
Screen Manipulation
The Iollowing commands allow the editor screen (or window) to move up or down several
lines and to be reIreshed.

# move forward one screen


Basic vi Commands http://www.cs.colostate.edu/helpdocs/vi.html
2 oI 5 3/22/2011 9:18 AM

# move backward one screen

#$ move down (forward) one half screen

#
move up (back) one half screen

# redraws the screen

# redraws the screen, removing deleted lines


Adding, Changing, and Deleting Text
Unlike PC editors, you cannot replace or delete text by highlighting it with the mouse. Instead use
the commands in the Iollowing tables.
Perhaps the most important command is the one that allows you to back up and your last
action. UnIortunately, this command acts like a toggle, undoing and redoing your most recent
action. You cannot go back more than one step.

UNDO WHATEJER YOU JUST DID, a simple toggle


The main purpose oI an editor is to create, add, or modiIy text Ior a Iile.
Inserting or Adding Text
The Iollowing commands allow you to insert and add text. Each oI these commands puts the
editor into insert mode; thus, the key must be pressed to terminate the entry oI text and to
put the editor back into command mode.


insert text before cursor, until hit

% insert text at beginning of current line, until hit

append text after cursor, until hit

& append text to end of current line, until hit

'
open and put text in a new line below current line, until hit

( open and put text in a new line above current line, until hit
Changing Text
The Iollowing commands allow you to modiIy text.
`

replace single character under cursor (no needed)

replace characters, starting with current cursor position, until hit

change the current word with new text,


starting with the character under cursor, until hit

)
change words beginning with character under cursor, until hit,
e.g., changes 5 words
Basic vi Commands http://www.cs.colostate.edu/helpdocs/vi.html
3 oI 5 3/22/2011 9:18 AM

* change (replace) the characters in the current line, until hit

change (replace) the entire current line, stopping when is hit


))
change (replace) the next N lines, starting with the current line,
stopping when is hit
Deleting Text
The Iollowing commands allow you to delete text.
`
delete single character under cursor

)
delete N characters, starting with character under cursor

$ delete the single word beginning with character under cursor

$)
delete words beginning with character under cursor,
e.g., deletes 5 words

+ delete the remainder of the line, starting with current cursor position

$$
delete entire current line
)$$$)$
delete lines, beginning with the current line,
e.g., deletes 5 lines
Cutting and Pasting Text
The Iollowing commands allow you to copy and paste text.

,,
copv (vank, cut) the current line into the buffer
),,,), copv (vank, cut) the next N lines, including the current line, into the buffer

put (paste) the line(s) in the buffer into the text after the current line
Other Commands
Searching Text
A common occurrence in text editing is to replace one word or phase by another. To locate
instances oI particular sets oI characters (or strings), use the Iollowing commands.

-. search forward for occurrence of in text

/. search backward for occurrence of in text

move to next occurrence of search string

)
move to next occurrence of search string in opposite direction
Determining Line Numbers
Basic vi Commands http://www.cs.colostate.edu/helpdocs/vi.html
4 oI 5 3/22/2011 9:18 AM
Being able to determine the line number oI the current line or the total number oI lines in the Iile
being edited is sometimes useIul.

01
returns line number of current line at bottom of screen

1 returns the total number of lines at bottom of screen

#.
provides the current line number, along with the total number of lines,
in the file at the bottom of the screen
Saving and Reading Files
These commands permit you to input and output Iiles other than the named Iile with which you are
currently working.

:r filename<Return>
read file named and insert after current line
(the line with cursor)

:w<Return> write current contents to file named in original call

:w newfile<Return> write current contents to a new file named

:12,35w smallfile<Return>
write the contents of the lines numbered 12 through 35 to a new file
named

:w! prevfile<Return> write current contents over a pre-existing file named



Basic vi Commands http://www.cs.colostate.edu/helpdocs/vi.html
5 oI 5 3/22/2011 9:18 AM

You might also like