How to highlight printf, scanf in Vim?

1.1k views Asked by At

I've installed macvim with Homebrew; OS X version 10.11.6; macvim version:

macvim: stable 8.0-119 (bottled), HEAD

But it doesn't color printf, scanf by default while editing "my file.c".

I've searched that how to add them by creating ~/.vim/after/c.vim. (another way come through my mind is to edit the default c.vim file directly, but I don't know where they are).

In my ~/.vim/after/c.vim I wrote:

syn keyword printf scarf

and it didn't work...(If I know where the default c.vim file is, I could refer to it and maybe my problem could be solved.)

btw, I download c.vim from vim.org, but I found the last-edited date was in 2010, if I want the C11 standard version where could I get one, if it matters(?).

Is it a good practice that not to change the default syntax-highlight-file like c.vim, and add my own by ~/.vim/after/ or ~/.vim/syntax/?

2

There are 2 answers

2
Ruslan Osmanov On BEST ANSWER

Create ~/.vim/after/syntax/c.vim file (as recommended in the official documentation) with the following content:

syn keyword cFunctions printf scanf
hi link cFunctions Function

As far as I can see, the default syntax rules do not highlight the functions, even from the standard library. So you need to add them yourself.


The syn command adds printf and scanf to the cFunctions highlighting group.

The hi command links cFunctions group to the Function highlight group, which is usually defined by the colorscheme. If the Function group is not defined, you can define it yourself, e.g.

hi Function term=underline cterm=bold ctermfg=LightGray

Is it a good practice that not to change the default syntax-highlight-file like c.vim, and add my own by ~/.vim/after/ or ~/.vim/syntax/?

Yes, of course. The syntax files are usually distributed as packages. For example, on my Gentoo Linux /usr/share/vim/vim80/syntax/c.vim is shipped with app-editors/vim-core package. The local modifications to the syntax files will be overwritten the next time the package is upgraded.

0
Juan On

If you need more than one library, while programing C, for example GTK2, GTK3, glib, gimp, etc; you cand add all of them in the same directory /home/juan/.vim/after/syntax/c/.

Otherwise you just need the /home/juan/.vim/after/syntax/c.vim