(Need a .vimrc to get you started on your way to customization glory? Use mine!)
vim
, not vi
.
vi
is the legacy, POSIX-standard program, vim
is the new, not
always compatible program<Esc>:q!<CR>
:q
is short for
:quit
.Access Vim’s built-in help system by typing <Esc>:help<CR>
<i>
in Normal mode (or <I>
, or
<o>
, or <O>
, or…).:q
to quit.ex
mode: line-oriented mode. Run screaming.<i>
to enter Insert modeWhen done typing, hit <Esc>
to go back to Normal mode, then :saveas
doc.txt
to save the file. (Shortcut: :sav <filename>
)
Hit <i>
to add more text, then hit <Esc>:w
to write the
current file to disk.
Congratulations, you’ve used Vim to write a document!
Sure, you can use arrow keys, PageUp, PageDown, etc. with Vim.
vi
(like OpenBSD’s nvi
), it
may not understand arrow keyshjkl
instead!
<h>
moves the cursor one character left (think: h
is the
left-most character)<l>
moves the cursor one character right (think: l
is the
right-most character)<j>
moves the cursor one character down (think: j
hangs
below the line)<k>
moves the cursor one character up (think: k
ascends
above the line)<C-d>
/<C-u>
to page down/uphjkl
and <C-d>
/<C-u>
instead of arrow and page keys if
they work?
<w>
moves one word forwards<b>
moves one word backwards<^>
moves to the first non-whitespace character at the beginning
of a line<0>
moves to the beginning of a line, too (think: column zero)<$>
moves to the end of a line (think: regular expressions)<G>
moves to the last line of a document (think: Goto. No, it
doesn’t make sense in this context. More later.)<gg>
moves to the first line of a document
vi
. Use :0
instead.:[line]<CR>
moves the cursor to the line number specified. For
instance, :16<CR>
would move the cursor to line 16 of the current
buffer.<e>
moves one word forwards, and places the cursor at the end
of the word<2w>
moves two words forwards<4j>
moves four lines down<12G>
goto the twelfth line from the top of the document (I told
you to wait for it.)<3h>
moves three characters backwardsz
command can be useful to move the viewport around (in Normal
mode)
<z<CR>>
moves the current line to the top of the screen<z.>
moves the current line to the middle of the screen<z->
moves the current line to the bottom of the screen<i>
enters Insert mode and lets you start typing
exactly where the cursor is located<I>
will place the cursor at the first non-space character on a
line, then put you in Insert mode.<a>
will let you append text. It places the cursor one
character to the right, then places you in Insert mode.<A>
appends text to the end of a line.<o>
opens a new text line below the current line.<O>
opens a new text line above the current line.<h>
to move backwards until your cursor
is resting on the extra “d”<x>
to delete the character under the cursor<BS>
(Backspace) to delete it<Esc>
to go back to Normal mode<b>
to moves backwards by words until your cursor is
at the beginning of the second “brown”<dw>
to delete the word at the cursor.d
in the
previous example). Other modifiers include:
<c>
changes the following [character/word/etc.] by
deleting it, then putting Vim into Insert mode so you can
start typing<dd>
to delete a line./pattern<CR>
<Esc>/Normal<CR>
The above is an example of a forwards search. To perform a
backwards search, use ?pattern<CR>
instead.
n
.
n
when doing a backwards search will find the
next occurrence earlier in the document.To find the previous occurrence of the same pattern, use N
.
To find the next occurrence of the word underneath the cursor, in
Normal mode hit <*>
Similarly, to find the previous occurrence of the word underneath the
cursor, use <#>
<Esc>
to get there from Insert mode) for all of these.
:%s/cat/dog/g
:s/cat/dog/g
. (Note the absence of the
percentage sign!):s/cat/dog/
. (Note the absence of the g
at
the end!)
<n>
to search for the next occurrence of cat,
and then use <&>
to replace it with dog.<v>
(this
enters Visual mode)<y>
to yank the text into Vim’s clipboard.<p>
to paste it. Note: the yanked text will be
placed after the cursor. Use <P>
to paste yanked text before
the cursor.<V>
instead of <v>
.<y>
and <Y>
commands do not work in regular
vi
!:set number
in Normal mode. Turn them
off again by typing :set nonumber
.:help command
. For example:
:help number
:help w
(to get help on the movement command):help :w
(to get help on the :write
command):help vimtutor
(a very useful built-in tutorial!)make
supportctags
supportSee my .vimrc
for a fully-annotated example of a .vimrc
configuration file that
includes many of the above features. Clone the repo to $HOME/.vim
and run make
(OSX/Linux only) for added fun.