Create color scheme for Vim

Create color scheme for VimGilli carmonBlockedUnblockFollowFollowingMay 21Photo by Clément H on UnsplashWhen I first met Vim, I thought someone was looking to make his life more complicated.

But after a while I figured that Vim is the perfect text editor for people who want the text editor to be perfect for them.

Vim is very customizable in general, and in particular, it has a lot of themes you can choose from.

But I have decided to create my own vim theme, and why not report about it?Why create your own vim theme?First of all, because you can, and it is also quite simple.

Secondly, I don’t know about you, but I love change.

I think almost every week I move something in my room to a different place to make it feel new.

So if it works on my room, why won’t it work on my text editor?How do you set your vim theme?Well first, if you want to use any theme to your text editor, you need to save the name.

vim (the .

vim file of the color scheme) in your colors directory:~/.

vim/colorsIf it doesn’t exists, you can create one.

Then go to .

vimrc file and add:colorscheme colorscheme_nameSome terms you should know, before you startIn vim you have 7 terms for highlighting: guifg, guibg: The colors of your graphical vim (gui = graphical user interface).

guifg is for your text color, and guibg is for the background.

gui: The text style.

Can be bold, italic or underline (as far as I know).

 ctermfg, ctermbg, cterm: same as guifg, guibg and gui, but for the terminal.

There is also term, but we won’t talk about it now.

Writing your color schemeFirst, we have a boilerplate code that we need to write in our colorscheme.

vim file:What this code does is clearing any highlights defined before.

 ‘highlight clear’ resets the highlighting the user added, and ‘syntax reset’ gets the colors back to default ones.

The default colors are determined by the background variable, that we should set too:set background=darkThis sets the color palette, it can be dark or light.

 And of course, naming our color scheme:set g:colors_name=”best-theme-ever”It helps identifying our color scheme.

 So our final boilerplate code is:That is it with the boilerplate, now the real deal — the command to set new highlight is:highlight group_name key=value When key is one of the terms we met above (guifg, guibg, etc.

) Group name by syntax.

txt:syntax group name is to be used for syntax items that match the same kind of thing.

These are then linked to a highlight group that specifies the color.

You can find the groups names conventions if you type in your vim:help syntax.

txtand scroll down to NAMING CONVENTIONS.

groups namesSo for example, if you want to change the background of the gui vim to white, and the comment text color to red and boldAnd let the magic happen.

A big & important note for those who want to highlight the terminalwhen you choose color for your gui, you use rgb, but in the terminal you use Indexed color which can include 8|16|88|256 colors, depends on your terminal.

Mine is 256 so I found this page that contains the 256 colors and their numbers.

So if you want your text terminal color to be red you writehighlight Normal ctermfg=9As programmers, we should always search for shortcuts, so instead of writing command for each group, we should write a function!My function assumes that you always pass it group name, guibg, guifg, gui, ctermfg, ctermbg (no need for cterm since it will be the same as gui).

You can also checkout this article, which shows a different kind of function for highlighting.

What this function does is simply building the command we met before, and in the end, execute it.

So if, for example, we want our comment text to be red, we will call our function like this:call Coloring(“Comment”,"NONE",#ff0000,”NONE”,"NONE","9")Cool isn’t it?Linking highlightsIf you want some groups to have the same highlight you simply writehighlight link x yWhen x copies the highlight of y.

Get the syntax groupAn easy function I found here helps you get the syntax group of the word your cursor is on.

Just save the function on your .

vimrc file, and press ctrl +shift+p on the word you want to check its syntax word.

That’s it, thank you for reading, and I hope you found it useful ????.

. More details

Leave a Reply