12 min read

(Read more interesting articles on Hacking Vim 7.2 here.)

Some of these tasks contain more than one recipe because there are different aspects for personalizing Vim for that particular task. It is you, the reader, who decides which recipes (or parts of it) you would like to read and use.

Before we start working with Vim, there are some things that you need to know about your Vim installation, such as where to find the configuration files.

Where are the configuration files?

When working with Vim, you need to know a range of different configuration files. The location of these files is dependent on where you have installed Vim and the operating system that you are using.

In general, there are three configuration files that you must know where to find:

  • vimrc
  • gvimrc
  • exrc

The vimrc file is the main configuration file for Vim. It exists in two versions—global and personal.

The global vimrc file is placed in the folder where all of your Vim system files are installed. You can find out the location of this folder by opening Vim and executing the following command in normal mode:

:echo $VIM

The examples could be:

  • Linux: /usr/share/vim/vimrc
  • Windows: c:program filesvimvimrc

The personal vimrc file is placed in your home directory. The location of the home directory is dependent on your operating system. Vim was originally meant for Unixes, so the personal vimrc file is set to be hidden by adding a dot as the first character in the filename. This normally hides files on Unix, but not on Microsoft Windows. Instead, the vimrc file is prepended with an underscore on these systems. So, examples would be:

  • Linux: /home/kim/.vimrc
  • Windows: c:documents and settingskim_vimrc

Whatever you change in the personal vimrc file will overrule any previous setting made in the global vimrc file. This way you can modify the entire configuration without having to ever have access to the global vimrc file.

You can find out what Vim considers as the home directory on your system by executing the following command in normal mode:

:echo $HOME

Another way of finding out exactly which vimrc file you use as your personal file is by executing the following command in the normal mode:


:echo $MYVIMRC

The vimrc file contains ex (vi predecessor) commands, one on each line, and is the default place to add modifications to the Vim setup. In the rest of the article, this file is just called vimrc.

Your vimrc can use other files as an external source for configurations. In the vimrc file, you use the source command like this:

source /path/to/external/file

Use this to keep the vimrc file clean, and your settings more structured. (Learn more about how to keep your vimrc clean in Appendix B, Vim Configuration Alternatives).

The gvimrc file is a configuration file specifically for Gvim. It resembles the vimrc file previously described, and is placed in the same location as a personal version as well as a global version. For example:

  • Linux: /home/kim/.gvimrc and /usr/share/vim/gvimrc
  • Windows: c:documents and settingskim_gvimrc and c:program filesvimgvimrc

This file is used for GUI-specific settings that only Gvim will be able to use. In the rest of the article, this file is called gvimrc.

The gvimrc file does not replace the vimrc file, but is simply used for configurationsthat only apply to the GUI version of Vim. In other words, there is no need to haveyour configurations duplicated in both the vimrc file and the gvimrc file.

The exrc file is a configuration file that is only used for backwards compatibility with the old vi / ex editor. It is placed at the same location (both global and local) as vimrc, and is used the same way. However, it is hardly used anymore except if you want to use Vim in a vi-compatible mode.

Changing the fonts

In regular Vim, there is not much to do when it comes to changing the font because the font follows one of the terminals. In Gvim, however, you are given the ability to change the font as much as you like.

The main command for changing the font in Linux is:

:set guifont=Courier 14

Here, Courier can be exchanged with the name of any font that you have, and 14 with any font size you like (size in points—pt).

For changing the font in Windows, use the following command:

:set guifont=Courier:14

If you are not sure about whether a particular font is available on the computer or not, you can add another font at the end of the command by adding a comma between the two fonts. For example:

:set guifont=Courier New 12, Arial 10

If the font name contains a whitespace or a comma, you will need to escape it with a backslash. For example:

:set guifont=Courier New 12

This command sets the font to Courier New size 12, but only for this session. If you want to have this font every time you edit a file, the same command has to be added to your gvimrc file (without the : in front of set).

In Gvim on Windows, Linux (using GTK+), Mac OS, or Photon, you can get a font selection window shown if you use this command:

:set guifont=*.

If you tend to use a lot of different fonts depending on what you are currently working with (code, text, logfiles, and so on.), you can set up Vim to use the correct font according to the file type. For example, if you want to set the font to Arial size 12 every time a normal text file (.txt) is opened, this can be achieved by adding the following line to your vimrc file:


autocmd BufEnter *.txt set guifont=Arial 12

The window of Gvim will resize itself every time the font is changed. This means, if you use a smaller font, you will also (as a default) have a smaller window. You will notice this right away if you add several different file type commands like the one previously mentioned, and then open some files of different types. Whenever you switch to a buffer with another file type, the font will change, and hence the window size too.

You can find more information about changing fonts in the Vim help system under Help | guifont.

Changing color scheme

Often, when working in a console environment, you only have a black background and white text in the foreground. This is, however, both dull and dark to look at. Some colors would be desirable.

As a default, you have the same colors in the console Vim as in the console you opened it from. However, Vim has given its users the opportunity to change the colors it uses. This is mostly done with a color scheme file. These files are usually placed in a directory called colors wherever you have installed Vim.

You can easily change the installed color schemes with the command:

:colorscheme mycolors

Here, mycolors is the name of one of the installed color schemes. If you don’t know the names of the installed color schemes, you can place the cursor after writing:

:colorscheme

Now, you can browse through the names by pressing the Tab key. When you find the color scheme you want, you can press the Enter key to apply it.

