Vim Colors – Differentiate Shebang from Comment Lines

colorsvimvimrc

Currently I have my .vimrc set to produce blue comment lines, I was wondering if it was possible within the vim framework to make the color of line that start with #! a different color than those that start with #. I can't seem to find anywhere where this question has been asked.

Best Answer

Commands to execute in vim:

:hi xShebang ctermfg=red ctermbg=blue
:syntax match xShebang /#!.*/

It makes the script Shebang line red text on blue background.

Depending on your vim configuration and order of files to load, it may work to add lines (without semicolumn symbols) to .vimrc or may be rewritten by next loaded files.

To check order of loaded scripts, execute:

:scriptnames

In my case ~/.vimrc somewhere in the middle of loaded scripts and syntax command is rewritten by other syntax script files.


UPDATED.

It takes too long to go into structure of uploaded by vim scripts. So I have a quick fix for this particular request: you can run extra vim commands from command line as parameters, when you start vim (it changes Shebang line colors to be red on black):

vim -c ':syntax match xShebang /#!.*/' -c ':hi xShebang ctermfg=red ctermbg=black' filename

But it is not nice to type it each time. To not do it, we can create an alias.

You can add this to your .bash_profile in user home directory, to have it automatically loaded each time when you log in:

alias vim="vim -c ':syntax match xShebang "'/#!.*/'"' -c ':hi xShebang ctermfg=red ctermbg=black'"

Now when you execute vim filename, it will run vim with all those parameters from alias.