5

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.

3

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.

  • I looked at the scripts in given by :scriptnames, several have some labeling of "syntax" either in their filename or path. How can I tell which one to add your lines to? Also what would I change to make the text red instead of the background? – David G. Feb 25 at 15:24
  • @DavidG. I edited my answer to show how you can change color of text and background. For text only you can do :hi xShebang ctermfg=red, in this case background is left unchanged. – Yurko Feb 25 at 18:37
  • So what I ended up doing was writing a function called Colors() in .vimrc that performs your first two commands. Then I wrote the alias vi="vim -c 'call Colors()'" – David G. Feb 25 at 18:50
  • @DavidG. We were thinking in the same direction. Before reading your last comment, in the meantime I updated the answer suggesting a very similar solution :) – Yurko Feb 25 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.