vi/vim notes

vi/vim notes

I first created and posted these notes online on May 28, 2002. They may be the most useful thing I ever do. I collected most of these over the years from several sources, but I can’t give proper credit since those links are all dead now.

Changing Text

cc change a line
C change from current cursor position to the end-of-line
cw change from current cursor position to the end of the word
J joins the next line to the current line (inserts a space between the two)
gJ joins the next line to the current line (without a space)
:12,24j join lines 12 thru 24, leaving a space between each line.
~ switch lower and upper cases
r replace the current character with the next character typed
4rx replace next four characters with “x”
R overwrite until <esc>
s substitute the current character and go into insert mode. Same as “rxi <ESC>
S substitute the current line, erase it, and go into insert mode, starting at the indentation of the line. Same as “cc”
>> shift current line forward
3>> shift three lines forward
>% shift all lines until a matching brace or parenthesis
>} shift to the end of the paragraph
>’m shift to the marked line
<< shift current line backward
vEU change from here to end of word to uppercase
vEu change from here to end of word to lowercase

Copying and Pasting

yl yank (copy) current character
yn yank n characters
yw yank the current word
yy yank the current line
Y yank a line, same as “yy”
nyy yank n lines of text
y$ yank to end of line
y) yank to the end of the sentence
y’x yank from current line to the mark x (use mx to mark as x)
y/word yank from current position to "word"
“ay’m yank from here to the mark. Place this into the named buffer “a”. This will also allow one to switch files using the “:e fn” or “:n fn” command and then place the buffer back.
“ayy yank current line into buffer a.
“a3Y yank three lines, place into buffer “a”
“A3Y append 3 lines, place into “a”. This allows one to build up a buffer.
“ap paste the contents of “a” back, below current line.
"zyy
@z

"zyy places the highlighted text into buffer z; @z will run the contents of buffer z. This allows a search pattern to be written in the document and then reused.

p paste to the right of the cursor
P paste to the left of the cursor

Deleting

x delete the character under the cursor
dd delete the current line
d^ delete from current cursor position to the beginning of the line
d$ delete from current cursor position to the end of the line
dw delete from current cursor position to the end of the word
d3w delete three words
3dd delete three lines
d/word delete until you find word
dfx delete from here until the character “x”
D delete everything to the end of the current line (same as d$)
3dd delete everything to the end of the current line and the following 2 lines
u undo deletion
U undo the line you just changed

Inserting

i insert at the current character
I insert at the beginning of the current line
20I <ESC> insert 20 dashes (-) at the beginning of the line
a append to the right of the current character
A append at the end of the current line
o insert a new line immediately following the current line
O insert a new line immediately before the current line

Moving

