VI cheat sheet
VI cheat sheet
Almost all deletion commands are performed by typing d followed by a motion. For /string Search forward for string
example, dw deletes a word. A few other deletes are: ?string Search back for string
x Delete character to the right of cursor n Search for next instance of string
Deletes n characters starting with current; omitting n deletes current N Search for previous instance of string
nx
character only
% Searches to beginning of balancing ( ) [ ] or { }
X Delete character to the left of cursor
fc Searches forward in current line to char
Deletes previous n characters; omitting n deletes previous character
nX Fc Searches backward in current line to char
only
D Delete to the end of the line tc Searches forward in current line to character before char
d$ Deletes from the cursor to the end of the line Tchar Searches backward in current line to character before char
dd or :d Delete current line ?str Finds in reverse for str
ndw Deletes the next n words starting with current
ndb Deletes the previous n words starting with current :set ic Ignores case when searching
ndd Deletes n lines beginning with the current line :set noic Pays attention to case when searching
:n,md Deletes lines n through m Searches from n to m for str1; replaces str1 to str2; using opt-opt can be
:n,ms/str1/
Deletes everything included in the Motion Command (e.g., dG g for global change, c to confirm change (y to acknowledge, to
str2/opt
dMotion_cmd would delete from current position to the end of the file, and d4 suppress), and p to print changed lines
would delete to the end of the fourth sentence). & Repeats last :s command
"np Retrieves the last nth delete (last 9 deletes are kept in a buffer) :g/str/cmd Runs cmd on all lines that contain str
Scrolls through the delete buffer until the desired delete is retrieved :g/str1/s/
"1pu.u. Finds the line containing str1, replaces str2 with str3
(repeat u.) str2/str3/
:v/str/cmd Executes cmd on all lines that do not match str
– Yanking Text –
, Repeats, in reverse direction, last / or ? search command
yy Yank the current line
:y Yank the current line – Replace –
nyy or nY Places n lines in the buffer-copies The search and replace function is accomplished with the :s command. It is commonly
Copies everything from the curser to the Motion Command (e.g., used in combination with ranges or the :g command (below).
yMotion_cmd yG would copy from current position to the end of the file, and y4 :s/pattern/string/flags Replace pattern with string according to flags.
would copy to the end of the fourth sentence)
g Flag - Replace all occurences of pattern
"(a-z)nyy or "(a- Copies or cuts (deletes) n lines into a named buffer a through z;
z)ndd omitting n works on current line c Flag - Confirm replaces.
& Repeat last :s command
– Changing text –
– Regular Expressions –
The change command is a deletion command that leaves the editor in insert mode. It is
performed by typing c followed by a motion. For example cw changes a word. A few . (dot) Any single character except newline
other change commands are: * zero or more occurances of any character
C Change to the end of the line [...] Any single character specified in the set
cc or S Change the whole line until ESC is pressed [^...] Any single character not specified in the set
xp Switches character at cursor with following character \< Matches beginning of word
stext Substitutes text for the current character until ESC is used \> Matches end of word
cwtext Changes current word to text until ESC is used ^ Anchor - beginning of the line
Ctext Changes rest of the current line to text until ESC is used $ Anchor - end of line
Changes to text from current position to Motion \< Anchor - begining of word
cMotion_cmd
Command until ESC is used
\> Anchor - end of word
Shifts the line left or right (respectively) by one shift
<< or >> \(...\) Grouping - usually used to group conditions
width (a tab)
Shifts n lines left or right (respectively) by one shift \n Contents of nth grouping
n<< or n>>
width (a tab) Escapes the meaning of the next character (e.g., \$ allows you to search for
\
<Motion_cmd or Use with Motion Command to shift multiple lines left or $)
>Motion_cmd right \\ Escapes the \ character
Named buffers may be specified before any deletion, change, yank or put command. [A-Z] The SET from Capital A to Capital Z
The general prefix has the form "c where c is any lowercase character. for example, [a-z] The SET from lowercase a to lowercase z
"adw deletes a word into buffer a. It may thereafter be put back into text with an [0-9] The SET from 0 to 9 (All numerals)
appropriate "ap.
[./=+] The SET containing . (dot), / (slash), =, and +
– Markers – The SET from Capital A to Capital F and the dash (dashes must be
[-A-F]
specified first)
Named markers may be set on any line in a file. Any lower case letter may be a [0-9 A-Z] The SET containing all capital letters and digits and a space
marker name. Markers may also be used as limits for ranges.
[A-Z][a-zA- In the first position, the SET from Capital A to Capital Z
mc Set marker c on this line Z] In the second character position, the SET containing all letters
`c Go to beginning of marker c line. [a-z]{m} Look for m occurances of the SET from lowercase a to lowercase z
'c Go to first non-blank character of marker c line. Look for at least m occurances, but no more than n occurances of the
[a-z]{m,n}
SET from lowercase a to lowercase z
– VI Settings – – Count –
Note: Options given are default. To change them, enter type :set option to turn them on Nearly every command may be preceded by a number that specifies how many times
or :set nooptioni to turn them off.To make them execute every time you open VI, create it is to be performed. For example, 5dw will delete 5 words and 3fe will move the
a file in your HOME directory called .exrc and type the options without the colon (:) cursor forward to the 3rd occurence of the letter e. Even insertions may be repeated
preceding the option conveniently with this method, say to insert the same line 100 times.
Set Default Description
– Ranges –
:set ai noai Turns on auto indentation
:set all -- Prints all options to the screen Ranges may precede most "colon" commands and cause them to be executed on a line
or lines. For example :3,7d would delete lines 3-7. Ranges are commonly combined
:set ap aw Prints line after d c J m :s t u commands with the :s command to perform a replacement on several lines, as with
:set aw noaw Automatic write on :n ! e# ^^ :rew ^} :tag :.,$s/pattern/string/g to make a replacement from the current line to the end of the file.
:set bf nobf Discards control characters from input :n,m Range - Lines n-m
:set dir=tmp dir = /tmp Sets tmp to directory or buffer file :. Range - Current line
:set eb noed Precedes error messages with a bell :$ Range - Last line
:set ed noed Precedes error messages with a bell :'c Range - Marker c
:set ht= ht = 8 Sets terminal hardware tabs :% Range - All lines in file
:set ic noic Ignores case when searching :g/pattern/ Range - All lines that contain pattern
:set lisp nolisp Modifies brackets for Lisp compatibility.
:set list nolist Shows tabs (^l) and end of line ($) – Shell Functions –
Allows pattern matching with special Executes shell command cmd; you can add these special characters to
:set magic magic :! cmd
characters indicate:% name of current file# name of last file edited
:set mesg mesg Allows others to send messages Executes shell command cmd, places output in file starting at current
:set nooption Turns off option !! cmd
line
:set nu nonu Shows line numbers :!! Executes last shell command
Speeds output; eliminates automatic :r! cmd Reads and inserts output from cmd
:set opt opt
RETURN :f file Renames current file to file
para = macro names that start paragraphs for :w !cmd Sends currently edited file to cmd as standard input and execute cmd
:set para=
LIlPLPPPQPbpP { and } operators
:cd dir Changes current working directory to dir
:set prompt prompt Prompts for command input with :
:sh Starts a sub-shell (CTRL-d returns to editor)
:set re nore Simulates smart terminal on dumb terminal
:so file Reads and executes commands in file (file is a shell script)
:set remap remap Accept macros within macros
Sends text from current position to Motion Command to shell
Indicates largest size of changes reported on !Motion_cmd
:set report noreport command cmd
status line
Sorts from current position to end of paragraph and replaces text with
:set ro noro Changes file type to "read only" !}sort
sorted text
:set scroll=n scroll = 11 set n lines for CTRL-d and z
:set set shell escape (default is /bin/sh) to – Files –
sh = /bin/sh
sh=shell_path shell_path
:set showmode nosm Indicates input or replace mode at bottom :w file Write to file
:set slow slow Pospone display updates during inserts :r file Read file in after line
:set sm nosm Show matching { or ( as ) or } is typed :n Go to next file
:set sw=n sw = 8 Sets shift width to n characters :p Go to previous file
Path for files checked for tags (current :e file Edit file
:set tags=x tags = /usr/lib/tags
directory included in default)
:set term $TERM Prints terminal type
!!program Replace line with output from program
:set terse noterse Shorten messages with terse
:set timeout Eliminates one-second time limit for macros
Sets significance of tags beyond n characters
:set tl=n tl = 0
(0 means all) – Other –
:set ts=n ts = 8 Sets tab stops to n for text input
~ Toggle upper and lower case
Inhibits normal checks before write
:set wa nowa J Join lines
commands
:set warn warn Warns "no write since last change" Joins the next n lines together; omitting n joins the beginning of the next line
nJ
to the end of the current line
:set window=n window = n Sets number of lines in a text window to n
. Repeat last text-changing command
Sets automatic wraparound n spaces from
:set wm=n wm = 0 Undo last change (Note: u in combination with . can allow multiple levels of
right margin. u
undo in some versions)
Sets automatic wraparound n spaces from U Undo all changes to line
:set ws ws right margin. ; Repeats last f F t or T search command
:N You can open up a new split-screen window in (n)vi and then use ^w to
or :E switch between the two.