Vim can be downloaded from vim.org. The Windows installer is a self-extracting archive which does not require administrator privileges to run (as long as you have permission to write to the selected install folder).
You can find help here:
- Intro
- Undo/Redo
- Inserting
- Motions
- Editing
- Navigating Help
- Searching
- Scrolling
- Marks
- Tags
- Completion
- Spell Checking
- Macros
- Substituting
- Visual Mode
- Text Objects
- Buffers and Multiple Files
- Windows
- Tab Pages
- Vimdiff
- Runtime Configuration
- Filetypes
- QuickFix Mode
- Compiling Code
- Compiler Plugins
- Browsing Folders
Vim is modal
- Normal mode maps each key to an editing command
- Insert mode is used to enter text
- GVim is Vim in a GUI.
- a GUI terminal might intercept keystrokes that it understands; GVim only understands Vim commands.
- EVim is GVim in "easy mode".
- Easy mode can also be run using
vim -yin the terminal - Easy mode can also be activated using the Vim command
:set insertmode. This also works in the command-line Vim. (see:help evim-keysfor the full list of options that EVim uses). - There are also "View" variants that open the file read-only
- In Vim/GVim, the program starts in Normal mode; use
ito enter Insert mode andESCto return to Normal mode - In EVim, the program starts in Insert mode; use
CTRL-Lto enter Normal mode andESCorito return to Insert mode
- When in Normal mode, use
:to enter the Vim command-line - Use
:w [filename]to save a file- To save a file under a different name without overwriting the original, use
:saveas [filename]
- To save a file under a different name without overwriting the original, use
- Use
:qto quit Vim - Use
:q!to quit Vim without saving changes - Use
:wqorZZto save and close - Use
:helpto read Vim's help
uundoes the previous commandCTRL-Rredoes the last undo
ienters Insert mode at the current positionaenters Insert mode after the current positionIenters Insert mode at the beginning of the lineAenters Insert mode at the end of the lineoinserts a new line below the current line and enters insert mode on the new lineOinserts a new line above the current line and enters insert mode on the new lineRenters Replace / overtype mode; typed characters replace existing onescfollowed by a motion deletes text and enters Insert mode (effectivelydfollowed byi)Escreturns to Normal mode
Note that everything from when Insert mode is first entered to pressing ESC
is treated as one command, and therefore one item on the undo stack.
For that reason, I usually use o to enter newlines so that each line is a
separate command and a separate item on the undo stack.
Motions move the cursor around. Most commands can be followed by a motion to indicate the affected text.
- When in Normal mode, use
h,j,k, andlto move the cursorhis on the left and moves the cursor leftlis on the right and moves the cursor rightjlooks like a down arrow and moves the cursor downkmoves the cursor up
- Normally, only
jandkare used; there are faster ways to move around
wmoves the cursor to the beginning of the next wordemoves the cursor to the end of the next wordbmoves the cursor to the beginning of the previous word- Lowercase
w,e, andbtreat whitespace and punctuation as word separators; UppercaseW,E, andBonly treat whitespace as separators (punctuation is considered part of the word)
ffollowed by a character goes forward (right) to the next character (e.g.fggoes to the nextg)Ffollowed by a character goes back (left) to the previous character (e.g.Fggoes to the previousg)forFprefixed with a number goes to the nth character (e.g.2frgoes to the 2ndrafter the cursor)
0jumps to the beginning of the line^jumps to the first non-whitespace character of the line$jumps to the end of the line
Hmoves the cursor to the highest visible line (on the screen)Lmoves the cursor to the lowest visible lineMmoves the cursor to the middle of the screen
ggmoves to the beginning of the document- Prefixed with a number, goes to the nth line (e.g.
20gggoes to line 20)
- Prefixed with a number, goes to the nth line (e.g.
Gmoves to the end of the document
{and}jump over paragraphs. A paragraph is any block of text separated by a blank line
%scans forward for a grouping character and jumps to the matching character
For nearly all editing commands, repeating the command character operates on the line under the cursor (e.g. dd deletes the line, yy copies the line, etc.).
dfollowed by a motion will delete characters (placing them in the default register)Dwill delete characters from the current cursor position to the end of the line (shortcut ford$)yfollowed by a motion will copy (yank) characters into the default registerYwill copy (yank) the current line into the default register (shortcut foryy)pwill put the contents of the default register after the cursorPwill put the contents of the default register before the cursor- The above commands can be prefixed with
"followed by a register letter to operate on a specific register (e.g."addwill delete the line and place it in register a;"apwill then paste this register)- Also, with
yandd, using a captial register letter will append to the register instad of overwriting it
- Also, with
- Also note that the special register
+synchronizes with the system clipboard (e.g."+pwill paste the clipboard contents;"+yywill copy a line to the clipboard)
- The above commands can be prefixed with
xwill delete the character under the cursor>followed by a motion indents the lines indicated by the motion<followed by a motion un-indents the lines indicated by the motion.repeats the last command
- use
:helpto view Vim's help - use
:help <topic>to jump to a topic - You may notice some help text that is highlighted. These are tags. Position the cursor on the text and press
CTRL-]to jump to the tagged topic. UseCTRL-Tto return to the previous topic. - use
:helpgrep <text>to search for<text>in the help- use
:cnto go to the next search result- You can use
@:to repeat the:cncommand
- You can use
- use
/allows to search for the next occurrence of a regex?allows to search for the previous occurrence of a regexnrepeats the previous search in the same directionNrepeats the previous search in the opposite direction
*jumps to the next occurrence of the identifier under the cursor#jumps to the previous occurrence of the identifier under the cursor
CTRL-Fscrolls down one screenCTRL-Bscrolls up one screenztplaces the current line at the top of the screenzbplaces the current line at the bottom of the screenzzplaces the current line at the middle of the screen- (This is not the same as
ZZ, which saves and quits Vim)
- (This is not the same as
You can mark a location in a file and return to it later. Every buffer has its own set of lowercase marks. Uppercase marks are global and can be used to jump between files.
mfollowed by a letter sets a mark at the cursor position (e.g.masets marka)'followed by a letter jumps to the mark
You can have a tags file which contains the locations of function/class/method
definitions so that you can easily jump to the definition from other places.
See :help ctags for a list of programs that can generate tags files for
various programming languages.
:tag myfuncjumps to the definition ofmyfuncCTRL-]jumps to the definition of the function/method under the cursorCTRL-Tjumps to the previous location (before the lastCTRL-])CTRL-W ]jumps to the definition of the function/method under the cursor in a new windowCTRL-W }jumps to the definition of the function/method under the cursor in the preview window
When in Insert mode, use CTRL-P to quickly enter words that appear elsewhere in the document. Use CTRL-P and CTRL-N to scroll through the list. Use CTRL-Y to accept ("yes") or CTRL-E to cancel ("escape" or "exit").
When in Insert mode, use CTRL-X to enter Insert mode completion. See :help ins-completion.
Useful subcommands:
CTRL-X CTRL-F: Filename completion.CTRL-X CTRL-K: Completion from one or more dictionary files (see:help dictionary).CTRL-X CTRL-T: Completion from one or more thesaurus files (see:help thesaurus).CTRL-X CTRL-SorCTRL-X s: If spelling is enabled, complete spelling suggestions.CTRL-X CTRL-]: Tag completion.CTRL-X CTRL-O: Omni-complete. This allows for completion based on filetype. See:help compl-omni-filetypes.- When the popup menu is displayed, use
CTRL-NandCTRL-Pto scroll through the list. You can also repeat the subcommand (e.g.CTRL-Ffor file completion) as a shortcut forCTRL-N. CTRL-Y: Accept completion.CTRL-E: Cancel completion.
The command :setlocal spell spelllang=en_us enables spell checking.
Spell files for other languages can be placed in ~/.vim/spell (UNIX)
or %USERPROFILE%\vimfiles\spell (Windows).
Use the following commands with the spell checker:
]sgoes to the next misspelled word[sgoes to the previous misspelled wordz=suggests correctly spelled words for the word under the cursor.- In Insert mode, use
CTRL-X CTRL-SorCTRL-X sto find spelling suggestions. zgmarks a word as "good" (i.e. not misspelled). This will be saved in your spell file.zGmarks a word as good, but does not add it to the spell file. This will be lost when you exit Vim.zwmarks a word as misspelled. This will be saved in your spell file.zWmarks a word as misspelled, but does not add it to the spell file. This will be lost when you exit Vim.zug/zuG/zuw/zuWundozg,zG,zw, orzW
qfollowed by a register will record all following commands to the register (useqto stop recording)@followed by a register will execute the contents of the register as commands- Prefix
@with a count to execute the macro multiple times.
- Prefix
Note that these commands use the same registers as p, y, and d.
This means that you can use qa to record into register a, then "ap to paste
the commands entered; or use "ayy to yank a line into register a, then @a
to execute it. This ultimately means that you can edit a macro just like any
other text, to correct any mistakes made when recording.
:sallows for regular expression find-and-replace&repeats the last:s
:s/red/blue/replaces the first occurrence ofredon the current line withblue:s/red/blue/greplaces all occurrences ofredon the current line withblue:3,5s/red/blue/greplaces all occurrences ofredon lines 3 to 5 withblue:%s/red/blue/greplaces all occurrences ofredin the file withblue:%s/red/blue/gcasks to confirm each replacement:%s/emacs//igdeletes (note the empty replacement string) all occurrences ofemacs(ignores case):s/.*/insert(&);/wraps the entire line in a function call:s/\(".*"\) *: *\(.*\),/mymap->insert(make_pair(\1, \2));/converts a Python dictionary or JSON object literal to a C++ STL map insert
Note that the separator (the character following s) is / by tradition,
but can be (almost) any punctuation character.
This is useful if the pattern or replacement contains many / characters
(e.g. UNIX path names or URLs).
Common alternatives are :, ,, and @.
The following all work:
:s:red:blue::s@red@blue@:s,red,blue,
Visual mode allows you to easily select text to be manipulated by a command. After entering visual mode, all text between the cursor's current position and the cursor's position when you entered visual mode are operated on.
vstarts visual mode in character modeVstarts visual mode in line modeCTRL-Vstarts visual mode in block modegvstarts visual mode with the previously-selected text selectedogoes to the other end of the selected areaOis likeo, but in block mode it goes to the opposite corner
The following commands are useful when you have text selected in visual mode.
ddeletes the selected textcchanges the selected textyyanks the selected textpoverwrites the selected text with the contents of the default register- Note that these commands can be used with
"to select a different register
- Note that these commands can be used with
<and>change the indentation of the selected lines:enters the Vim command line with a prefix so that the executed command operates on the selected text
Text objects are a quick way to select and operate on various types of
structured text, such as words, sentences, paragraphs, quoted strings, blocks
delimited by any type of bracket or brace, and even XML tags.
See :help text-objects.
Vim reads files into a buffer.
You can have multiple buffers to open multiple files.
You can create new buffers using :hide edit <filename> or :hide enew.
By default, every file specified on the command line is read into a separate buffer.
By default, you must write the current buffer before switching to another buffer.
You can append a ! to the command to force the switch, but this will cause Vim to forget any changes.
:set hidden allows you to switch buffers without losing changes.
:buffersor:lslists all buffers:buffer [N]and:b[N]switch to buffer N:bnextand:bnswitch to next buffer:bpreviousand:bpswitch to previous buffer:bunloadunloads the current buffer and switches to another one (frees memory)gfopens the file under the cursor in a new buffer
Vim's viewport can be split into multiple windows. Each window can display a different buffer. Different windows can also view the same buffer (e.g. to view one part of the file while editing a different part).
The -o command-line option will cause Vim to open a window for each file on the command line.
For example, $ vim -o file1 file2 file3 will open Vim with three windows,
one each for file1, file2, and file3.
You can also specify a number of windows.
For example, $ vim -o2 file1 file2 file3 will open Vim with two windows,
one for file1 and one for file2.
You will have to switch buffers to edit file3.
-O works like -o, except that the windows are split vertically.
:splitandCTRL-W CTRL-SandCTRL-W ssplit the current window in two, horizontallyCTRL-W CTRL-^andCTRL-W ^split the current window and edit the alternate file:sbuffer [N]and:sbuffer {bufname}split the window and edit buffer N orbufname[N] CTRL-W CTRL-^and[N] CTRL-W ^split the current window and edit buffer N:vsplitandCTRL-W CTRL-VandCTRL-W vsplit the current window in two, vertically:newandCTRL-W CTRL-NandCTRL-W ncreate a new empty window:vnewcreates a new empty window, split vertically:split fileand:new fileopenfilein a new windowCTRL-W CTRL-]andCTRL-W ]split the window and jump to the tag under the cursorCTRL-W CTRL-FandCTRL-W fsplit the window and edit the file under the cursorCTRL-W Fsplits the window and edits the file and line number under the cursor:allopens a window for each open file:quitandCTRL-W CTRL-QandCTRL-W qclose the current window:onlyandCTRL-W CTRL-OandCTRL-W oclose all windows except the current
CTRL-W CTRL-(HJKL)andCTRL-W (hjkl)move the cursor to another windowCTRL-W =makes all windows equal sizeCTRL-W -makes the current window shorterCTRL-W +makes the current window tallerCTRL-W _makes the current window as tall as possibleCTRL-W >makes the current window widerCTRL-W <makes the current window narrowerCTRL-W |makes the current window as wide as possible
See :help windows.txt.
A tab page holds one or more windows. You can easily switch between tab pages, so that you have several collections of windows to work on different things.
The -p command-line option will cause Vim to open a tab page for each file on the command line.
:tabslists tab pages and the windows they contain:tabnewcreates a new tab page:tabedit fileopens a file in a new tab page:tab cmdexecutescmdand when it opens a new window open a new tab page instead:tab allopens a new tab page for each file given on the command lineCTRL-W gfopens a new tab page and edit the file under the cursorCTRL-W gFopens a new tab page and edit the file and line under the cursorCTRL-W Tmoves the current window to a new tab page:tabclosecloses the current tab page:tabonlycloses all other tab pages
:tabnextandgtgo to the next tab page:tabnext {count}and{count}gtgo to tab page{count}:tabpreviousandgTgo to the previous tab page:tabprevious {count}and{count}gTgo{count}tab pages back
:tabmove [N]moves the current tab page after tab page N:tabmove +[N]and:tabmove -[N]move the current tab page relative to its current position
See :help tabpage.txt
Diff mode is used to compare two to four files side-by-side.
You can run Vim in diff mode by running vimdiff file1 file2
or vim -d file1 file2.
You may also use gvimdiff or vim -d -g. The GUI is started then.
You may also use viewdiff or gviewdiff. Vim starts in readonly mode then.
This only works when a standard diff command is available.
See :help diffexpr.
See :help diff.txt
When Vim runs, it reads and executes an "rc" file.
This file is used to store user settings.
By default, this is a file called .vimrc or _vimrc in your $HOME folder.
(Run :help .vimrc for more info on where Vim searches for this file).
There are many settings which can be overridden in the rc file,
including :set options and key bindings.
If filetype detection is enabled, then you can use the :autocmd
command to set filetype-specific settings.
When running GVim or EVim, they will also load the .gvimrc
(after loading the .vimrc).
This can be used for GUI-specific startup commands (e.g. disabling the mouse).
I have included excerpts of my .vimrc and .gvimrc files for reference.
Vim can perform syntax coloring/highlighting for various file types.
To enable this, run the command :syntax enable.
This will keep your current color settings, which can be changed with :highlight.
If you want Vim to overrule your color settings, use :syntax on.
It may be useful to have one of these in your .vimrc.
The filetype option allows Vim to customize settings for various types of files.
Use :filetype on to enable filetype detection.
With filetype detection enabled, you can register autocommands to execute when
certain types of files are loaded.
This is done using :autocmd (and is often done in your .vimrc).
Vim indentation plugins are used to load different automatic indentation
settings based on the file type.
Use :filetype indent on to enable this.
Vim filetype plugins are used to load settings and key bindings for specific
file types.
To enable loading plugins, use :filetype plugin on.
You can combine filetype detection, plugin settings, and indentation settings
using :filetype plugin indent on.
You can install new syntax, filetype, and indentation settings in the folder
~/.vim/ (UNIX) or %USERPROFILE%\vimfiles (Windows).
Note that filetype plugins will override settings in your .vimrc.
You can over-override your plugins with plugins stored in
~/.vim/after/ (UNIX) or %USERPROFILE%\vimfiles\after (Windows).
The :filetype detect command will cause Vim to detect the filetype of the
current file (if you opened an empty file and started writing).
QuickFix mode enables Vim to parse the output of programs and open the files and lines mentioned in the output. The most common use is to parse compiler errors and jump to the offending lines.
You can save the compiler output and start Vim using the -q option:
$ g++ *.cpp > errors.txt || echo "there were errors, see errors.txt"
$ vim -q errors.txtAn easier way to do this is to use the :make command.
See Compiling Code below.
The errorformat option should be set to match the error messages from your
compiler (see :help errorformat).
:ccdisplays and jumps to the current error message:cnextdisplays and jumps to the next error message:cpreviousdisplays and jumps to the previous error message:clistlists all recognized errors:clist!lists all errors (this will also contain context from the compiler output):copenopens a new window containing the list of errors (this will also contain context from the compiler output). Press<Enter>to jump to the error under the cursor.CTRL-W <Enter>opens a new window and jumps to the error there:cclosecloses the quickfix window
:cfilere-reads the error file and jumps to the first error:cfile filenamereads error filefilename:cgetfile [filename]is like:cfile, but does not jump to the first error:cquitquits Vim with an error code, so that the compiler can abort (if the compiler opened Vim to correct the errors)
See :help quickfix.txt
The :make command can be used to compile programs.
The name comes from Make,
but you can use other build tools.
To do so, set the makeprg option to the name of your build tool
(e.g. :set makeprg=ant)
(This is best done using :autocmd in your .vimrc)
(You could also write a Makefile so make can run your compiler,
which is what make is for).
Running :make will cause Vim to save the output and enter quickfix mode.
Make sure you set errorformat before running :make
(e.g. in your .vimrc, or via a compiler plugin).
See :help :make_makeprg
Compiler plugins are used to set the makeprg and errorformat options for
specific compilers.
You can install new compiler settings in the folder ~/.vim/compiler (UNIX)
or %USERPROFILE%\vimfiles\compiler (Windows).
Use :compiler name to load a compiler plugin.
See :help compiler-select
Vim includes a standard plugin for browsing folders.
To use it, simply edit a folder (e.g. by specifying a folder name on the
command-line or using :edit).
This plugin can also be used to browse FTP sites and network shares
(the URL must end with a /).
:Exploreopens the directory containing the current file. If the file has unsaved changes, the directory will open in a new window.:Explore {dir}opens the directory containingdir. If the current file has unsaved changes, the directory will open in a new window.:Sexplorealways opens the directory in a new window:Lexplorealways opens the directory in a new window on the left side of the tab page:Texplorealways opens the directory in a new tab page:Rexploretoggles between the previously-viewed file and the previously-viewed directory
The above commands (except :Rexplore) can also be given a pattern for searching for files.
See :help netrw-star.
Use :Nexplore and :Pexplore or SHIFT-<Down> and SHIFT-<Up> to scroll through the list of matches.
<Enter>makes Vim enter the directory or edit the file under the cursoroenters the file/directory in a new windowPenters the file/directory in the last used windowventers the file/directory in a new window, using a vertical splittenters the file/directory in a new tab pageugoes to the previous directory
schanges the sorting style (by name, by time, by size)rreverses the sorting order
Rrenames a file or directorydcreates a new directoryDattempts to delete a file or directory
icycles between thin, long, wide, and tree listingscmakes the viewed directory Vim's current directory (i.e. it makes Vim:cdinto that directory)CTRL-Lrefreshes the directory listingqfdisplays information on the file under the cursor
See :help netrw-browse and :help pi_netrw.txt