VIM quick-start, cheat-sheet, and links
If you’ve been following me for a while, you might know my editor of choice is VIM, it’s very clean. There’s nothing there to get in your way, except your own limitations, and those limitations can be destroyed through practice. Haven’t tried vim yet? What are you waiting for?
Vim is a text editor written by Bram Moolenaar and first released publicly in 1991. It is commonly found on unix-based operating systems. VIM is based off an older text editor, vi, and it’s name is an acronym for Vi-Improved. With vim Your fingers never have to leave the keyboard to command great power. The learning curve is a little steep but you can very quickly pick up new skills as you need them. The basic premise is that there is an Input mode and a Command mode. By default you start in command mode, here you can move around the document, search, and do a fair bit of editing quickly. Insert mode is designed for adding (and removing) text, it’s just like a normal text editor.
Quick Start/Cheat Sheet
Vim commands are more of a language than a list of commands you need to use.
From the Command Line
- type “vim” into the command line to create a new untitled file
- type “vim /path/to/file.name” to open an existing file. (if the file doesn’t exist, this creates it)
- to open multiple files list them like
vim file1.txt file2.sh
they’re placed into buffers- you can switch to the next open file with the command
:bn
, or the previous one with:bp
, to move to the next or previous buffer. - to open in a split window
vim -o file.type file2.type file3.type
(lower case “o” for horizontal, uppercase for vertical split)
- you can switch to the next open file with the command
- open a file & jump to a particular line with
vim +10 file.type
for the 10th line (+
alone will jump to the end of the file) - You can run vim functions directly from the command line
vim +FunctionName +qall
(qall
tries to quit all, but refers to the user to accept or reject changes made with a standard:w
/q!
) - open a file & jump to a particular word or phrase with
vim -c "/searchstring" file.type
(you can execute other commands with the-c
flag too) - find the differences between two files with
vim -d file.v1 file.v2
(this works like vimdiff)
Working with VIM modes
- hit “i” to switch from command mode to input mode
- hit “v” to switch from command mode to visual mode
- hit “esc” to switch back to command mode
note: ‘i’ doesn’t mean “enter insert mode” so much as it is a command to “insert the following text until you hit ESC” Also, “command mode” is often referred to as “normal mode,” because for a lot of tasks, you will probably want to use a bunch of commands, instead of writing a bunch of stuff in input mode.
VIM Command/Normal-mode
- type “:wq” or “ZZ” to save and quit
- type “:q!” to quit without saving
- moving the cursor (basic movements)
j
move down one linek
move up one lineh
move left one characterl
move right one character
- to move to line 88 hit
88G
or88gg
- move to the begining of a file hit
gg
- move to the end of the file hit
G
- move one word forward with
w
- move one word backward with
b
- move to the end of the line with
$
- move to the begining of the line with
0
- move to the first character of the line with
^
- hitting
%
while the cursor is on a bracket like[({})]
will find the matching bracket - to delete a character hit
x
- to cut a line hit
dd
- to copy a line hit
yy
- to cut or copy multiple lines put a number before the command like
5dd
- you can put a number before most commands to repeat them X many times.
- press
p
to paste after the current line - press
P
to pase before the current line - to delete from the cursor to the end of the line hit
d$
, for example, or any other movement key (wbhjkl…) - in command mode to search for
apple
type/apple
and hit enter- to look for the next occurance hit
n
- to look for the next occurance hit
- in command mode to find and replace all occurances in a line type
:s/original/replacement/g
- when searching you can use regular expressions
- in command mode to find and replace all occurances in a file type
:%s/original/replacement/g
- for an interactive history of commands use
q:
- for an interactive history of searches use
q/
orq?
) - need more info? try
:help
there’s a wealth of information there- you can get more details about the help command with
:help help
- you can find an index of commands with
:help index
- you can get more details about the help command with
VIM Input-mode
I’m not sure how useful these are in gvim, but these should work if you’re running in a terminal.
- alt/meta+normal command usually works
alt+hjkl
offers the standard movementsalt+o
opens a new line below the current onealt+A
appends to the end of the current linealt+p
to pastealt+R"
to paste from register"
, for example, or use and other register.
VIM tips
- If the file is owned by root and you opened it as another user, you can escalate privileges and save with
:w !sudo tee %
- You can insert a file below the cursor with
:r /path/to/file.txt
or if you don’t supply a file it will insert the current file below the cursor. - the delimiter when using
:s
doesn’t have to be/
you can try%
or\_
if you want to avoid fences like in:s/\/usr\/local\/bin/\/common\/bin/
you can use:s#/user/local/bin#/common/bin#
- unless you set a
equalprg
in your vimrc, you can auto-indent with=
- to autoindent an entire file use
gg=G
(could have “unexpected” results) - to autoindent the current line
==
- to autoindent this line and the one after it is
=j
wherej
is a movement key - if you select a section with visual mode you can indent just that selection with
=
- to autoindent an entire file use
- You can store a cursor location in a mark, you can set a mark with command
m
followed by a letter likema
, it accepts [A-Za-z] so you get 52 different marks.- you can move to a line containing a mark using the
'
(single quote) command'a
moves to the line containing the mark labeleda
- you can move to the exact location of the mark using the
`
(backquote) command`a
moves to the mark labeleda
- these are “movements” that can be combined with other statements like
d`a
to cut text from the cursor’s location to the mark labeleda
- you can move to a line containing a mark using the
- You can make macros with the
q
command,- hit
qa
to create a macro nameda
(should show a record indicator) enter a series of commands and hitq
again to stop recording. - hit
@a
to execute the macro nameda
, you can execute the command multiple times in the standard way23@a
will repeat it 23 times. - You can execute a register as a macro with
@A
for registerA
, for example, or use any other register.
- hit
- You can execute a register as an ex command with
:@A
for registerA
, for example, or use any other register.
Going Further
Vim is highly customizable, you can set shortcuts and preferences in the .vimrc
file, usually located in your home directory.
There are a ton of plugins (aka scripts) available too. They’re easy to manage with other scripts
like Pathogen, Vundle, or vim-plug. I just switch from vundle to vim-plug because it makes it easier to configure your plugins and does it a lot faster.
If you want to get a headstart, my dotfiles are available on github, but there are a lot of people doing that lately, so look around. Also there’s a few very nice VIM Distributions like Janus, SPF13-vim, and dotvim that have a lot of plugins and a nice vimrc right out of the box, definitely worth a look.
:godmode:
Links
- Vim Interactive Tutorial (try it now!)
- VIM docs
- Vim Galore (“Everything you need to know about vim”)
- Vimbits
- Vimcasts (video tutorials)
- usevim
- Today I Learned: vim
- vim awesome
- The Patient Vimmer
- Michael’s VIM Cheat-sheet
- Kendall’s VIM Cheat-sheet
- VIM Keyboard Cheat-sheet
- Leaning Vim the Pragmatic Way
- Learning Vim for People Who Think Things Like Vim Are Weird and Hard
- VIM Regex
- Vim Tips Wiki
- Vim Tips Wiki’s Best Vim Tips
- r/Vim Wiki
- Zzapper’s Best of Vim Tips
- Secrets of the Vim Ninja
- Learn Vim Progressively
- VI Cheat Sheet
- Learning Vi & Vim editors (O’Reilly) (prefer books?)
- Download Vim
- Derek Wyatt’s Vim Video Tutorials
- VIM Adventures (Learn VIM playing an RPG)
- vimgolf (find the shortest way to complete the challenges)
- shortcutFoo (Drills to learn your tools better)
- Stack Overflow Question: What is your most productive shortcut with Vim?
- Stack Overflow Question: What are the most frequently used vimdiff commands?
- Stack Overflow Question: What simple vim commands do you wish you’d known earlier?
- Quora Questions: What are the most amazing things that can be done with Vim?
- amix’s .vimrc (a huge default vimrc file with lots of goodies)
- vimrc generator (makes a simple/minimal vimrc with a nice GUI)
- Vim Regex Tutor
- Vim Regexes
- 7 habits of effective text editing