Search code examples
vimvim-syntax-highlighting

Is it possible to have vim underline the scope of parentheses my cursor is currently in?


I know there is the rainbow plugin for vim that colorizes opening and closing parentheses and I know that vim can highlight the matching parentheses. I'd like vim to underline everything in the scope of parentheses I am currently in.

For example: Let | be the current cursor position, then:

(a|bc (de fg))

Would underline everything.

(abc (de |fg))

Would underline only (de fg).

Is that even possible?


Solution

  • This match seems to work pretty well:

    syn match Foo /([^(]*\%#.*)/
    hi link Foo Underlined
    

    You can put it in your ~/.vimrc or a syntax file for a particular language.

    Step by step:

    syn match Foo - match a syntax group based on a regex and name it Foo

    /([^(]* - match a ( followed by an unlimited amount or 0 of any character except (

    \%# - match the current cursor position

    .* - match an unlimited amount or 0 of any character

    ) - match a )

    hi link Foo Underlined - link the Foo group to Underlined

    It should be noted that this is an imperfect solution since regex is not ideal for nested substructures. Rainbow Parenthesis accomplishes this by adding up to 13 levels of recursion, but it does not provide infinite recursion. You could modify its 13 level recursion using the regex example I've provided to accomplish an arbitrary amount of recursion. I am not certain of the impacts on speed that might have.