Settings like indentation and keyword-pairs can vary between different programming languages and file types. You might need to adapt style guides based on client requirements. Or perhaps, you wish to create or override commands to suit your preferences.
This chapter will discuss how you can customize Vim for different purposes. Some of the settings will be specific to GVim.
Documentation links:
From :h usr_41.txt and :h vimrc-intro:
The Vim script language is used for the startup vimrc file, syntax files, and many other things.
The vimrc file can contain all the commands that you type after a colon. The simplest ones are for setting options.
This chapter only covers some use cases. You'll see what some of the settings do, how to use mappings, abbreviations and so on. Not much will be discussed about the programming aspects of Vim script. Make sure you have a vimrc
file using the following details:
vimrc
file, you can use this predefined variable to open itvimrc
file should be located for your OSdefaults.vimTo view a sample
vimrc
file, I have one on GitHub. More resources are mentioned in the Further Reading section at the end of this chapter.
If you haven't created a vimrc
file, the defaults.vim
file that comes with Vim installation will be used. This file aims to provide saner defaults like enabling syntax highlighting, filetype settings and so on.
vimrc
file if you want to keep these defaultsdefaults.vim
Alternatively, you can copy only the parts you want to retain from the defaults.vim
file to your vimrc
file.
set
syntax and guidelines were introduced in the Setting options section.
:
commands, search patterns, etcbash
-like tab completion
:h 'history' will give you the documentation for the given option (note the use of single quotes).
You can use these settings from the Command-line mode as well, but will be active for the current Vim session only. Settings specified in the
vimrc
file will be loaded automatically at startup. You can also load a different file as thevimrc
, which will be discussed in the CLI options chapter.
Further Reading
plugin
and indent
files
indent
and plugin
directories would be present in this path)indent
setting80
characters as the limit
0
which disables this setting80
highlight ColorColumn
setting to customize the color for this vertical bar8
)8
)hlsearch
settingMapping helps you to create new commands or redefine existing ones. You can restrict such mappings for specific modes as well. Only the following settings will be discussed in this chapter:
nnoremap
Normal mode non-nested, non-recursive mappingxnoremap
Visual mode non-nested, non-recursive mappinginoremap
Insert mode non-nested, non-recursive mappinginoreabbrev
Insert mode non-nested, non-recursive abbreviationThe following will not be discussed, but you might find it useful to know or explore further:
nmap
, xmap
, imap
and iabbrev
allows nested and recursive mappingsnunmap
, xunmap
, iunmap
and iunabbrev
unmaps the given command (usually used from Command-line mode to temporarily disable a mapping, will be available again on startup if it was defined in vimrc
)
mapclear
instead of unmap
to clear all the mappings for that particular modeonoremap
(or omap
) map a motion or text object to be used with commands like d
or y
command
helps you create a Command-line mode command, see :h 40.2 and :h user-commands for detailsNormal mode:nmap, :xmap, :imap and :iab will list all the current mappings for that particular mode. You can provide an argument to display the mapping for that particular command, for example :nmap Y. See :h key-mapping and :h map-overview for reference manuals.
<F2>
represents the F2 function key and <CR>
represents the Enter key<silent>
modifier executes the command without displaying in the Command-line areaSee :h map-which-keys to know which keys are not already Vim commands, which ones are not commonly used, etc.
Map leaderSee :h key-notation for a list of keys that can be represented using the
<>
notation.
Normal mode commands are already crowded, so if you are looking to create new commands, using a leader mapping can help you out. You can define a key that'll serve as a prefix for these new set of commands. By default, the backslash key is used as the leader key.
mapleader
hasn't been set, using \f
will auto indent the code for the whole file;
;f
since the leader key was changedInsert modeSee learnvimscriptthehardway: Leaders for more examples and details.
Visual modeUse noremap! if you want a mapping to work in both Insert and Command-line modes.
AbbreviationsNote that
xnoremap
is used here sincevnoremap
affects both Visual and Select modes.
Abbreviations are usually used to correct typos and insert frequently used text. From :h abbreviations documentation:
An abbreviation is only recognized when you type a non-keyword character. This can also be the
<Esc>
that ends insert mode or the<CR>
that ends a command. The non-keyword character which ends the abbreviation is inserted after the expanded abbreviation. An exception to this is the character<C-]>
, which is used to expand an abbreviation without inserting any extra characters.
inoreabbrev p #!/usr/bin/env perl<CR>use strict;<CR>use warnings;<CR> expand p
to the text as shown in the code snippet below
#!/usr/bin/env perl
use strict;
use warnings;
inoreabbrev py #!/usr/bin/env python3 expand py
to #!/usr/bin/env python3
py
literally (for example, script.py
)[p
or @p
insteadinoreabbrev teh the automatically correct teh
typo to the
inoreabbrev @a always @()<CR>begin<CR>end<Esc>2k$ expand @a
to the text as shown in the code snippet below
@a
followed by the Esc key to place the cursor at the end of the first linealways @()
begin
end
:abbreviate or :ab list all abbreviations
Matching PairsSee :h 24.7 for more details about using abbreviations.
<>
to the list of pairs matched by the % command in Normal modeGUI optionsTo match keywords like
if
-else
pairs with %, you can use thematchit.vim
plugin. This supports filetypes such as HTML, Vim, LaTeX, XML, etc. See :h matchit-install for more details.
Third-party customizationsSee :h guioptions for more details.
See :h 'runtimepath' to know the path within which you can add the plugins and packages discussed in this section.
~/.vim
is commonly used on Unix/Linux systems.
Make sure to backup your directory (~/.vim
for example) and the vimrc
file, so that you can easily apply your customizations on a new machine.
Some plugins are loaded by default. Some come with Vim installation but you have to explicitly enable them. You can also write your own or add plugins written by others. From :h add-plugin:
Vim's functionality can be extended by adding plugins. A plugin is nothing more than a Vim script file that is loaded automatically when Vim starts.
There are two types of plugins:
global plugin: Used for all kinds of files
filetype plugin: Only used for a specific type of file
If you want to add a global plugin created by you or someone else, place it in the plugin
directory. If you don't have that directory yet, you can create it using the below command (assuming Unix/Linux):
$ mkdir -p ~/.vim/plugin
$ cp plugin_file.vim ~/.vim/plugin/
If you have multiple related plugin files, you can put them under a subdirectory:
$ mkdir -p ~/.vim/plugin/python
$ cp file_1.vim file_2.vim ~/.vim/plugin/python/
If you want to add plugins that should work based on a specific filetype, add them to the ftplugin
directory:
$ mkdir -p ~/.vim/ftplugin
$ cp ftplugin_file.vim ~/.vim/ftplugin/
package
Packages make it easy to manage projects that require multiple plugins, use a version controlled repository directly and so on. See :h packages for more details. From :h add-package:
A package is a set of files that you can add to Vim. There are two kinds of packages: optional and automatically loaded on startup.
The Vim distribution comes with a few packages that you can optionally use. For example, the matchit plugin.
matchit
package
!
is used to prevent loading this plugin when Vim is started with the --noplugin
CLI optionvim-surround is used here as an example for a third-party package. Installation instructions (provided in this repository) are shown below, assuming you want to enable this package at startup:
# 'pack' is the directory for packages
# 'tpope' subdirectory is useful to group all packages by this author
# 'start' means this package will be loaded at startup
$ mkdir -p ~/.vim/pack/tpope/start
# go to the directory and clone the git repository
# you can then update the repository when new changes are needed
$ cd ~/.vim/pack/tpope/start
$ git clone https://github.com/tpope/vim-surround.git
When you start Vim after the above steps, vim-surround
will be automatically active. Couple of examples are shown below, see the repository linked above for more details.
[]
, for example hello
to [hello]
"hi bye"
to 'hi bye'
If you want to enable this package optionally, put it under opt
directory instead of start
.
# 'opt' makes it optional
$ mkdir -p ~/.vim/pack/tpope/opt
$ cd ~/.vim/pack/tpope/opt
$ git clone https://github.com/tpope/vim-surround.git
vimrc
(usually under some condition)There are different ways to add a new color scheme. The simplest is to copy the theme.vim
file to the ~/.vim/colors
directory. Or, follow the installation steps provided by the theme creators. Here are couple of solarized themes you can check out:
After installation, you can use the :colorscheme command to set the new theme. If the theme offers multiple variations, you might need additional settings like set background=dark
or set background=light
. See the installation instructions provided in the above repositories for more details.
See Where to put what section under :h packages for more details about installation directories.
autocmdSee also this collection of awesome color schemes for Vim.
From :h 40.3:
An autocommand is a command that is executed automatically in response to some event, such as a file being read or written or a buffer change.
Autocommands are very powerful. Use them with care and they will help you avoid typing many commands. Use them carelessly and they will cause a lot of trouble.
Syntax from the reference manual is shown below:
:au[tocmd] [group] {event} {aupat} [++once] [++nested] {cmd}
Here's an example for Python files:
augroup pyg
autocmd!
" add Python shebang for a new buffer with .py extension
" py abbreviation was discussed earlier in this chapter
autocmd BufNewFile *.py normal ipy
" Black command is provided by a Python code formatter plugin
autocmd BufWritePre *.py Black
augroup END
autocmd BufNewFile *.py normal ipy
BufNewFile
event that triggers on editing a file that doesn't already exist*.py
filenames ending with .py
(similar to shell wildcards)normal ipy
command to be executed (normal
is needed here since by default commands are treated as Command-line mode)autocmd BufWritePre *.py Black
BufWritePre
event that triggers on writing a fileBlack
command to be executed (see black vim plugin documentation for more details)augroup
helps you to group related autocommandspyg
in the above example)
vimrc
fileNote that in earlier versions of Vim, double quotes is used for comments as shown in the above snippet. You'll need to use the
#
character instead forvim9script
. See vim9-conversion-aid for upgrading old scripts.
See also:
version 7.3
)vimrc
reference, tips and generation
vimrc
by selecting optionsRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4