How to add words to vim coloring in C/C++ mode? - vim

I notice that in C/C++ mode, comments such as TODO XXX and FIXME get special color marking.
How can I add the word HACK to this list of words to be marked in the same way?
I tried adding the following to my ~/.vimrc, but it didn't work:
syn keyword cTodo contained TODO FIXME XXX HACK

I would advise against directly modifying the original syntax file; you then have to maintain your version whenever the original changes (e.g. after a Vim upgrade). For these small syntax enhancements, the place is in the ~/.vim/after/syntax/c.vim file, which is sourced after the original syntax. The line would be
syn keyword cTodo contained HACK

You need to modify the syntax file. Typically, it is in /usr/share/vim/vim72/syntax, and the file you want is c.vim and cpp.vim. You will see a line syn keyword cTodo contained followed by a list of words that are considered under the Todo label for coloring. You can add your word there, or make your own keyword, but adding your own keyword would mean adding your keyword to the coloring file as well.
For user only changes, make a directory ~/.vim/syntax. Copy the c.vim and cpp.vim files there, and edit as necessary.
Second edit: Decided to look further, and it appears you can just add to a current syntax file, but I haven't tried it. Add your one line you added to your .vimrc to a file in ~/.vim/after/syntax

Related

Can we escape types, codes, etc. in comments section so that spell check does not consider them as typo