h move the cursor one character to the left
j move the cursor one character down
k move the cursor one character up
l move the cursor one character to the right
0 move cursor to the beginning of the current line
$ move cursor to the end of the current line
w move cursor forward a word
3w move cursor forward three words
W move cursor forward, ignoring punctuation
b move cursor back a word
B move cursor back a word, ignoring punctuation
e move cursor to the end of the word
E move cursor to the end of the word, ignoring punctuation
G move cursor to the last line of the file
nG move cursor to the beginning of line n
1G move to the first line of a file
n| moves the cursor to the beginning of column n
f find a character in the line, forward
fa find the character “a” in the line, forward
2fa find the second occurrence of “a” in the line, forward
F find a character in the line, backward
Fz find the character “z” in the line, backward
t find up to a character in the line, forward
) jump to the next sentence
} jump to the next paragraph
] jump to the next section
% find matching brace or parenthesis
^b scroll backwards one page. A count scrolls that many pages
^f scroll forwards one page. A count scrolls that many pages
^u scroll up half a screen
^d scroll down half a screen
z <enter> put current line at top of screen
z. put current line at middle of screen
z- put current line at bottom of screen
m start a mark, the next character is the name, any character from a-z
ma mark this spot with the character “a”
‘a return to the line marked “a”
`a return to the character marked “a”
return to previous line (auto-mark)
return to previous character (auto-mark)

Search and Replace

/word search forward for word
?word search backward for word
?word?z.
search forward for word, put line at the middle of screen
n jump forward to next occurance of word
N jump backward to next occurance of word
* search forward for word under cursor (vim)
# search backward for word under cursor (vim)
g* search forward for word under cursor (partial match) (vim)
g# search backward for word under cursor (partial match) (vim)
:noh stop highlighting the word that was searched for
:s/old/new/ replaces first occurence of old with new on current line
:s/old/new/g globally replaces old with new on current line
:%s/old/new/g globally replaces old with new on all lines
:s/old/new/gc confirms replacements
:%s/^V^M//g globally remove DOS carriage returns
:%s/,/^V^M/g

globally replace commas with newlines (yes, I know this looks like it would insert DOS carriage returns, but it only inserts the newlines on Unix)
:%s/\s*$// strip blanks from end of lines
:%s/^.\{30}// remove the first 30 characters from each line
:%s/[ ^I]*$/!d remove trailing whitespace (where ^I is produced by pressing the tab key)
:%s/.*/\L&/ convert the entire file to lowercase.
:s/.*/\u&/ convert the first character of line to uppercase.
:%s/\<./\u&/g

convert the first character of each word to uppercase

\< matches the start of a word;
. therefore matches the first character of a word;
\u tells Vi(m) to uppercase the next character in the substitution string;
& means substitute whatever was matched on the lhs;

:s/book{./\L&/ convert the first character after the { to lowercase.
:’x,.s/\([a-z]\)=/\1 =/ replace any lowercase character in the alphabet followed by an equal sign with the same character followed by a space and an equal sign, e.g., book= >> book =
::%s/\(That\) or \(this\)/\2 or \1/ change "that or this" to "this or that"
::%s/\(^\\foilhead{\(.*\)}\)/
%\1^M\\section{\2}/
comment out each line beginning with ‘\foilhead’ and replace with a line beginning with ‘\section’; note the nested backreferences for keeping the rest of the line the same
::%s/\(.*\)/<a href="\1">\1<\/a>/ add HTML tags for references to files
:s/ \(\d\{1,2}\)\.\(\d\{1,2}\)\(.*\)/<a href="\1-\2.sh">\1.\2\3<\/a>

change a line of the form
       1.1 Accessing Substring
to
       <a href="1-1.sh">1.1 Accessing Substrings</a>

:’x,.s:^\([^ ]\+\) \(.*\):
<\1>\1 \2<\/\1><br>:

change a line of the form
       KBD used for text to be typed by the user
to
       <KBD>KBD used for text to be typed by the user</KBD><br>

Colons are used here instead of the usual slashes for separating the ‘find’ and ‘replace’ parts. The part ^\([^ ]\+\) finds all of the characters up to the first space and saves them for use in the ‘replace’ part as \1.

:’x,.s/^/#/ insert a pound sign (#) at the beginning of the line from the line marked with mx to the current line
:%s/word1\nword2// remove word1 from the end of a line and word2 from the beginning of the next line; \n represents the newline between the two
:& repeat previous substitution
:<up arrow> recall : history
:g/string/d deletes every line that contains string
:g/\.c/+|s/^/\.cc/ look for all “.c” commands, jump to next line, substitute the beginning of line with “.cc”.
:g/^\..*/|s//\U&/ convert all characters at beginning of line that start with a “.” to upper case.
:v/string/d deletes every line that does not contain string

:g/.*/m0

This will reverse the order of the lines in the current file. m0 is the ex command to move the line to line 0.

:v/./d or :g/^$/d

Remove all blank lines.
:g/^\s*$/d Removes all lines that only have whitespace.
:v/./.,/./-1join Replaces multiple blank lines with just one blank line.

Options

:se ai autoindent (unset with :se noai)
:se ff=unix set fileformat to unix
set fileencoding=utf-8 change from Unicode to ASCII
:se ic ignore case when searching
:se list display tabs and carriage returns
:se nu display line numbers in the file. They are not actually in the file. (unset with :se nonu)
:se sm show matching brace or parenthesis while inserting
:se smd display the mode
:se sw=2 set shifting to 2 spaces
:se tabstop=4 set tabs to 4 spaces
^^D turn off autoindent for current line, resume same place for the next line (up carrot and control-D)
0^D reset the autoindent, start at the left margin

Using External Programs

!cmd execute an external program called cmd
!!cmd execute an external program, replacing the output with the results
!$ send from here to the end-of-line
!L send from here to the last line of screen
!23G send from here to line 23
!/word send from here until you find “word”
!) send from here until the next sentence
!} send from here until the next paragraph
!!date adds the date
!!cut -c41- cut the first 40 characters from a file
3!!sort send three lines to sort, and return the output
!Grev send from here to the end of file to the “rev” command; the results will reverse the characters in each line
!}sort sends from the current line until the first blank line to sort
:’x,.!sort will sort from the line marked with mx to the current line
:r !ll read the output of an “ll” command, and put it after the current line.
:’t,’b !spell check spelling from mark t to mark b
:’t,. !awk ‘{print $3 ” ” $2 ” ” $1}’ reverse the order of three columns
:%! sed G double space the entire file.
:1,5! sed G double space the lines from 1-5
:’x,.!sed ‘/^$/d’ remove the blank lines from mark x to the current line

Miscellaneous

^g give file name, status, current line number and relative position
^l refresh the screen (sometimes `^P‘ or `^R‘)
^v visual mode. in Windows, Ctrl-v is mapped to paste text. Use Ctrl-q instead (vim)
. repeat latest command
& repeat latest `ex’ substitute command, e.g. :s/wrong/good/
:ab lg longer create an abbreviation. Whenever lg is typed, it will be replaced with longer.
vi +/string filename start vi, jumping to the first occurrence of string
vi -r filename recovers an open file after a crash
vi `cat somefile` open a list of files that are in the file somefile
:r filename insert file filename, placing its contents after current line
:1,4w filename write lines 1 through 4 to filename
:e filename edit filename
:f filename change current file name to filename
:n edit next file
:sh call up the shell, run commands until a ^d (CONTROL-d), then return to editing
:vi VI MODE. Used if “Q” is pressed, or called up ex, and now wish to use the visual mode
  In Insert mode, press the Ctrl-p or Ctrl-n key to complete part of a word that has been typed. This is useful for entering function names.
:<up arrow> recall : history
Comments are closed.