Search code examples
vimsyntax-highlightingvim-syntax-highlighting

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?


Solution

  • 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