2

Highlighting in vim: distinguish the lines with comment delimiter followed by special string

In some cases, for example with the scripts for HPC clusters, when using SGE, TORQUE or Slurm we can have a shell script (like bash) with comment lines denoted by # but grid commands in lines with #$, #PBS, or #SBATCH.

What is an easy way to keep the same highlighting used for bash but excluding #SomeString from being considered a comment?

6
  • This question will probably get a more appropriate audience on Stack Overflow Commented Jun 28, 2018 at 15:08
  • 3
    If you're going to fob people off with other StackExchange WWW sites, at least fob people off with the vi one: vi.stackexchange.com/questions/4975 (-: Commented Jun 28, 2018 at 15:22
  • @glennjackman I am not sure if this is more for SO. It's not really about developing but about settings in vim. Commented Jun 28, 2018 at 17:35
  • Thanks @JdeBP, I didn't know that existed. Commented Jun 28, 2018 at 17:39
  • 1
    Also, there's a specific tag for syntax files: stackoverflow.com/tags/vim-syntax-highlighting/info Commented Jun 28, 2018 at 17:48

1 Answer 1

2

Start by examining the definitions for comments in the shell syntax script:

:syn list shComment --- Syntax items --- shComment xxx match /^\s*\zs#.*$/ contains=@shCommentGroup match /\s\zs#.*$/ contains=@shCommentGroup match /#.*$/ contained contains=@shCommentGroup links to Comment 

Extend syntax cluster

There, you see what patterns for comments need to be overridden. But wait! That syntax script offers an extension mechanism via the :help :syn-cluster; the contains=@shCommentGroup

syn match shHPC "PBS" contained 

That would highlight PBS anywhere inside a comment. If additional stuff can follow, append .*$ to the pattern. If the PBS keyword must directly follow the #, add a positive lookbehind assertion:

syn match shHPC "#\@<=PBS" contained 

You can all the other keywords via additional :syn match commands, or by using regular expression branches: "#\@<=\(PBS\|SBATCH\|...\)".

Finally, that new syntax group has to be included in the syntax cluster for comments, and given a highlighting so you see it:

syn cluster shCommentGroup add=shHPC hi link shHPC PreProc 

Define overriding syntax match

Alternatively, you may want to skip the cluster and overwrite the comment syntax. For example, when the special keywords can only appear at the beginning of the line, but now after Bash commands. Or if you can't stand the fact that the cluster method will keep the # in the original highlighting.

syn match shHPC "^\s*\zs#PBS" hi link shHPC PreProc 

Activation

As these commands extend the built-in syntax, they need to be executed after them. The place for that is ~/.vim/after/syntax/sh.vim. If you want to only conditionally activate them for certain projects, you can write a filetype detection that sets a marker variable, e.g. b:is_HPC, and then make the above code check that flag.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.