A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learnbyexample.github.io/vim_reference/Customizing-Vim.html below:

Customizing Vim - Vim Reference Guide

Customizing Vim

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:

Editing vimrc

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:

To 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.

defaults.vim

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.

Alternatively, you can copy only the parts you want to retain from the defaults.vim file to your vimrc file.

General Settings

set syntax and guidelines were introduced in the Setting options section.

: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 the vimrc, which will be discussed in the CLI options chapter.

Further Reading

Text and Indent Settings Search Settings Custom mapping

Mapping 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:

The following will not be discussed, but you might find it useful to know or explore further:

: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.

Normal mode

See :h map-which-keys to know which keys are not already Vim commands, which ones are not commonly used, etc.

See :h key-notation for a list of keys that can be represented using the <> notation.

Map leader

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.

See learnvimscriptthehardway: Leaders for more examples and details.

Insert mode

Use noremap! if you want a mapping to work in both Insert and Command-line modes.

Visual mode

Note that xnoremap is used here since vnoremap affects both Visual and Select modes.

Abbreviations

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 &LTEsc> that ends insert mode or the &LTCR> 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 &LTC-]>, which is used to expand an abbreviation without inserting any extra characters.

See :h 24.7 for more details about using abbreviations.

Matching Pairs

To match keywords like if-else pairs with %, you can use the matchit.vim plugin. This supports filetypes such as HTML, Vim, LaTeX, XML, etc. See :h matchit-install for more details.

GUI options

See :h guioptions for more details.

Third-party customizations

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.

plugin

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:

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.

vim-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.

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
color scheme

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.

See also this collection of awesome color schemes for Vim.

autocmd

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

Note 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 for vim9script. See vim9-conversion-aid for upgrading old scripts.

See also:

Further Reading

RetroSearch 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