Personalizing Vim

(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 files\vim\vimrc

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 settings\kim\_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 settings\kim\_gvimrc and c:\program files\vim\gvimrc

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:


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:


Background color in console


Background color in Gvim


Text color in console


Text color in Gvim


Font formatting in Gvim


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:


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


The column number


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]\zs\t\+/

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


The tab character


En<!--[if supportFields]> XE "Vim:personal highlighting" <![endif]--><!--[if supportFields]><![endif]-->d of the character group


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


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 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]\)[.]
\\ \[3-9][0-9][0-9]\)[.][0-9]\{1,3\}[.][0-9]
\\ \[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]
\\ \{3\}\(25[0-5]\|2[0-4][0-9]\|[01]\?

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

A more informative status line

At the bottom of the Vim editor, you will find two things:

  • The command line buffer (where you can input commands)
  • The status line

In the default configuration, Vim has a simple and quite non-informative status line. To the right it shows the number of the current row and column, and to the left it shows the name of the file currently open (if any).

Whenever you execute a Vim command, the status line will disappear and the command buffer will be shown in that line instead. If the command you execute writes any messages, then those will be shown on the right of the status line.

For simple and fast file editing, this status line is adequate. But if you use Vim everyday and for a lot of different file formats, it would be nice to have a more informative status line.

In this recipe, we see some examples of how the status line can be made a lot more informative with simple methods.

The command that sets how the status line should look is simply called:

:set statusline format

Here, format is a string such as printf (known from C programming) that describes how the status line should look.

If you look in the Vim help system by typing :help 'statusline', you will see that the status line can contain a wide variety of pieces of information. Some of these are more useful in your daily work than others.

My status line always contains information about:

  • Name of the file that I am editing
  • Format of the file that I am editing (DOS, Unix)
  • File type as recognized by Vim for the current file
  • ASCII and hex value of the character under the cursor
  • Position in the document as row and column number
  • Length of the file (line count)

The following command will turn your status line into a true information bar with all the previously mentioned information:

:set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\
[ASCII=\%03.3b]\[HEX=\%02.2B]\ [POS=%04l,%04v]\ [%p%%]\ [LEN=%L]

I have added a [ ] around each of the pieces of information so that it is easier to distinguish them from each other. This is purely to give a visual effect and can be left out if necessary.

Personalizing Vim

However, we now see that the status line still shows the old non-informative status line, as in the default installation. This problem occurs because Vim, by default, does not show the status line at all. Instead, it just shows the command buffer with a little bit of information in it. To tell Vim that you would like to have a real status line shown, you will have to add the following setting to your vimrc file. This command will make sure that your status line is always shown as the second-last line in the editor window:

:set laststatus=2

You will now see that the command buffer gets a place of its own in the last line of the editor window. This way there's always room for the status line and you will always have information about the file right in front of you. The status line does, of course, take up some of the editing area, but it is then up to you to decide whether it should be shown or not. You can always remove it for the rest of the editing session by executing the following command from within Vim:

:set laststatus=0

Toggle menu and toolbar

If you are used to working with Vim in the console mode, you are also quite used to having no menus and toolbars at the top of the window. However, when you move to Gvim, you will soon realize that both the menu and the toolbar are present by default in the GUI.

Many believe that extra room for text is far more important than the menu and the toolbar. If you are one of those persons, you might like to remove the menu and toolbar while working in Gvim. However, some scripts add useful functionality in the menu and it is, therefore, important to have menus. The solution for this could be toggling if the menu and toolbar are shown or not.

The following code maps the key combination Ctrl+F2 to toggle the menu and toolbar in Gvim. You can add it to your vimrc file if you want this functionality.

map <silent> <C-F2> :if &guioptions =~# 'T' <Bar>
\set guioptions-=T <Bar>
\set guioptions-=m <bar>
\else <Bar>
\set guioptions+=T <Bar>
\set guioptions+=m <Bar>

Now, whenever you don't need the menu and toolbar, you can just press Ctrl+F2 and you will get the full space for your text.

If you want either the menu or the toolbar to be hidden all the time, add one of the following lines to your vimrc file.

To remove the menu completely, add:

:set guioptions-=m

To remove the toolbar completely, add:

:set guioptions-=T

Other parts of the GUI can be modified with the set guioptions command. To find out what you can modify, look in

:help 'guioptions'

Adding your own menu and toolbar buttons

If you are in Gvim, you can make a handy menu with all the functionality you use the most. You might not always need to use it from the menu, but whenever you forget how to use it, you can always just find it there. If you need to get to the functionality really fast, you can even add it directly in the toolbar of Gvim.

In this recipe, we look at both how to make your own menu and, later, how to add extra buttons to the toolbar in Gvim. Let's start with the menu construction.

Adding a menu

Building a menu is basically just executing a command for each item you want in the menu. As long as you follow the right naming convention, you will see a nice little menu with all your items in it.

Let's start with a simple example. Say you want to add a menu like the buffers menu, but for tabs.

The command you will need to use is:

:menu menupath command

This command works much like the map command , except that instead of mapping a command to a key combination, here the mapping is done to a menu item.

The command has two arguments. The first is the actual path in the menu where the item should be placed, and the second argument is the command that the menu item should execute. If, for instance, you want to add a menu item called Next to the menu item Tabs, then you would need to use a command like this:

:menu Tabs.Next <ESC>:tabnext<cr>

So, now you have a menu called Tabs with one menu item called Next. Now, the Next menu item executes the following command:


This command is prepended with &ltESC> to get into the normal mode, and then <cr> to actually execute the command. If you haven't added , this command won't work. Another way to get around this is by adding specific menu items according to the current mode. For this, Vim has a range of alternatives to the :menu command:

  • :nmenu for the Normal mode
  • :imenu for the Insert mode, ^O is prepended
  • :vmenu for the Visual mode, ^C is prepended and ^\^G is appended
  • :cmenu for the Command-line mode, ^C is prepended and ^\^G is appended
  • :omenu for the OP-pending mode, ^C is prepended and ^\^G is appended

The prepended parts (^O and ^C) are to get into normal mode.

The ^O (Ctrl + O) is especially for insert mode because it gets you back into insert mode after executing the command.

^\^G (Ctrl + \, Ctrl + G) is to handle the special case wherein the global insert mode setting is set to true and Vim has the insert mode as the default mode (Vim is modeless). In this case, it will get you back into the insert mode and in the rest of the case, it will get you back in the mode you just came from.

Instead of setting the same menu item for each and every mode, you can just replace the commands with this single command:

:amenu menu-path command

According to the current mode, this command prepends and appends things to the right.

So, let's go to our new Tabs menu, and add some more items and functionality to it. With the following, it should look similar to the Buffers menu:

:amenu Tabs.&Delete :confirm tabclose<cr>
:amenu Tabs.&Alternate :confirm tabn #<cr>
:amenu <silent> Tabs.&Next :tabnext<cr>
:amenu <silent>Tabs.&Previous :tabprevious<cr>

The observant reader might have noticed that some new things have been added in the commands.

The first thing is the <silent> tag in the last two commands. By adding this, we can avoid the command being echoed in the command-line buffer during execution. Although, this is purely a cosmetic functionality, the & in the menu path is a more functional extension. By adding & in front of one of the letters in the last part of the menu path, you can define a keyboard shortcut for an item. This makes it easy to navigate to that particular item in the menu and execute it.

Let's say that you want to go to the next tab by executing the Tabs | Next menu item. Now, you can do so by simply pressing Alt + T + N. This is Alt + T for opening Tabs, and N (N because the & is in front of the N in Next) to call the Next item. If another menu item uses the same character for a shortcut, you can cycle through it by pressing the Alt key repeatedly.

If you would like to have a line that separates some of the items in your drop-down menu, you can use the name SEP for the item and ":" for the command:

:amenu Tabs.-SEP-:

The menu that we have created will only exist as long as Vim is open in this session. So, in order to get it into your menu all the time, you need to add it to your vimrc file (without the : in front of the commands).

So, now we have a simple Tabs menu that looks a bit like the Buffers menu. It does not, however, have the functionality that lists active buffers in the Buffers menu. This does not make much of a difference when you realize that buffers can be hidden for the user, but tabs cannot. You can, in other words, always see exactly how many tabs you have and what they are called by just looking at the tab bar.

A Personal menu can be used for a lot of other interesting things. If you work with many types of files, you can even start having menus for specific file types or submenus for the different types in the same menu.

A submenu is constructed by following the naming convention in the menu path. So, if you want to navigate through Tabs | Navigation | Next, you will simply have to add the Next menu item with the Tabs.Navigation.&Next menu path.

Adding toolbar icons

So, now that we know how to make our menus, adding our own icons to the toolbar isn't that difficult. Actually, Vim is constructed in such a way that the toolbar is just another menu with a special name. Hence, adding an icon to the toolbar is just like adding an item to a menu.

In the case of a toolbar menu, you will be able to add items to it by using a menu path that starts with the name ToolBar. To add an item to the toolbar that gives access for executing the :buffers command (show list of open buffers), all you have to do is to execute the following command:

:amenu icon=/path/to/icon/myicon.png ToolBar.Bufferlist :buffers<cr>

Of course, you will need to have an icon placed somewhere that can be shown in the toolbar.

The path to the icon is given with the icon argument to the amenu command . If you do not give a path to the file, but only the filename, then Vim will look for the icon in a folder called bitmaps/ in the Vim runtime path (execute :echo $VIMRUNTIME to see where it is). The type of icons supported is dependent on the system you use it on.

And that's really it! After executing the command, you will see your icon in the toolbar as the last one on the right. If you press it, it will execute the :buffers command and show you a buffer list.

As with the menus, you can add toolbar buttons that are only shown in specific modes using the mode-specific menu commands imenu, vmenu, cmenu, and so on.

If you want your menu or toolbar icon placed elsewhere than to the right of the others, then you can use priorities. Read more about how you can do this in :help menu-priority and :help sub-menu-priority.

Modifying tabs

Ever since the release of Vim version 7.0, there has been support for tabs or tab pages as it is called. Tab pages are not like the normal tabs in other applications; rather they are a way to group your open files. Each tab can contain several open buffers and even several windows at the same time.

What makes tabs special is the commands that you would normally execute on all open buffers / windows (such as :bufdo, :windo, :all, and :ball) are limited to only the windows and buffers in the current tab page.

Normally, tab pages are shown as a list of tabs at the top of the window (just above the editing area). Each tab has a label, which by default shows the name of the file in the currently active buffer. If more windows are open at the same time in the tab page, then the tab label will also show a number telling how many windows.

Personalizing Vim

Sometimes you might like to have the label on the tabs telling you something different. For instance, if you often have one tab for each project, then it would be nice to name the tab according to the name of the project in it.

Personalizing Vim

The label on the tabs is set in a way very similar to the one used for the status line (see the A more informative status line section). But here, instead of setting the status line property, you set the tabline property:

:set tabline=tabline-layout

Personalizing Vim

However, if you are in Gvim use the following line:

:set guitablabel

Even though setting the tabline resembles the way you set the status line, it is a bit more troublesome. This is mainly because you need to take care of whether the tab is an active one or not. So, let's start with a little example for Vim.

When we have a lot of tabs, they tend to take up too much space in the tab page, especially if they contain the entire name of the file in the currently active buffer. We want to have only the first six letters of the name of the active buffer in the tab label. The active tab should also be easy to distinguish from the other tabs. So, let's make its colors white on red like error messages.

The following script in Vim script does just that (learn more about how to create Vim scripts later).

function ShortTabLine()
let ret = ''
for i in range(tabpagenr('$'))
" select the color group for highlighting active tab
if i + 1 == tabpagenr()
let ret .= '%#errorMsg#'
let ret .= '%#TabLine#'
" find the buffername for the tablabel
let buflist = tabpagebuflist(i+1)
let winnr = tabpagewinnr(i+1)
let buffername = bufname(buflist[winnr - 1])
let filename = fnamemodify(buffername,':t')
" check if there is no name
if filename == ''
let filename = 'noname'
" only show the first 6 letters of the name and
" .. if the filename is more than 8 letters long
if strlen(filename) >=8
let ret .= '['. filename[0:5].'..]'
let ret .= '['.filename.']'
" after the last tab fill with TabLineFill and reset tab page #
let ret .= '%#TabLineFill#%T'
return ret

Now, we have the function and just need to add it to our vimrc file, along with a line that sets the tabline to the output of our function. This can be done with the following command:

:set tabline=%!ShortTabLine()

The result is a more compact tablist as shown in the following screenshot:

Personalizing Vim

Changing the tabline in Gvim is a bit different, but still follows almost the same basic ideas. However, when in the GUI, you do not have to consider things such as the color of the active tab, or whether it is actually active or not because this is all a part of the GUI design itself.

So, let's simplify the ShortTabLine() function a bit so that it only sets the tab label:

function ShortTabLabel()
let bufnrlist = tabpagebuflist(v:lnum)
" show only the first 6 letters of the name + ..
let label = bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
let filename = fnamemodify(label,':h')
" only add .. if string is more than 8 letters
if strlen(filename) >=8
let ret=filename[0:5].'..'
let ret = filename
return ret

So, now we just have to set the guitablabel property to the output of our function:

:set guitablabel=%{ShortTabLabel()}

The result will be fine, small tabs as shown in the following figure.

Personalizing Vim

If you want to remove the tabs bar completely from Gvim, then you can use the::set showtabline=0 command (set to 1 to show it again).

Now we have limited the information in the tabs, but we would still like to have the information somewhere. For that we have a nice little tip—use the tool tips.

The nice thing about tool tips is that when you don't activate them (hold your cursor over some area, for example, a tab), you don't see them. This way you can have the information without it filling up the entire editor.

To set the tool tip for a tab, you will need to use the following command:

:set guitabtooltip

This property should be set to the value you want to show when the mouse cursor hovers over the tab.

To test it, you can try it with a simple execution like this:

:set guitabtooltip='my tooltip'

Now, this only shows a static text in the tool tip. We need some more information there. We removed the path from the filenames on the tabs, but sometimes it is actually nice to have this information available. With the tool tips, this is easily shown with the following command:

:set guitabtooltip=%!bufname($)

As with the tabs, the contents of the tool tip can be constructed by a function. Here, we have constructed a small function that shows all the information you would normally have in the tabs, but in a more organized way:

function! InfoGuiTooltip()
"get window count
Chapter 2
[ 37 ]
let wincount = tabpagewinnr(tabpagenr(),'$')
let bufferlist=''
"get name of active buffers in windows
for i in tabpagebuflist()
let bufferlist .= '['.fnamemodify(bufname(i),':t').'] '
return bufname($).' windows: '.wincount.' ' .bufferlist ' '

Use the code described previously like this:

:set guitabtooltip=%!InfoGuiTooltip()

In the following screenshot, you can see how the resulting tool tip will look in Gvim:

Personalizing Vim

You can probably imagine many other interesting ways to use the small information space that the tabs and tool tips provide. Following the previous example, you should have no problems in implementing them.

There's more

In this article, we have covered

  • Changing the fonts
  • Changing the color scheme
  • Personal highlighting
  • A more informative status line
  • Toggle menu and toolbar
  • Adding your own menu and toolbar buttons

If you have read this article you may be interested to view :

You've been reading an excerpt of:

Hacking Vim 7.2

Explore Title