Vim is great, but not perfect. The default settings are not always useful. To make Vim useful almost everyone needs to make a vimrc file with a few settings.
This is why ‘Vim distributions’ such as spf13, janus, and others exist. These setups provide people with a useful and ‘modern’ Vim experience in a short amount of time, at the price of complexity.
My first vimrc is a tool to help people set up a simple but effective vimrc for every-day use in an interactive and educational way.
This doesn’t list all possible options or things you can stick in your vimrc, only the most useful and common ones that a reasonable percentage of new(-ish) users may want to use. All options come with a detailed description of what they do. Read the descriptions!
Completely new to Vim? Start with vimtutor.
Vim’s help pages should be your first stop if you’re confused about something. See
:help
help-summary
for instructions on how to use Vim’s excellent help system.
A great many questions can be solved by effective searching and reading of the help pages.
Still stuck or want to learn more? These places might be of help:
:help
quickref
is also very handy.This list does not contain any plugins yet. This is not because I am opposed to them, but rather because I haven't had the time to create a curated list. In the meanwhile, there are already a number of ‘best Vim plugins’ out there already.
You can save this webpage to your computer for offline use. It is a single HTML page.
Copy the output file to $HOME/.vim/vimrc
. Make the directory if it doesn’t exist
yet.
Vim will also load $HOME/.vimrc
, but the above locations are recommended. You may
want to check if your system has this file already to make sure you’ve got only one vimrc
file.
Ensure that Vim won't try to be compatible with the now-archaic 1970s vi. It influences a lot of Vim behaviour.
This is not strictly required when loading from
~/.vimrc
or ~/.vim/vimrc
– as Vim will
automatically set it when loading from those files – but it's
not set automatically when loaded as vim -u
test.vim
or :source test.vim
.
Most people won't load it like that, but a single line is easy enough to add and will avoid very confusing errors down the line.
You must keep this at the top, since changing the value resets many options. See nocompatible.
Vim ships with support for many languages, which is known as a filetype in Vim jargon. A filetype usually comes with a syntax file to control the syntax highlighting, but may also come with specific rules for indentation and may set settings specific for this filetype.
The default is to not use any of this though. This will enable detection of the filetype, which is usually done based on the filename's extension or the first line, and can be manually overridden with a modeline.
You almost certainly want to keep this.
Enable syntax highlighting for this filetype. This will implicitly enable filetype detection ("filetype on").
You almost certainly want to keep this.
Vim's default behaviour when pressing the backspacing is somewhat peculiar, it won’t allow you to backspace to the previous line, automatically inserted indentation, or previously inserted text.
With this value you will be able to to backspace over everything in insert mode, which is how almost any other application behaves.
Also see Why doesn't the backspace key work in insert mode?.
By default Vim will always place the cursor in the first column when starting a new line. When autoindent is enabled it will use the same indentation as the previous line, which is often what you want.
Note that there are more options to control indentation: cindent and smartindent. You usually want to leave those alone as they will cause confusing behaviour in many programming languages. A filetype that benefits from these will usually set one of those options, or use a custom indentexpr.
By default Vim will display only "@" characters if the last line on the screen won't fit when wrap is enabled.
If this is enabled Vim will display as much as the last line as will fit and display "@@@" at the end.
There is another useful option for display that I rather like: "uhex". This will make Vim show unprintable characters as <xx> rather than ^L (Use i_CTRL-V in insert mode to see the difference.)
Make commandline-completion (after you type :
)
behave more useful and roughly like most shells do.
See wildmode on how to configure the completion mode to your liking. wildignore is also a useful setting to ignore binary files such as compiler output, images, etc.
Vim has many commands that consist of two or more keystrokes. If this option is enabled Vim will show the command you've typed thus-far in the bottom-right of the screen.
It will also show the size of the selection in visual-mode.
Vim will insert ‘real’ tabs by default. Setting this will make it insert spaces.
Note that some filetypes may set this option to match that
filetype's standard. See :verbose set filetype
to
see where it was last set. You can use an autocmd to override
the filetype settings.
Remove shiftwidth worth of spaces on backspace – like most editors – instead of just a single space.
Vim won't directly show line numbers by default. This will enable a column with line numbers on the left-hand side of the screen.
You can also show the current line number in the statusline by setting ruler, or pressing CTRL-G or g_CTRL-G.
Also see relativenumber for showing line numbers as relative to the current line, numberwidth for controlling the width, and hl-LineNr for controlling the colours.
When long lines are not wrapped Vim will hide any text that's larger than the screen and scroll horizontally.
Also see breakindent for continuing wrapped lines on the same indent level (requires Vim 7.4.338) and linebreak to wrap only at the end of words.
You can also use gj and gk to navigate "visual" lines more easily. Many people line to override the default behaviour by remapping keys with something like:
nnoremap k gk nnoremap j gj nnoremap <Up> gk nnoremap <Down> gj inoremap <Down> <C-o>gj inoremap <Up> <C-o>gk
By default Vim will show the statusline only if there are two or more windows.
The statusline displays useful information about the current buffer and cursor position, so it’s useful to always show it.
Also see statusline for controlling/expanding the information shown here.
The ruler is shown on the right side of the statusline and usually contains information about the current cursor position and the like.
Also see laststatus to enable displaying the statusline and rulerformat for configuring what’s displayed here.
Automatically break lines at n characters.
Jump to the first match while typing the pattern with /.
The cursor will jump back to the original position when aborting (<ESC> or CTRL-C).
The last used search pattern is stored in the / register. This will highlight whatever is in that pattern.
You can use :nohlsearch to clear the highlighting. Many people like to map this to e.g. CTRL-L:
nnoremap <silent> <C-l> :nohlsearch<CR><C-l>
Case-insensitive searching unless the pattern contains an upper case letter or if /\C is in the pattern.
This controls how Vim should interpret numbers when pressing CTRL-A or CTRL-X to increment to decrement a number. By default numbers starting with a 0 are treated as octal numbers, which can be rather confusing, so remove that.
Number of spaces to display tab characters as.
Keep lines above and below the screen when scrolling up or down. This is useful so you have some context what you’re scrolling to.
Also see sidescrolloff.
The formatoptions setting controls how automatic formatting when inserting text, formatting with gq, as well as some other commands.
n
– Recognize numbered lists when formatting (see formatlistpat).
c
– Wrap comments with textwidth.
r
– Insert comment char after enter.
o
– Insert comment char after o/O.
q
– Format comments with gq.
l
– Do not break lines when they were longer than textwidth to start with.
j
– Remove comment character when joining lines with J.
~/.vim/tmp
By default Vim will store various files in the current directory. These files are useful, but storing them in the current directory next to the original file usually isn’t.
With this enabled Vim will store all these files in the user’s home directory.
Store undo history to a file, so that it can be recalled in future Vim sessions.
Also see How can I use the undofile? and Can I be notified when I'm undoing changes from the undofile?.
The matchit.vim plugin that comes bundled with Vim expands
the % key to work with various programming language
keywords (e.g. jumping between if
and
end
in Ruby).
Selecting some text in visual-mode and then changing the indentation with v_> and v_< will make Vim lose the visual selection, which is annoying if you want to change several levels of indentation.
With this mapping it will re-select the last selection after using < or >.
Go to the last cursor location when a file is opened, unless this is a git commit (in which case it's annoying).
This uses the information stored in the viminfo file.
This command removes all trailing whitespace in a file, without causing any side-effects.
Also see What's the simplest way to strip trailing whitespace from all lines in a file?.
In “wizard mode” you will step through all the settings one-by-one, asking you for your choice. This is great if you really want to learn about Vim and grok what all the settings do. It is recommended you also read through all the related help pages, as they will often contain more information or links to related settings.
The current wizard step and selected options are saved in the URL, so if you save/bookmark it you will be able to continue from where you stopped.
You can also switch back to “list mode” at any time. You will continue from the step where you left when going back to “wizard mode”.
set nocompatible " Vim defaults rather than vi ones. Keep at top. filetype plugin indent on " Enable filetype-specific settings. syntax on " Enable syntax highlighting. set backspace=2 " Make the backspace behave as most applications. set autoindent " Use current indent for new lines. set display=lastline " Show as much of the line as will fit. set wildmenu " Better tab completion in the commandline. set wildmode=list:longest " List all matches and complete to the longest match. set showcmd " Show (partial) command in bottom-right. set smarttab " Backspace removes 'shiftwidth' worth of spaces. set wrap " Wrap long lines. set laststatus=2 " Always show the statusline. set ruler " Show the ruler in the statusline. set textwidth=80 " Wrap at n characters. set nrformats-=octal " Remove octal support from 'nrformats'. set tabstop=4 " Size of a Tab character. set shiftwidth=0 " Use same value as 'tabstop'. set softtabstop=-1 " Use same value as 'shiftwidth'.