0

when using setline() to insert brackets, matchpairs does not do its job correctly. Using these commands

fu Test()
    cal setline(".", "()")
endf

au TextChangedI * cal Test()

set up the text

(

)

upon typing ( into the middle line, (with matchpairs including (:)) you will get

(
|(|)
|)|

(|| represents what's highlighted)

I've tried disabling and reinstating matchpairs but I couldn't get that to work. How can I make it behave as it should?

2

The 'matchpairs' option itself doesn't perform any highlighting. It's the standard plugin matchparen that actually does the highlighting.

The problem is that the autocommands matchparen uses to implement the highlighting don't line up well with your code*. For a quick fix, try disabling the highlighting before making your change, and reinstating it afterwards:

function! Test()
    NoMatchParen
    call setline(".", "()")
    DoMatchParen
endfunction

* Specifically, what's happening is that you are entering the (, the matchparen autocommand fires and highlights the last ), then your autocommand fires and changes the text (but not the highlighting). Then, (at least in my configuration) after a pause, another matchparen autocommand will fire and update the highlighting to be correct.

  • Turns out just a DoMatchParen afterwards is enough to fix it! – Mason Oct 10 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.