The vimrc file contains some commands which set/customise the colours for Vim syntax for Python and R. This post gives a brief description of how to go about doing this yourself.

Colour codes

To specify your colours, you will need to know the colour code, e.g. #abc123 and the colour number, between 0 and 255.

Colour codes can be found using this very useful website. More colours can be found here, but I prefer to choose the colour myself using the first link. However, since tmux uses 256 colours, your options are actually limited to the 256 colours.

Update: an even better colour chart can be found on this page.

In order to see the colour codes (numbers 0 to 255, note that 0 is black), save the following script

for i in {0..255} ; do
    printf "\x1b[38;5;${i}mcolours${i}\n"

from this post as, then set it as executable and run it using:

chmod a+x 

will display the colours in the terminal. Then, the RGB colour code can be obtained by looking at the list on this Vim page.

Once you have chosen a colour you like, you can assign it a name, such as:

hi ArrowGreen 		    guifg=#95e454 ctermfg=113 gui=none

Vim: main syntax groups

First go to the Vim syntax directory, e.g.

cd /usr/share/vim/vim80/syntax

and then open the corresponding Vim file, i.e. r.vim.

Scroll down to the end of the r.vim file, and note the commands:

HiLink rComment     Comment
HiLink rConstant    Constant
HiLink rString      String
HiLink rNumber      Number
HiLink rBoolean     Boolean

Identifying a syntax group

Let us look at the last line, which contains rBoolean. This is linking the words/regions specified in r.vim under the name rBoolean to the syntax group Boolean. If we look for the line containing rBoolean, we will find:

syn keyword rBoolean  FALSE TRUE

This is saying that, in R, the words FALSE and TRUE belong to the rBoolean group, and then the line

HiLink rBoolean     Boolean

links this group to the more general Boolean group. I say it is more general, because other languages link other words to this group. For example, in cpp.vim, we have the lines

syn keyword cppBoolean		true false
  HiLink cppBoolean		Boolean

which links true and false in C++ (.cpp) files to this group.

Selecting a colour for the group

Then, in your colourtheme.vim file, you need to specify the colour of this group, e.g.

hi Boolean         guifg=#6e9cbe ctermfg=73 gui=none

Note: In fact, Boolean is an oddity, and if you start your own colourtheme.vim file by modifying an existing one, you will find that the Constant line:

hi Constant         guifg=#6e9cbe ctermfg=73 gui=none

will actually take care of the Boolean group.

Identifying a syntax group II

What happens if the command is not listed? For example, what about parentheses (...), or the command is.null?

Fortunately, the Vim experts have created some very helpful tutorials for finding out the name of any group under the cursor. Please this external post, or this other external post.

The following line:

map <F10> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<'
\ . synIDattr(synID(line("."),col("."),0),"name") . "> lo<"
\ . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"

is already in the vimrc. Simply pusing the F10 key will tell you the syntax group.

For example, when a ( is highlighted, and F10 is pushed, the following is displayed:

hi<Delimiter> trans<Delimiter> lo<Special>

As another example, when a command such as rep or length is highighted, and F10 is pushed, then the following us displayed:

hi<rFunction> trans<rFunction> lo<Function>

Example 1: delimiters

Then, to change those colours of the parentheses, look at the colours for the Special group. In wombat17.vim, this is set to:

hi Special          guifg=#e7f6da ctermfg=229 gui=none

Example 2: function names

Similarly, for the Function group:

hi Function         guifg=#ffafff ctermfg=219 gui=none

Example 3: the matching delimiters

Another interesting group, that provides an example showing what happens when the background colour is set, is MatchParen

hi MatchParen     guifg=#f6f3e8 ctermfg=83 guibg=#857b6f ctermbg=243 gui=bold

When the open parenthesis ( is highlighted, then the closing parenthesis is highlighted in dark grey, with the parenthesis in green (a scheme that seems nice).

Other existing syntax groups

Suppose we want to change the colour of arrows in R to green.

In the Vim syntax file, i.e. /usr/share/vim/vim73/syntax/r.vim, look for the group corresponding to arrows; searching for "arrow" or "Arrow", we find the line:

syn match rArrow /<\{1,2}-/

Now, in your colourtheme.vim file create a line:

hi link rArrow ArrowGreen

This will link the rArrow group to the ArrowGreen colour.

Creating your own syntax groups

Now let's suppose we want to make the $ symbol blue. Well, there is no group for that in the r.vim syntax file.

In this case, we need to make our own syntax group:

autocmd FileType r syn match rDollar /[$]/

and we place this line in our ~/.vimrc file. Then we can place the following colour lines:

hi DollarBlue           guifg=#92a1ea ctermfg=110 gui=none
hi link rDollar DollarBlue

in our colourtheme.vim file, and we are done.

In fact, in your ~/.vimrc file, you can create a group containing a collection fo syntax groups. Here are some other groups I use with R:

"These commands assign symbols/words to syntax groups in R
augroup rcmds 
    "matching a single character, e.g. $, =
    autocmd FileType r syn match rDollar /[$]/
    autocmd FileType r syn match rEquals /[=]/
    "matching a collection of symbols
    autocmd FileType r syn match rMathOperators /[+^%/*]/
    "matching a whole word
    autocmd FileType r syn match rRequire /require/
    "autocmd FileType r syn keyword rComparisons <= >= == < > joe
    autocmd FileType r syn match rComparisons /[ < ][ > ]/
    " this line is for <= and >=    
    autocmd FileType r syn match rComparisonsMore /[<>\{1,2}=][=\{1,2}=]/
augroup END

Similar groups can be created for other languages, such as Python. Check my vimrc file, and wombat16.vim colourtheme file.

Something to check for tmux users

In order for the active/inactive pane colouring to work, as detailed in this post, in your Vim colourtheme file, check the line starting hi Normal looks something like:

hi Normal           guifg=#e6e1de ctermfg=none gui=none


I realise that this may be a lot to get to grips with, juggling the language.vim, ~/.vimrc and colourtheme.vim files. I have tried to break down the various cases, and hopefully this will be of use to someone.