Customizing Syntax Highlighting in Vim

前端 未结 2 1038
难免孤独
难免孤独 2021-01-03 07:08

How I can keep all the current formatting for a file type but add functionality.

I would like to highlight colors in .vim files so t

2条回答
  •  清酒与你
    2021-01-03 08:00

    I would expect the ~/.vim/after/syntax/vim.vim approach to work, but if the keyword (yellow) is already highlighted, you may need to change the matcher with something like:

    syn keyword yellow yellow containedin=ALL
    

    What is the file you're trying to highlight?

    Edit

    I've had a look at the java.vim file and it looks like there's a lot of overlapping syntax groups, which can make highlight customisation quite difficult. Here's what I did, in case it's helpful.

    A useful mapping is:

    " What is the current syntax highlighting group?
    map  :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<' . synIDattr(synID(line("."),col("."),0),"name") . "> lo<" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">" . " FG:" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"fg#") . " BG:" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"bg#")
    

    You can then move the cursor over something you're interested in and press F3 to see what the current highlight group is. As a test, I opened java.vim in the syntax directory and went to line 52, which defines a javaStorageClass. I decided to highlight the word transient on that line (as yellow wasn't in the file anywhere and I needed something to work with).

    I moved the cursor over transient and pressed F3. Vim reported:

    hi trans lo FG: BG:
    

    It is obviously part of a vimSynKeyRegion, which one would guess from the name is a syn region. I decided to look at this further, so I interrogated the current highlighting configuration:

    :redir @a>
    :silent syn list
    :redir END
    :vnew
    "ap
    

    This produces a file containing all of the syntax information. I searched for vimSynKeyRegion and found this line:

    vimSynKeyRegion xxx matchgroup=vimGroupName start=/\k\+/ skip=/\\\\\|\\|/ matchgroup=vimSep end=/|\|$/  contained oneline keepend contains=@vimSynKeyGroup 
    

    The vimSynKeyRegion has been configured to contain items from the syntax cluster named vimSynKeyGroup. Therefore, we can highlight transient by making it a keyword in this group:

    :syn keyword MyNewGroup transient
    :hi MyNewGroup guifg=#ff00ff
    :syn cluster vimSynKeyGroup add=MyNewGroup
    

    Although this may not fit exactly into what you're wanting to do, hopefully it will give you something to work with. You may find some parts cannot be overridden (if they're matched with keywords or similar), but you can always redefine the highlighting to change the colour, e.g.

    :hi vimGroupName guifg=#ffff00
    

    Hope all of that helps.

提交回复
热议问题