Codementor Events

VimLeaning: How to Create a Custom Vim Color Scheme

Published Dec 02, 2016Last updated Feb 02, 2018
VimLeaning: How to Create a Custom Vim Color Scheme

Vim is one of the popular text editor choices for power users. In fact, you can check out how good it this comparison of popular text editors. One of the most important aspects to consider when using a text editor is the look and feel, and Vim is quite rich in terms of themes, either pre-bundled with Vim distribution or added by users in the community. If you want to use an existing color scheme with Vim, the simplest way is to:

Create `~/.vim/colors` folder if not exists already.
Add the `.vim` colorscheme file to the colors folder.
Go to vim/mvim and use `colorscheme scheme_name` to use the corresponding colorscheme.
To apply the corresponding colorscheme everytime vim/mvim runs, add the `colorscheme scheme_name` to your ~/.vimrc or ~/.vimrc.after or any other config file which loads every time you runs vim/mvim.

A good color scheme is a key to work long hours on any editor. Mostly, people use one of the existing themes. But when they get more familiar with the structure and insights of Vim, they either customize existing theme or create their own. Here, I am going to discuss about how we can create a new Vim theme.

How to create a new theme in Vim

First of all, we need to decide what we are actually targeting in terms of Vim color scheme: are we looking for dark/light color palate, are we planning to support GUI and terminal mode of vim, etc. For this particular tutorial, I will be concentrating on GUI based dark color-scheme.

First, we need to clear any predefined colors or background.

highlight clear
if exists("syntax_on")
  syntax reset
endif

hi[ghlight] clear will reset all the highlighting for groups added by user. Use the current value of the 'background' to decide which default color group to use. syntax reset gets the colors back to default ones, but doesn't change color for highlight options.

Now, set the global variables, background, and the name of your theme so that it's easier to include and manipulate:

set background=dark
set g:colors_name="my-theme"

set background will set the theme palate for us. Setting a global colors_name helps in identifying the themes.

function!  GuiFor(group, ...)
  let histring = 'hi ' . a:group . ' '

  if strlen(a:1)
    let histring .= 'guibg=' . a:1 . ' '
  endif

  if strlen(a:2)
    let histring .= 'guifg=' . a:2 . ' '
  endif

  if a:0 >= 3 && strlen(a:3)
    let histring .= 'gui=' . a:3 . ' '
  endif

  execute histring
endfunction

This method will be the cornerstone on how we are going to add highlight rules for different highlight groups. This method is expecting the first argument to be the highlight group. Also, the second and third arguments are guibg and guifg, respectively, and are mandatory to be passed.

The last argument, gui, is optional. This method concatenates the arguments with their respective values and evaluates the resultant string with the execute command.

A sample method call can be something like this:

GuiFor("Normal","#000000", "#afafaf")
GuiFor("Constant", "NONE", "#gray80")

Wrapping up

With this, you will be able to create your own Vim colorscheme. Apart from the happy flow mentioned above, we can also have some sort of compatibility matrices to be fulfilled like, based on the version of vim, whether 256 color palate or not, etc.

In case of any further query, put it either in comments or write to me at konflikt.

Discover and read more posts from sandeep kumar
get started
post commentsBe the first to share your opinion
Show more replies