Basic Vi Commands
Basic Vi Commands
3/11/2015, 00:22
Basic vi Commands
What is vi?
The default editor that comes with the UNIX operating system is called vi (visual editor).
[Alternate editors for UNIX environments include pico and emacs, a product of GNU.]
The UNIX vi editor is a full screen editor and has two modes of operation:
1. Command mode commands which cause action to be taken on the file, and
2. Insert mode in which entered text is inserted into the file.
In the command mode, every character typed is a command that does something to the text file
being edited; a character typed in the command mode may even cause the vi editor to enter the
insert mode. In the insert mode, every character typed is added to the text in the file; pressing the
<Esc> (Escape) key turns off the Insert mode.
While there are a number of vi commands, just a handful of these is usually sufficient for
beginning vi users. To assist such users, this Web page contains a sampling of basic vi commands.
The most basic and useful commands are marked with an asterisk (* or star) in the tables below.
With practice, these commands should become automatic.
NOTE: Both UNIX and vi are case-sensitive. Be sure not to use a capital letter in place of a
lowercase letter; the results will not be what you expect.
To Exit vi
Usually the new or modified file is saved when you leave vi. However, it is also possible to quit vi
without saving the file.
https://www.cs.colostate.edu/helpdocs/vi.html
Page 1 of 5
Basic vi Commands
3/11/2015, 00:22
Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This type of command
is completed by hitting the <Return> (or <Enter>) key.
* :x<Return> quit vi, writing out modified file to file named in original invocation
:wq<Return> quit vi, writing out modified file to file named in original invocation
:q<Return> quit (or exit) vi
* :q!<Return> quit vi even though latest changes have not been saved for this vi call
j or <Return>
[or down-arrow]
* k [or up-arrow]
h or <Backspace>
*
[or left-arrow]
l or <Space>
*
[or right-arrow]
* 0 (zero)
*$
move cursor to start of current line (the one with the cursor)
move cursor to end of current line
w
b
Screen Manipulation
https://www.cs.colostate.edu/helpdocs/vi.html
Page 2 of 5
Basic vi Commands
3/11/2015, 00:22
The following commands allow the vi editor screen (or window) to move up or down several lines
and to be refreshed.
^f move forward one screen
^b move backward one screen
^d move down (forward) one half screen
^u move up (back) one half screen
^l redraws the screen
^r redraws the screen, removing deleted lines
Changing Text
The following commands allow you to modify text.
*r
https://www.cs.colostate.edu/helpdocs/vi.html
Page 3 of 5
Basic vi Commands
3/11/2015, 00:22
replace characters, starting with current cursor position, until <Esc> hit
cw
cNw
change N words beginning with character under cursor, until <Esc> hit;
e.g., c5w changes 5 words
change (replace) the characters in the current line, until <Esc> hit
cc
change (replace) the entire current line, stopping when <Esc> is hit
Ncc or cNc
change (replace) the next N lines, starting with the current line,
stopping when <Esc> is hit
Deleting Text
The following commands allow you to delete text.
*x
Nx
dw
dNw
D
* dd
Ndd or dNd
Nyy or yNy copy (yank, cut) the next N lines, including the current line, into the buffer
p
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
of particular sets of characters (or strings), use the following commands.
https://www.cs.colostate.edu/helpdocs/vi.html
Page 4 of 5
Basic vi Commands
3/11/2015, 00:22
^g
provides the current line number, along with the total number of lines,
in the file at the bottom of the screen
:w<Return>
:w newfile<Return>
:12,35w smallfile<Return>
:w! prevfile<Return>
:r filename<Return>
Comments:
https://www.cs.colostate.edu/helpdocs/vi.html
Page 5 of 5