xpanta
xpanta

Reputation: 8418

How to add color to parenthesis and brackets in vim using molokai

I am using molokai in vim to code in python/html/css/javascript. When I edit python files (or javascript) parenthesis are not colored. This is not true for simple scripts (like molokai.vim itself) where parenthesis are colored gray.

I edited molokai.vim and added

hi parens guifg=#999999

and then I edited .vimrc and added:

syn match parens /[(){}]/

but parenthesis and brackets remain white.

What am I doing wrong?

Upvotes: 2

Views: 1362

Answers (1)

ZyX
ZyX

Reputation: 53604

  1. Never use :syn to highlight all filetypes, there is matchadd() for this. Using :syn can easily break highlighting, matchadd() is an overlay.
  2. Syntax highlighting is being overridden when Syntax event fires. More, it has effect only on the current buffer. So just syn in vimrc will never work, you have to use autocommands

    autocmd! Syntax python :syntax match Parens /[(){}]/
    

    (for python it is safe as parenthesis and figure brackets are not matched by any other syntax element).

  3. In javascript parenthesis (()) are already matched by javaScriptParens highlighting group. Thus you have to use

    hi def link javaScriptParens Parens
    

    (in colorscheme). Braces are matched by javaScriptBraces and require similar command.

  4. To determine what highlighting is used for specific symbol I place cursor on this symbol and launch

    echo 'Normal '.join(map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")'))
    

    , the last displayed word is normally what you need. If only Normal is displayed then symbol is not highlighted and you have to go 2., otherwise you have to go 3.

  5. For universal solution disregarding currently used highlighting you may use matchadd() as I already said. But it is local to window so if you are working with multiple windows/tabs you can’t go without autocmd:

    autocmd! WinEnter * :if !exists('w:parens_match_id') | let w:parens_match_id=matchadd('Parens', '[(){}]') | endif
    

All autocommands are to be surrounded with

augroup HighlightParens
    autocmd! …
augroup END

Upvotes: 3

Related Questions