Vim supports spell-check only in comments section already, however, if I have a type name or something not a regular word, it will consider it as a typo. For instance, in the following example, std::endl will be highlighted as typo.
// Don't use std::endl, it will flush unnecessarily
I wish we could use `` to escape them like following.
// Don't use `std::endl`, it will flush unnecessarily
Is there any tips or solution for this besides adding everything into dictionary?
I really don't want to disable spell-check due to this, so any help is greatly appreciated.
Thank you!
You can use this syntax rule to create a new group matching a `...` block and disable spelling inside those blocks:
syntax region cCommentNoSpell start=+`+ end=+`+
\ contained containedin=cComment,cCommentL transparent
\ contains=#NoSpell
To load this for cpp and c files, add this line (by itself) to a file ~/.vim/after/syntax/c.vim, so it is loaded after the system syntax files for C++ and C. (The cpp syntax rule includes all syntax for c so you'll get it on cpp too.)
The syntax rule uses ` as both start and ending delimiter.
It uses contained and containedin to only match inside comments. The cComment rule matches traditional multi-line /* ... */ comments and cCommentL matches single-line // ... comments. (Both are defined in the syntax file for C and C++ shipped with Vim.)
The transparent attribute instructs it not to use this syntax rule as a highlighting group, so it keeps the normal highlighting for comments in the parts matched by this rule.
Finally, contains=#NoSpell is what disables spelling on the regions that match this rule. See :help spell-syntax for more details on how spelling works together with syntax highlighting.

problem in jumping to system verilog macros `define in VIM using ctags

I checked following things, tags file path is set properly.
the macro tag i am looking for exist in the tags file. task/function/parameters etc works without hiccups. i have problem only with regex.
the expression present in ctags file are
1. --regex-SystemVerilog=/^\s*`define\b\s*(\w+)/`\1/d,define/
2. --regex-systemverilog=/^[ \t]*`define[ \t]*([a-zA-Z_0-9]+)/`\1/d,define/
both the options does not work.
When :tag `altuvm_test_arg works, but the corresponding CTRL-] key doesn't, this often is caused by incompatible 'iskeyword' definitions, so that actually gets passed to :tag is altuvm_test_arg (without the leading backtick).
You can check the scope of keyword matching (when on such a tag) with the * command, which highlights the same keyword under the cursor. If backticks are excluded, try adding the backtick to the range of keyword characters:
:setlocal iskeyword+=`
If this helps (and doesn't break things like syntax highlighting, or annoys you with the changed navigation of commands like w and e), you can make that setting permanent for verilog filetypes put putting it in ~/.vim/after/ftplugin/verilog.vim.

How can I augment an existing set of syntax rules for a filetype in `vim` without root?

Suppose I wanted to define a new keyword in C called Foo. I do not have root on the system, so I cannot modify the file /usr/share/vim/vim74/syntax/c.vim.
I tried to add the following at the bottom of my .vimrc, but it had no effect at all.
In particular, when I edit a file called Main.c and write Foo, that word is not highlighted the way int is.
syn keyword cType Foo
What is the correct way to augment existing syntax highlighting rules in vim?
Here's the VIM documentation on how to add to existing syntax.
ADDING TO AN EXISTING SYNTAX FILE *mysyntaxfile-add*
If you are mostly satisfied with an existing syntax file, but would like to
add a few items or change the highlighting, follow these steps:
1. Create your user directory from 'runtimepath', see above.
2. Create a directory in there called "after/syntax". For Unix:
mkdir ~/.vim/after
mkdir ~/.vim/after/syntax
3. Write a Vim script that contains the commands you want to use. For
example, to change the colors for the C syntax:
highlight cComment ctermfg=Green guifg=Green
4. Write that file in the "after/syntax" directory. Use the name of the
syntax, with ".vim" added. For our C syntax:
:w ~/.vim/after/syntax/c.vim
That's it. The next time you edit a C file the Comment color will be
different. You don't even have to restart Vim.
You can get there by :help mysyntaxfile-add

Adding syntax highlighting for latex plugin in vim

I am using a package called outlines for LaTeX. It adds commands such as \1 \2 \3 etc.
They are not highlighted by default in vim. So, I created a file called tex.vim in my .vimrc/syntax folder, and put this in the file:
:syn match outline /\\[1-9]/
hi link outline Label
This works only at the top level, not within a block. In other words, it works before my \begin{document}, but not between \begin{document} and \end{document}.
This is pretty much useless. How can I get vim to recognize the syntax, regardless of where it appears in the document?
You need to find the syntax group or cluster defined by the Tex syntax, and use contained containedin=..., but in your case, there is already a syntax group for statements, it's just that it doesn't include numbers. Therefore, you can just piggyback on the existing group and only add matching for numbers:
:syn match texStatement /\\\d/

extend/modify vim highlighting for all filetypes at once?

How do I extend/modify vim highlighting for all filetypes at once?
I have certain relatively simple patterns which I'd like highlight differently, that can occur in any filetype. So rather than adding something like the below to every conceivable filetype I might use (~/.vim/syntax/python.vim, .../css.vim, .../html.vim, ...) is there some way I can define it once for all filetypes?
syn match SpecialComment "#[#\-+].*" containedin=Comment
syn match Comment "\* .*$"hs=s+1 containedin=SpecialComment
update:
As suggested I saved my changes to ~/.vim/after/filetype.vim, with the result that it works in Cream but not stock Gvim or Vim. The actual code I'm using here, a sample python file to test against here, and the desired result:
You could try putting those two lines in ~/.vim/after/filetype.vim. That should get sourced after any of the top level syntax files. It's possibly not the 'correct' place to put it, but it should work.
filetype.vim seems to be sourced BEFORE the syntax files, so it gets overwritten by the default syntax file. Therefore, I'd recommend you create a new file called something like:
~/.vim/after/common_syntax.vim
with the highlight lines that you're interested in. Then, add this to ~/.vim/after/filetype.vim:
if !exists("after_autocmds_loaded")
let after_autocmds_loaded = 1
au BufNewFile,BufRead * source ~/.vim/after/common_syntax.vim
endif
This will cause the file to be sourced once the file has been read.
P.S. Responding to the comment in your sample code: "why can't we use plain ol 'comment' group instead of 'pythoncomment' etc. ?", it's because the syntax highlight group is pythonComment, which is merely coloured in the same way as Comment. If your syntax is unique enough for it not to be a problem, you could just do containedin=ALL. If it is close, but not quite unique, you could do containedin=ALLBUT,conflictgroup where conflictgroup is the highlight group you want to steer clear of.

Resources