Vi Commands Cheat Sheets
Vi Commands Cheat Sheets
0
March 22, 2005
:set showmode
When you open Vi, you can’t just start typing a thesis because Vi has different modes such as APPEND MODE, INSERT MODE,
REPLACE MODE, and COMMAND MODE. So to do certain actions, you must first enter the appropriate mode. See “MODES” for
more in-depth information.
The final two pages of this document is a list of Vi core commands. These are the bare essentials of Vi compressed onto two
pages.
Table of Contents
STARTING VI (VI IS CASE SENSITIVE!!! SO MAKE SURE CAPS LOCK IS OFF.) ....................................................................................... 2
ENDING VI (SAVING, EXITING, ETC.) ............................................................................................................................................................ 2
STATUS (LINE NUMBERS, ETC.) ..................................................................................................................................................................... 3
MOVING (THESE WILL TELL YOU HOW TO GET THE CURSOR WHERE YOU WANT IT–FAST.)......................................................................... 3
MOVING THE CURSOR .............................................................................................................................................................................. 3
MOVING THE SCREEN............................................................................................................................................................................... 4
MODES (ADDING AND/OR REPLACING TEXT)............................................................................................................................................... 5
SEARCHING (FIND AND/OR REPLACE WHAT YOU NEED) ........................................................................................................................... 6
EDITING (DELETING, COPYING, PLACING TEXT, JOINING LINES, CHANGING TEXT, REPEAT COMMAND, UNDO COMMAND) ........................ 7
SHELL WORKS (COMMANDS THAT EXECUTE OUTSIDE OF THE VI PROGRAM.) ......................................................................................... 9
MACROS, SUBS, ABBREVIATIONS (MACRO FUNCTIONS, SUBSTITUTION STRINGS, AND ABBREVIATIONS) ........................................ 10
Setting Options ............................................................................................................................................................................................ 10
VI CORE COMMANDS (THE MINIMUM FUNCTIONS) ............................................................................................................................... 12
TECHREPUBLIC ................................................................................................................................................................................................ 14
Additional resources ................................................................................................................................................................................... 14
Version history ............................................................................................................................................................................................. 14
Tell us what you think ................................................................................................................................................................................. 14
Page 1
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
UNIX Vi Commands Cheat Sheets
Vi filename file2 ... Edits filename and then edits file2 and so on. After saving file1 enter :n for the next file.
Vi +/string file Edits file and places cursor at the first line containing string.
:q Quits Vi, saving changes before leaving. (You may be prompted to save.)
Page 2
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
UNIX Vi Commands Cheat Sheets
CTRL-g Shows filename, current line number, total lines in file, and % of file location.
:l (letter “l”) Displays tab (^l) backslash (\) backspace (^H) newline ($) bell (^G) formfeed (^L^) of
current line.
MOVING (These will tell you how to get the cursor where you want it–fast.)
MOVING THE CURSOR
Command Result
arrow keys These do work, but they may be too slow on big files.
w or W Moves to the start of the next word in the line; W ignores punctuation.
b or B Moves to the start of the previous word in the line; B ignores punctuation.
Page 3
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
UNIX Vi Commands Cheat Sheets
Command Result
e or E Moves to the end of the next word in the line; E ignores punctuation.
z <CR> z–carriage return makes the current line the top line on the page.
Page 4
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
UNIX Vi Commands Cheat Sheets
itext ESC insert mode—You can start typing and it will insert text before the letter your cursor currently
highlights until ESC is used.
Itext ESC insert mode—Same as (i) except it will insert text before the first nonblank character on the line
until ESC is used.
atext ESC append mode—Allows you to insert text after the letter your cursor currently highlights until
ESC is used.
Atext ESC append mode—Same as (a) except it will insert text at the end of the line until ESC is used.
rchar ESC replace mode—Replaces the currently highlighted character with char until ESC is used.
Rtext ESC replace mode—Same as (r) except it will overwrite until ESC is used.
o Opens new line below the current line and insert until ESC is used.
O (letter “O”) Opens new line above the current line and insert until ESC is used.
CTRL–v char While inserting, ignores special meaning of char (e.g., for inserting characters like ESC and
CTRL) until ESC is used.
:r file Reads file and inserts it after current line.
Page 5
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
UNIX Vi Commands Cheat Sheets
:n,ms/str1/str2/opt Searches from n to m for str1; replaces str1 to str2; using opt–opt can be g for global change,
c to confirm change (y to acknowledge, <CR> to suppress), and p to print changed lines.
& Repeats last :s command.
:g/str1/s/str2/str3/ Finds the line containing str1, replaces str2 with str3.
Page 6
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
UNIX Vi Commands Cheat Sheets
EDITING (Deleting, copying, placing text, joining lines, changing text, repeat
command, undo command)
Command Result
nx Deletes n characters starting with current; omitting n deletes current character only.
ndw Deletes the next n words starting with current; omitting n deletes the current word only.
ndb Deletes the previous n words starting with current; omitting n deletes the previous word
only.
ndd Deletes n lines beginning with the current line; omitting n deletes the current line only.
dcursor_cmd Deletes everything included in the cursor command (e.g., dG would delete from current
position to the end of the file, and d4 would delete to the end of the fourth sentence).
nyy or nY Places n lines in the buffer–copies; omitting n copies only the current line to the buffer.
ycursor_cmd Copies from cursor to cursor_cmd (e.g., yG copies current line to the last line in the file to the
buffer).
“(a-z)nyy or Copies or cuts (deletes) n lines into a named buffer a through z; omitting n works on
current line.
“(a-z)ndd
p Pastes copied text after cursor; also prints last deleted text.
P Pastes copied text before the cursor; also prints last deleted text.
“(a-z)p or Pastes text from a named buffer a through z after or before the current line.
“(a-z)P
nJ Joins the next n lines together; omitting n joins the beginning of the next line to the end of the
current line.
stext ESC Substitutes text for the current character until ESC is used.
Page 7
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
UNIX Vi Commands Cheat Sheets
Command Result
S or cc text ESC Substitutes text for the entire line until ESC is used.
Ctext ESC Changes rest of the current line to text until ESC is used.
ccursr_cmdtext Changes to text from current position to cursr_cmd until ESC is used.
ESC
“np Retrieves the last nth delete (last 9 deletes are kept in a buffer).
“1pu.u. Scrolls through the delete buffer until the desired delete is retrieved (repeat u.).
n<< or n>> Shifts n lines left or right (respectively) by one shift width; omitting n shifts one line.
< or > Use with cursor command to shift multiple lines left or right.
Page 8
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
UNIX Vi Commands Cheat Sheets
:! cmd Executes shell command cmd; you can add these special characters to indicate:
% name of current file
# name of last file edited
!! cmd Executes shell command cmd, places output in file starting at current line.
:w !cmd Sends currently edited file to cmd as standard input and execute cmd.
:so file Reads and executes commands in file (file is a shell script).
!cursor_cmd cmd Sends text from current position to cursor_cmd to shell command cmd.
!}sort <CR> Sorts from current position to end of paragraph and replaces text with sorted text.
Page 9
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
UNIX Vi Commands Cheat Sheets
address One line number or two line numbers separated by a comma. A. represents the current line, $
represents the last line, and % represents the entire file.
search-string A regular expression that can be a simple string of characters.
Setting Options
:map key Defines key to run cmd_seq when :map Displays all created macros on
pressed. status line.
cmd_seq
:unmap key Removes macro definition for key. :ab str string When str is input, replaces it with
string.
:ab Displays all abbreviations. :una str Unabbreviates str.
Options given are default. To change them, enter type :set option to turn them on or :set nooption to turn them off.
To make them execute every time you open Vi, create a file in your HOME directory called .exrc and type the options without the colon (:) preceding
the option.
:set all Prints all options to the screen. :set nooption Turns off option.
:set ic Ignores case when searching. :set dir=tmp Sets directory or buffer file.
:set lisp Modifies brackets for Lisp :set magic Allows pattern matching with special
compatibility. characters.
Page 10
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
UNIX Vi Commands Cheat Sheets
:set nu Shows line numbers. :set opt Speeds output; eliminates automatic
RETURN.
:set prompt Prompts for command input with :. :set re Simulates smart terminal on dumb
terminal.
:set report Indicates largest size of changes :set ro Changes file type to “read only.”
reported on status line.
:set scroll=n set n lines for CTRL–d and z :set set shell escape (default is /bin/sh)
sh=shell_path
:set showmode Indicates input or replace mode at :set sw=n Sets shift width to n characters.
bottom.
:set term Prints terminal type. :set terse Shorten messages with terse.
:set timeout Eliminates one-second time limit for :set tl=n Sets significance of tags beyond n
macros. characters (0 means all).
:set ts=n Sets tab stops to n for text input. :set wa Inhibits normal checks before write
commands.
:set warn Warns “no write since last change.” :set window=n Sets number of lines in a text
window to n.
:set wm=n Sets automatic wraparound n
spaces from right margin.
Page 11
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
UNIX Vi Commands Cheat Sheets
:q Quits Vi, saving changes before leaving. (You may be prompted to save.)
MOVING (These will tell you how to get the cursor where you want it–fast.)
MOVING THE CURSOR
arrow keys These do work, but they may be too slow on big files.
hjkl Left, down, up, and right, respectively.
CTRL-d Moves forward ½ screenful.
CTRL-f Moves forward 1 whole screenful.
Atext ESC append mode—Same as (a) except it will insert text at the end of the line until ESC is used.
rchar ESC replace mode—Replaces the currently one highlighted character with char until ESC is used.
Rtext ESC replace mode—Same as (r) except it will overwrite until ESC is used.
o Opens new line below the current line and insert until ESC is used.
O (letter “O”) Opens new line above the current line and insert until ESC is used.
Page 12
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
UNIX Vi Commands Cheat Sheets
Command Result
fchar | Fchar Searches forward in current line to char. | Searches backward in current line to char.
/str <CR> | ?str Finds forward to str. | Finds in reverse for str.
<CR>
EDITING (Deleting, copying, placing text, joining lines, changing text, repeat command, undo command)
CTRL–h or While inserting, deletes previous character.
Backspace
CTRL–w While inserting, deletes previous word
nx Deletes n characters starting with current; omitting n deletes current character only.
ndw Deletes the next n words starting with current; omitting n deletes the current word only.
ndd Deletes n lines beginning with the current line; omitting n deletes the current line only.
nJ Joins the next n lines together; omitting n joins the beginning of the next line to the end of the current line.
u Undoes last command.
U Restores current line to its original state.
n | N Repeats last / or ? search command. | Repeats, in reverse order, last / or ? search command.
OPTIONS
Command Result Command Result
Options given are default. To change them, enter type :set option to turn them on or :set nooption to turn them off. To make them execute every time
you open Vi, create a file in your home directory called .exrc and type the options without the colon (:) preceding the option.
:set all Prints all options to the screen. :set nooption Turns off option.
:set ic Ignores case when searching. :set magic Allows pattern matching with special characters.
:set nu Shows line numbers. :set ro Changes file type to “read only.”
:set scroll=n Sets n lines for CTRL–d and z. :set sw=n Sets shift width to n characters.
:set showmode Indicates input or replace mode at bottom. :set window=n Sets number of lines in a text window to n.
:set ts=n Sets tab stops to n for text input. :set wm=n Sets automatic wraparound n spaces from right
margin.
Page 13
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
UNIX Vi Commands Cheat Sheets
TechRepublic
Additional resources
• Use vi to edit files in a UNIX environment (Article)
• 15 tips for a more pleasant Vi experience (Download)
• Vi for programmers, part 1 (Article)
• Vi for programmers, part 2 (Article)
Version history
Version: 2.0
Reformatted for easier reading and printing
Published: March 22, 2005
Thanks!
Page 14
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.