vim_ref
vim_ref
Kurt Schmidt
April 2022
1
Contents
Foreword 4
Crutches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1 Modes 5
1.1 Getting Lost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2 Startup 6
2.1 Some Handy Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2 Options Beginners Might Consider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 .vimrc – Vim Config File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 Getting Help 9
3.1 Signalling Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5 Motion 11
5.1 Line Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.2 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6 Insert Mode 13
6.1 Inserting Literal Character i_ctrl-v . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6.2 Unicode Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7 Edit Operations 14
7.1 Undo / Redo / Repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
7.2 Deleting Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
7.3 Pasting Text (Put) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
7.4 Copying Text (Yank) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
7.5 Changing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
7.6 Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
8 Formatting 17
8.1 Indent/Exdent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
8.2 Joining 2 lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
8.3 Re-formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
12 Buffers 21
12.1 Buffers v Tabs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2
12.2 Using Multiple Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
12.3 Working With Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
13 Command Mode 22
13.1 Some Really Helpful Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
13.2 Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
13.3 External Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
14 Folds 25
Epilogue 27
3
Foreword
This document is a quick reference, for things you’ve learned, and a quick survey of some features you
might wish to learn.
You must go through the Vim tutorial first
$ vimtutor
, and keep practising. Get comfortable, and add new skills you find useful.
Crutches
While learning Vim I really discourage the use of the mouse, and of the arrow keys. Navigation,
selection, will take a little bit of practice, but, this effort is well worth it. There is much more available
than the four simple motions, but you’ll never get there if you stop at the arrow keys.
The mouse is great for editing photos, playing solitaire, but moving the cursor, selecting text, we have
many fine tools. Similarly, the mouse will slow you down.
4
1 Modes
Vim typically starts in Normal Mode. You can navigate around your document, save it, go into insert
mode, etc. Usually the <ESC> key will bring you back to Normal Mode.
Some of the other modes:
Insert Mode Where you do all your typing. See Insert Mode Section
Visual Mode Use motions to visually select text. See Visual Mode Section
Replace Mode Really just an overstrike mode, as best I can tell.
Use <ESC> to get back to Normal Mode, same as Insert
Command Mode For things that start with a :, /, or ?. Shows up at the bottom of the window.
See also the Command History Window
5
2 Startup
Some useful features can be invoked at startup, either through a command-line option, or installed
alias:
view Starts vim in read-only mode. Same as vi -R
vimdiff Starts you in a diff mode, documents are side-by-side. Make your terminal wide. vim -d
gvim gview The GUI version (if installed). vim -g
6
2.2 Options Beginners Might Consider
Note, you often needn’t spell out the entire option. I’ll include common shortcuts that I remember.
Examples will show settings I recommend.
[no]compatible cp Don’t behave as the original VI editor
set nocompatible
hidden hid Allows you to switch to another buffer w/out saving current one
set hidden
background bg By default, color schemes assume a light background. If you use a dark terminal, then:
set bg=dark
showcmd sc Toggle. Show partial commands (what you’re typing in Normal Mode) at bottom
ruler ru Toggle. Shows row and column in lower right corner
hlsearch hls Toggle. Highlights matches from a search. (Use :set nohl to turn it off, or see below
for a binding I use)
incsearch is Toggle. Show matches as regex is being typed
wildmenu Toggle. Better command-line completion (try the <Tab> key)
showmode smd Toggle. Displays the vim mode, bottom left (I think)
showmatch sm Toggle. Shows partner when cursor is placed on a bracket (parenthesis, etc.).
textwidth tw Number. Width of buffer. Where VIM will insert characters (on whitespace), as you
type (format cmd, etc.)
tabstop ts Number. The number of columns a tab displays. (Does not affect the buffer, only the
appearance.)
set tabstop=3
shiftwidth sw Number. Number of spaces to use for indent, shift (<<, >>, etc.). Set to 0 to use
tabstop value
set shiftwidth=3
• Set to 0 to use value of tabstop
set shiftwidth=0
expandtab et Toggle. Typed tabs will be converted to tabstop spaces in buffer
• Doesn’t affect existing tabs
• See :retab
number nu Toggle. Turns on line numbers.
I find I don’t really use this. I have the ruler, and <n>G will take me to a particular line.
relativenumber ru Toggle. Give it a try. I don’t use it, but, some might find it handy, especially
when nu is also set
wrap Toggle. Display only. Long lines are wrapped, rather than disappearing off to the right. Doesn’t
modify the buffer.
showbreak sb String. Prefix for wrapped lines.
7
2.3 .vimrc – Vim Config File
You can put your customisations in a config file. Vim will read ~/.vimrc upon startup. (GVim will
instead read ~/.gvimrc, if present.)
I have created a simple starter .vimrc file for newbies to use. Users on the department machines should
copy it from my Public directory. Or, you can look at it here.
Note, the double-quote, ", introduces a line comment in this file
Other than the options discussed above, here are some other settings you might find helpful:
syntax on Turns on syntax highlighting, autoindenting, etc. (as appropriate)
map Y y$ Behave like y$, rather than yy
D and C work in this way. The original Vi was inconsistent, here.
nnoremap <C-L> :nohl<CR><C-L> Ctrl-L now turns off search highlighting, then redraws the screen
map <F1> <Esc>
imap <F1> <Esc> I got tired of hitting the <F1> (help) key, when reaching for the <ESC> key
[Older Lenovos had <ESC> above the <F1> key.]
set pastetoggle=<F2> Use <F2> to toggle between paste and nopaste. Helpful if pasting into a
buffer from the terminal or desktop clipboard
map <F3> :set invlist<CR> Use <F3> to toggle between list, which shows you newlines, tabs, and
other characters, and nolist
map <Leader>m :wall<CR>q:?ˆmake<CR><CR> Type \m to save any dirty buffers, then re-execute the
most recent make command
8
3 Getting Help
From normal mode:
:help
It takes an optional argument. Wildcards and tab-completion (or CTRL-D) might help you.
:help k
To exit help:
:helpclose
, or, if it’s the current frame:
:q
Jump to specific topics by putting cursor on highlighted tag, hit ctrl-]. Use ctrl-o or ctrl-t to
jump back.
To review help navigation:
:help :help
9
4 Administrative Commands (Normal Mode)
4.1 Background
If you use a terminal with a dark background:
:set bg=dark
10
5 Motion
You can move the cursor around with the following keystrokes while in Command Mode. Most (all?) of
the following can be preceded by a count. Default is 1.
Simple motions
h Left
j Down
k Up
l Right
0 Beginning of line
$ End of line
H Top of window
M Middle line of window
L Bottom of window
gg 1st line of file
nG n th (last) line of file
ctrl-f Forward a screenful
ctrl-b Backward a screenful
Please don’t use the arrow keys. They’re slower than the 4 basic motions, right on the home row. Get
good at the basic motions, then add other motions. Don’t just dead-end at arrow keys. That’s Notepad.
Motions over objects
11
5.2 Searching
Note, / and ? are Command-line Mode commands; the cursor will jump to the bottom of the window.
12
6 Insert Mode
There are various ways to get into Insert Mode from Comand Mode:
Each of these can be preceded by a count. You type some text. When you hit <ESC>, the edit is
repeated.
6.2.2 Digraphs
Vim maps a great many 2-character (and 3-character) sequences to special characters.
While typing in Insert Mode, use ctrl-k, then the 2-key combo.
To see a list of digraphs, with descriptions:
:help digraphs-default
. May be useful to search (/re) for a character.
To see a complete, more compact table:
:digraph
13
7 Edit Operations
7.1 Undo / Redo / Repeat
u Undo
ctrl-r Redo
. Repeat the last edit
Unlike in the original Vi, we have an undo (and a redo) stack, so, you can undo all the way back to the
loaded buffer. (See variable undolevels.)
14
7.3.2 Read a File, or Command Output, into Buffer
I often find it handy to skip the clipboard, to simply read contents from a file <fn> right into the buffer.
From Normal Mode:
:r <file>
, will insert contents starting at line beneath the cursor.
To place output from a command into buffer, e.g.:
:r! grep Waldo *.tex
:r! date -R
:r! head -n12 <file>
r Replace single character under the cursor with another. Leaves you in Normal Mode
R Puts you in Replace Mode
c Change. Used much like d, above, but leaves you in input mode
s Substitute Delete char under cursor, leaves you in Insert Mode
~ Changes case of character under cursor. Can be used w/a count, or highlighed text
15
7.6 Registers
Any of the lower-case letters can be used to store deleted or yanked (copied) text, when preceded by a
" . Simply precede any delete, yank, or put operation with a register specifier:
"f2yy # copy current line and line below into register f
# move elsewhere
"fp # paste the 2 lines after the cursor
"fP # paste the 2 lines before the cursor
You can use Visual Mode when yanking or deleting text.
The numbered registers contain a history of your yanks. Vim maintains other registers, too.
:reg Will show you all the registers and their contents
Vim uses the numbered registers, 0-9, and a few others, to automatically store yanked/deleted text and
other things.
"%p # paste filename of current buffer into the buffer
16
8 Formatting
8.1 Indent/Exdent
8.3 Re-formatting
You can ask Vim to reformat, e.g., a paragraph, or a function
• Use Visual Mode, then gp
• Or, gp<motion>
– gp} – reformat to end of paragraph
• Inner-objects
– gpip – reformats entire paragraph cursor is on
– (See Text Objects, below)
17
9 Vim Text Objects
These Normal Mode keystrokes are sorta like motions, but, they don’t generally use the location of the
cursor as a mark. E.g., if the cursor is anywhere in a word, we can delete the word, or the sentence, or
reformat the paragraph.
Each object can be selected with (a) or without (i) surrounding whitespace (sorta).
aw A word
iw An inner word
aW iW A (inner) big word
as is A (inner) sentence
ap ip A (inner) paragraph
9.1.1 Strings
9.1.3 Examples
18
10 Marks, Visual Mode
Marks can be used for navigation, and for commands that take ranges. 2 marks can delimit a selection
of text, for a variety of actions, including deleting/yanking, reformatting, write (:w), substitute (:s)
and filter (:!) commands.
• To set a mark: mc , where c is any lower-case letter
• To go to that line: 'c , where c is the mark
v visual
V visual, line mode
ctrl-v visual, block mode
, the use motion keys (or a search) to get to the other border.
<ESC> will cancel the current Visual Mode, return you to Normal Mode.
Example:
• Use Visual Mode to highlight several lines of text
• Type
:
– Note the bottom line of your screen. The special marks are already filled in
• Continue to type
w ~/tmp/blah.sav
– Note, you’ve save the highlighted text to a new file
– Remember to delete the file
• Or, hit <ESC> to cancel the command
19
11 Splitting the Window
[I don’t quite have the jargon correct, so, don’t get too formal.]
When you call for :help, e.g., the window splits into 2 panes.
Other ways to split the window / open a new one (from Normal Mode) :
20
12 Buffers
12.1 Buffers v Tabs
In short, Vim “tabs” should be called “layouts”. Think of Vim’s buffers as you might regard tabs in a
different program.
This is a discussion of “Tab Madness”.
I have this handy binding in my .vimrc, so I hit gb, the buffers are listed, then I just type the new
buffer number:
nnoremap gb :ls<CR>:b<Space>
Careful, :q will try to quit all the buffers.
21
13 Command Mode
Largely, everything can be done here. The original Vi was built on an older line-editor, Ex. You
couldn’t move the cursor around the document, so, everything was done from this command line. It is
here you save the buffer, open new ones, quit, change settings, delete or replace text, delete lines, insert
or append, run the buffer against an external filter, etc.
You already know some:
:w[!]
:q[!]
:wq
:x
:r
:set
13.1.2 Substitute
Does a REGEX search and replace on first occurrence of current line:
:s/white chocolate/not chocolate/
To replace every occurrence on the line:
:s/this/that/g
13.1.3 Global
Allows us to apply an Ex command only to lines that match some regular expression.
To delete empty lines:
:g/^$/d
To delete blank lines (maybe contain whitespace):
:g/^\s*$/d
22
13.2 Ranges
Many of these commands which act on the buffer can be preceded by a range (the default is simply the
current line).
Ranges can be delimited by:
• Line numbers
– . is the current line
– $ can be used for the last line
– % is a range, the entire file (short for 1,$)
• Marks
• Regular Expressions
– REs are delimited by /
Some examples:
• To delete lines 1-12:
:1,12 d
• To swap first occurrence of Merry for Pippin on each line, from current line to end:
:.,$s/Merry/Pippin/
• To swap every occurrence of Merry for Pippin on each line, from current line to end:
:.,$s/Merry/Pippin/g
• To save the HTML header section to another file:
:/<head>/,/<\/head>/ w ~/public_html/general_header.html
• Note, had to escape that inner /
• Make a fold out of the highlighted code (Visual Mode):
:`<,`>fo
• Given 2 marks a and b, a precedes b, comment out all lines in range (inclusive)
:`a,`bs/^/# /
We can mix range element types.
• Delete lines from beginning of file until the string <body> is found (inclusive):
:1,/<body>/! d
13.2.1 Offsets
We can add or subtract lines from range indicators. E.g., to delete starting 2 lines after the cursor up
to (and including) the line before the next occurrence of “Waldo”:
:.+2,/Waldo/-1 d
23
13.3 External Filters
You can apply any external program which reads stdin and writes to stdout (filter) to the buffer.
To sort all lines of the file:
:%! sort
Use awk to put line numbers in the file:
:%! awk '{printf( "\%3s \%s\n", NR, $0 )}'
• We had to escape the % from Vim
– Vim will replace % with the filename of the curent buffer
24
14 Folds
The easiest way to create a fold is to use visual mode. Highlight section, then:
:fo
• or:
zf
Of course, text objects work:
zfip
Then, we can open, close, toggle, and delete folds:
25
15 Odds and Ends
Here are commands and options which I use often enough, but which don’t yet appear elsewhere in
this document
:[range]retab :ret Command. Reformats strings of whitespace containing tabs
• Handy if you’ve changed 'tabstop', or set 'expandtab'
:visual :vi Leave Ex Mode. Get back to normal VIM editing
• Happens to me all the time (I inadvertently hit Q in Normal Mode)
• Ex is an old line editor that the original Vi was built on
• It is the source of all the : commands
26
Epilogue
I didn’t touch a good deal of what Vim has to offer. This document is intended for those fairly new
to Vim. It contains some information immediately useful, and some that might provide anchors for
further investigation, a preview of things yet to learn.
Become proficient w/the basics, then add a feature to your reportoire, take it for a test drive. If it
improves your workflow, great. If not, that’s fine. Either way, pick the next feature that looks appealing,
take it for a spin. There’s some effort involved here, but, on balance, the effort spent will work for you,
make you more productive.
Again, there’s much to Vim. If you feel as though the editor should do something, it likely does. Or,
perhaps, you can record macros or even write a function to do it.
27