The color scheme not only applies to the foreground and background color, but also to the way code is highlighted, how errors are marked, and other visual markings in the text.

You will find that some color schemes are very alike and only minor things have changed. The reason for this is that the color schemes are user supplied. If some user did not like one of the color settings in a scheme, he or she could just change that single setting and re-release the color scheme under a different name.

Play around with the different color schemes and find the one you like. Now, test it in the situations where you would normally use it and see if you still like all the color settings. While learning Basic Vim Scripting, we will get back to how you can change a color scheme to fit your needs perfectly.

Personalizing Vim

Personalizing Vim

Personal highlighting

In Vim, the feature of highlighting things is called matching.

With matching, you can make Vim mark almost any combination of letters, words, numbers, sentences, and lines. You can even select how it should be marked (errors in red, important words in green, and so on).

Matching is done with the following command:

:match Group /pattern/

The command has two arguments. The first one is the name of the color group that you will use in the highlight.

Compared to a color scheme, which affects the entire color setup, a color group is a rather small combination of background (or foreground) colors that you can use for things such as matches. When Vim is started, a wide range of color groups are set to default colors, depending on the color scheme you have selected. To see a complete list of color groups, use the command:

:so $VIMRUNTIME/syntax/hitest.vim.

The second argument is the actual pattern you want to match. This pattern is a regular expression and can vary from being very simple to extremely complex, depending on what you want to match. A simple example of the match command in use would be:

:match ErrorMsg /^Error/

Personalizing Vim

This command looks for the word Error (marked with a ^) at the beginning of all lines. If a match is found, it will be marked with the colors in the ErrorMsg color group (typically white text on red background).

If you don’t like any of the available color groups, you can always define your own. The command to do this is as follows:

:highlight MyGroup ctermbg=red guibg=red gctermfg=yellow
guifg=yellow term=bold

This command creates a color group called MyGroup with a red background and yellow text, in both the console (Vim) and the GUI (Gvim). You can change the following options according to your preferences:

ctermb

Background color in console

guibg

Background color in Gvim

ctermf

Text color in console

guifg

Text color in Gvim

gui

Font formatting in Gvim

term

Font formatting in console (for example, bold)

If you use the name of an existing color group, you will alter that group for the rest of the session.

When using the match command, the given pattern will be matched until you perform a new match or execute the following command:

:match NONE

The match command can only match one pattern at a time, so Vim has provided you with two extra commands to match up to three patterns at a time. The commands are easy to remember because their names resemble those of the match command:

:2match
:3match

You might wonder what all this matching is good for, as it can often seem quite useless. Here are a few examples to show the strength of matching.

Example 1: Mark color characters after a certain column

In mails, it is a common rule that you do not write lines more than 74 characters (a rule that also applies to some older programming languages such as, Fortran-77). In a case like this, it would be nice if Vim could warn you when you reached this specific number of characters.

This can simply be done with the following command:

:match ErrorMsg /%>73v.+/

Here, every character after the 73rd character will be marked as an error. This match is a regular expression that when broken down consists of:

%>

Match after column with the number right after this

73

The column number

V

Tells that it should work on virtual columns only

.+

Match one or more of any character

Personalizing Vim

Example 2: Mark tabs not used for indentation in code

When coding, it is generally a good rule of thumb to use tabs only to indent code, and not anywhere else. However, for some it can be hard to obey this rule. Now, with the help of a simple match command, this can easily be prevented.

The following command will mark any tabs that are not at the beginning of the line (indentation) as an error:

:match errorMsg /[^t]zst+/

Now, you can check if you have forgotten the rule and used the Tab key inside the code. Broken down, the match consists of the following parts:

[^

Begin a group of characters that should not be matched

t

The tab character

]

End of the character group

zs

A zero-width match that places the ‘matching’ at the beginning of the line ignoring any whitespaces

t+

One or more tabs in a row

Personalizing Vim

This command says: Don’t match all the tab characters; match only the ones that are not used at the beginning of the line (ignoring any whitespaces around it).

If instead of using tabs you want to use the space character for indentation, you can change the command to:

:match errorMsg /[t]/

This command just says: Match all the tab characters.

Example 3: Preventing errors caused by IP addresses

If you write a lot of IP addresses in your text, sometimes you tend to enter a wrong value in one (such as 123.123.123.256). To prevent this kind of an error, you can add the following match to your vimrc file:

match errorMsg /(2[5][6-9]|2[6-9][0-9]|[3-9][0-9][0-9])[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}|
[0-9]{1,3}[.](2[5][6-9]|2[6-9][0-9]|
[3-9][0-9][0-9])[.][0-9]{1,3}[.][0-9]{1,3}|[0-9]{1,3}[.][0-9]{1,3}[.](2[5] [6-9]|2[6-9][0-9]|[3-9][0-9][0-9])[.][0-9]{1,3}
|[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.](2[5][6-9]|2[6-9][0-9]|[3-9][0-9][0-9])/

Even though this seems a bit too complex for solving a small possible error, you have to remember that even if it helps you just once, it is worth adding.

If you want to match valid IP addresses, you can use this, which is a much simpler command:

match todo /((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).)
{3}(25[0-5]|2[0-4][0-9]|[01]?
[0-9][0-9]?)/


Subscribe to the weekly Packt Hub newsletter. We'll send you this year's Skill Up Developer Skills Report.

* indicates required

LEAVE A REPLY

Please enter your comment!
Please enter your name here