Vim Tutorial
(Need a .vimrc to get you started on your way to customization glory? Use mine!)
Important Things: Starting, Quitting, Modes
- Start Vim by typing
vim, notvi.- Some installations have separate binaries for each (eg., OpenBSD).
viis the legacy, POSIX-standard program,vimis the new, not always compatible program
- Quit Vim in Normal mode by typing
<Esc>:q!<CR>- Vim uses lots of mnemonics for its commands:
:qis short for:quit.
- Vim uses lots of mnemonics for its commands:
Access Vim’s built-in help system by typing
<Esc>:help<CR>- So what’s Normal mode? What are modes?
- Vim is a modal editor, meaning it does different things based on which mode you have it in.
- There are many modes:
- Normal mode: used for navigation and text manipulation.
The default mode. Hit
to get back to it. - Insert mode: used for inserting new text into a buffer.
Enter this mode by typing
<i>in Normal mode (or<I>, or<o>, or<O>, or…). - Visual mode: used for manipulating text selections.
- Select mode: same as visual, but more GUI-oriented.
- Command mode: used for entering editor commands, like
:qto quit. exmode: line-oriented mode. Run screaming.
- Normal mode: used for navigation and text manipulation.
The default mode. Hit
Writing Our First Document
- Three steps:
- Start Vim
- Hit
<i>to enter Insert mode - Start typing
When done typing, hit
<Esc>to go back to Normal mode, then:saveas doc.txtto save the file. (Shortcut::sav <filename>)Hit
<i>to add more text, then hit<Esc>:wto write the current file to disk.Congratulations, you’ve used Vim to write a document!
Moving Around
Sure, you can use arrow keys, PageUp, PageDown, etc. with Vim.
- What if those don’t work?
- Mac’s Terminal.app remaps PageUp/PageDown to scroll through the windows’s scrollback buffer instead of passing it through.
- If you find yourself with a real
vi(like OpenBSD’snvi), it may not understand arrow keys
- Solution: use
hjklinstead!- In Normal/Command mode
<h>moves the cursor one character left (think:his the left-most character)<l>moves the cursor one character right (think:lis the right-most character)<j>moves the cursor one character down (think:jhangs below the line)<k>moves the cursor one character up (think:kascends above the line)
- What about paging up/down?
- Use
<C-d>/<C-u>to page down/up
- Use
- Why use
hjkland<C-d>/<C-u>instead of arrow and page keys if they work?- Your fingers never leave home row
- Do I have to use these weird keys?
- No! Do what works for you, not for anyone else.
Advanced Movement
<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- Note: this is a Vim command, and will not work in
vi. Use:0instead.
- Note: this is a Vim command, and will not work in
:[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.
More Advanced Movement
<e>moves one word forwards, and places the cursor at the end of the word- Most movement commands can take a count prefix. Examples:
<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 backwards
- The
zcommand 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
Text Manipulation
- Let’s talk about adding things.
- As seen before,
<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.
- As seen before,
- Let’s talk about deleting things.
- Type the sentence, “The quick brown brown fox jumped over the lazy ddog.”
- See how you misspelled “dog”?
- In Normal mode, use
<h>to move backwards until your cursor is resting on the extra “d” - Hit
<x>to delete the character under the cursor
- In Normal mode, use
- Oops, you wrote “brown” twice!
- Method 1: still in Insert mode, arrow-key your way back to
the second “brown” and hit
<BS>(Backspace) to delete it - Method 2:
- Hit
<Esc>to go back to Normal mode - Use
<b>to moves backwards by words until your cursor is at the beginning of the second “brown” - Type
<dw>to delete the word at the cursor.
- Hit
- Method 1: still in Insert mode, arrow-key your way back to
the second “brown” and hit
- Most movement commands also accept a modifier (the
din 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
- You know what, that sentence is terrible. Just delete the entire
line.
- Use
<dd>to delete a line.
- Use
Searching
- To find the next occurrence of a text pattern, in Normal mode type
/pattern<CR>- Example: To find the next occurrence of the word “Normal” in this
document, I’d type
<Esc>/Normal<CR>- Note that pattern can be a regular expression!
- Example: To find the next occurrence of the word “Normal” in this
document, I’d type
The above is an example of a forwards search. To perform a backwards search, use
?pattern<CR>instead.- To find the next occurrence of the same pattern, use
n.- Note that using
nwhen doing a backwards search will find the next occurrence earlier in the document.
- Note that using
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
<#>
Replacing
- There are many ways to replace text using Vim. The following are only
a couple of the most useful methods. You should be in Normal mode
(hit
<Esc>to get there from Insert mode) for all of these.- To replace all occurrences of the word “cat” with “dog” in a
document, type
:%s/cat/dog/g - To replace all occurrences of the word “cat with ”dog" on the
current line, type
:s/cat/dog/g. (Note the absence of the percentage sign!) - To replace the next occurrence of the word “cat” with “dog” in
the document, type
:s/cat/dog/. (Note the absence of thegat the end!)- You can use
<n>to search for the next occurrence of cat, and then use<&>to replace it with dog.
- You can use
- To replace all occurrences of the word “cat” with “dog” in a
document, type
Selecting, Copying & Pasting
- Vim calls “copying” text “yanking”.
- To yank text, first you have to highlight it.
- Navigate to the start of your selection, then press
<v>(this enters Visual mode) - Using the movement keys, navigate to the end of the selection
- Hit
<y>to yank the text into Vim’s clipboard.
- Navigate to the start of your selection, then press
- To paste text, place your cursor where you’d like the yanked text to
appear and hit
<p>to paste it. Note: the yanked text will be placed after the cursor. Use<P>to paste yanked text before the cursor. - If you want to select entire lines, use
<V>instead of<v>. - BIG NOTE: the
<y>and<Y>commands do not work in regularvi!
Miscellaneous
- Show line numbers by typing
:set numberin Normal mode. Turn them off again by typing:set nonumber. - To get help on any Vim command, type
:help command. For example::help number:help w(to get help on the movement command):help :w(to get help on the:writecommand):help vimtutor(a very useful built-in tutorial!)
- Fun tutorials:
- OpenVim - an interactive Vim tutorial (http://www.openvim.com/tutorial.html)
- Vim Adventures - “Learning Vim while playing a game” (http://vim-adventures.com)
What Else Can Vim Do?
- A lot!
- Custom keybindings
- Syntax highlighting
- Auto-indenting
- Keyword completion
- Custom color schemes
- “Vimscript”
- Plugins
- Spell-checking
- Built-in
makesupport - Built-in
ctagssupport - It’s not emacs!
See my .vimrc for a fully-annotated example of a
.vimrcconfiguration file that includes many of the above features. Clone the repo to$HOME/.vimand runmake(OSX/Linux only) for added fun.
