Getting started with vim
Vim (or "Vi IMproved") is a console-based multi-mode (modal) text editor. It is widely used and available by default on all Unix, Linux, and Apple OS X systems. Vim has a large active community and a wide user base. The editor supports all popular programming languages, and many plugins are available to extend its features.
Developers like the editor for its speed, many configuration options, and powerful expression based editing. In "command" mode the editor is controlled by keyboard commands, so the user is not distracted by a GUI or mouse pointer.
Vim is based on the earlier Unix "vi" editor created in the seventies and it has been in continuous development since 1991. With macros and plugins the editor offers most of the features of a modern IDE. It is also uniquely capable of processing large amounts of text with its scripting language (vimscript) and regular expressions.
- editing modes
- basic editing
- advanced editing
- related projects
Run interactive vim tutorials as many times as needed to feel comfortable with the basics.
Vim features several modes, e.g. normal mode, insert mode and command-line mode.
Normal mode is for editing and navigating text. In this mode
l correspond to the cursor keys
→. Most commands in normal mode can be prefixed with a "count", e.g.
3j moves down 3 lines.
Insert mode is for inserting the text directly, in this mode vim is similar to other more simple text editors. To enter insert mode press
i in normal mode. To leave it press
<ESC> (escape key).
Command-line mode is for running more complex commands like saving the file and exiting vim. Press
: to start the command-line mode. To leave this mode you can also press
<ESC>. To save the changes to the file use
:write). To exit vim without saving your changes use
These are some of the more useful commands in vim:
|(insert) enters insert mode before the current cursor position|
|enters insert mode before the first printable character of the current line|
|(append) enters insert mode after the current cursor position|
|enters insert mode after the last printable character of the current line|
|delete character at the current cursor position|
|delete character at the left to the current cursor position|
|move to next word|
|move to previous word|
|move to the beginning of line|
|move to the end of line|
|replace – enters replace mode for one character. The next character you type will replace the character under the cursor.|
|enters replace mode indefinitely. Every character you type will replace the character under the cursor and advance the cursor by one.|
|substitute – deletes the character at the current cursor position and then enters insert mode|
|delete the current line that the cursor is currently on and enter insert mode|
|exit insert mode and returns to normal mode|
|cut the current line, from the cursor to next word, or the character, current position to end of current line respectively, note: |
|change the current line, from the cursor to next word, or the character, respectively|
|yank ("copy") the current line, from the cursor to next word, or the character, current position to end of current line respectively|
|put ("paste") after, or before current position, respectively|
|to create a new empty line, after or before the current one and enter insert mode|
|write the current buffer to disk|
|quit without writing|
|write and quit|
|open a window with help file|
|show help for a specific subject|
|begin recording actions to register |
In order to exit Vim, first make sure you are in Normal mode by pressing Esc.
:qEnter (will prevent you from exiting if you have unsaved changes - short for :quit)
To discard changes and exit Vim:
:q!Enter to force exit and discard changes (short for
:quit!, not to be confused with
ZQis a shortcut that does the same as
:cqEnter quit and return error (discard all changes so the compiler will not recompile this file)
To save changes and exit Vim:
:wqEnter (shorthand for
:xEnter (same as
:wq, but will not write if the file was not changed),
ZZis a shortcut that does the same as
:x(Save workspace and quit the editor),
:[range]wq!Enter (write the lines in [range])
To close multiple buffers at once (even in multiple windows and/or tabs), append the letter
a to any of the Commands above (the ones starting with
:). For example, to write and quit all windows you can use:
:xaEnter — Write all changed buffers and exit Vim. If there are buffers without a file name, which are readonly or which cannot be written for another reason, Vim will not quit
:xa!Enter — Write all changed buffers, even the ones that are readonly, and exit Vim. If there are buffers without a file name or which cannot be written for another reason, Vim will not quit
:qaEnter — try to quit, but stop if there are any unsaved files;
:qa!Enter — quit without saving (discard changes in any unsaved files)
If you have opened Vim without specifying a file and you want to save that file before exiting, you will receive
E32: No file name message. You can save your file and quit using:
:wq filenameEnter or;
The : keystroke actually opens Command mode. The command
q is an abbreviation of
exit (you can also type
:exit if you want). Shortcuts not starting with
: such as
ZQ refer to Normal mode key mappings. You can think of them as shortcuts.
! keystroke is sometimes used at the end of a command to force its execution, which allows to discard changes in the case of
! at the beginning of the command has a different meaning.
For example, one can mistype
:!q instead of
:q! and vim would terminate with a 127 error.
An easy way to remember this is to think of
! as a way of insisting on executing something.
Just like when you write: "I want to quit!"
The Vim on your machine—if there is one—is very likely to be a "small" build that lacks useful features like clipboard support, syntax highlighting or even the ability to use plugins.
This is not a problem if all you need is a quick way to edit config files but you will soon hit a number of walls if you intend to make Vim your main editor.
It is therefore generally recommended to install a complete build.
Installation on Linux/BSD
On those systems, the trick is simply to install the GUI version which comes with both a
gvim command for starting the GUI and a
vim command for starting the TUI.
Arch and Arch-based distributions
Debian and Debian-based distributions
Gentoo and Gentoo-based distributions
RedHat and RedHat-based distributions
Slackware and Slackware-based distributions
OpenBSD and OpenBSD-based distributions
FreeBSD and FreeBSD-based distributions
Installation on Mac OS X
The strategy is similar to Mac OS X: we install the GUI version to get both the GUI and the TUI. In the end, we should be able to:
- double-click the MacVim icon in the Finder,
- click on the MacVim icon in the Dock,
$ mvimin the shell to open the MacVim GUI,
$ mvim -vin the shell to open the MacVim TUI.
Download and install an official snapshot like you would with any other Mac OS X application.
mvim script that comes bundled with MacVim somewhere in your
Package managerMacPorts: Homebrew:
To make MacVim the default console Vim:
Installation on Windows
There is no Vim on Windows systems by default. You can download and install Vim from the Tuxproject site for more up-to-date and complete builds or you can download and install Vim from the official Vim site.
Building Vim from source
If the methods above don't suit your needs it is still possible to build Vim yourself, with only the options you need.
This topic will be discussed in its own section (currently in draft).
Interactive Vim Tutorials (such as vimtutor)
vimtutor is an interactive tutorial covering the most basic aspects of text editing.
On UNIX-like system, you can start the tutorial with:
On Windows, “Vim tutor” can be found in the “Vim 7.x” directory under “All Programs” in the Windows menu.
:help vimtutor for further details.
Other interactive tutorials include these browser-based ones:
- Vim Adventures – An interactive game version of vimtutor available on the web. Only the first few levels are free.
- Open Vim – An interactive terminal which teaches you the basic commands with feedback.
- Vim Genius – Another interactive terminal which also includes intermediate and advanced lessons including macros and arglist.
Saving a read-only file edited in Vim
Sometimes, we may open a file which we do not have permission to write in Vim without using
Use this command to save a read-only file edited in Vim.
Which you could map to
:w!! in your
You will be presented a prompt as shown in the image.
O and the file will be saved. It remains open in vi/vim for more editing or reading and you can exit normally by typing
:q! since the file is still open as read-only.
vim from the command line, you can suspend
vim and get back to your prompt, without actually quitting
vim. Hence you will later be able to get back your
vim session from the same prompt.
When in Normal mode (if not, press esc to get there), issue either of these commands:
Alternatively, on some systems, when in Normal or Visual mode, issuing Ctrl+Z will have the same effect.
autowrite is set, buffers with changes and filenames will be written out. Add a
! before enter to avoid, eg.
Later, when you want to return to your
vim session, if you haven't suspended any other jobs, issuing the following will restore vim as your foreground job.
Otherwise you will need to find your
vim sessions's job ID by issuing
jobsenter and then foregrounding the matching jobs
fg %[job ID]enter eg.
What to do in case of a crash
Vim saves all your unsaved edits in a swap file, an extra file that gets deleted once the changes are committed by saving. The name of the swap file is usually the name of the file being edited preceded by a
. and with a
.swp suffix (you can see it with
So in case your vim process terminates before you've had the chance to save your edits you can recover your work by applying the changes contained in the swap file to your current file by using the command-line option
-r. For instance if
myFile is the file you were editing, use:
to recover the uncommitted changes.
If a swap file exists, vim should prompt you anyway for recovery options
If you choose (R)ecover then the changes from the
swp file are applied but the swap file won't be deleted, so don't forget to delete the swap file afterwards if you're satisfied with the recovery.