rhuffstedtler
rhuffstedtler

Reputation: 508

How to get matching brace highlighting without cursor jump in vim

I am running vim 7.3 on several machines. By default MatchParen is enabled on all of my instances. Using gvim on my windows machine, it is doing exactly what I want - when my cursor is on a bracket, paren, etc. it visually highlights the match. It does not affect cursor navigation. However, on my Ubuntu boxes, when I move the cursor onto the character, it actually jumps to the match.

I'm sure that the behavior is caused by MatchParens because if I do a :NoMatchParen, it stops. Unfortunately, I also don't get the highlighting at that point. I can't figure out where my settings differ, though.

I'll like you even more if you can point me towards a plugin that will always highlight the closest enclosing pair of characters around my current position (like a code oriented version of MatchTagsAlways)

Upvotes: 2

Views: 4179

Answers (4)

Romain Vincent
Romain Vincent

Reputation: 3581

Just like FDinoff said in the accepted answer, this is probably only a problem of colors.

So if the color of the matching "paren" disorients you, tweaking colors of background and foreground is likely the solution.

But HOW do you do this?? ^^

I've had quite a journey through the vimdoc (it was not easy). I've tested a whole bunch of variables and found that the relevant tweak is the [hi]ghlight command and specifically the MatchParen group.

The solution

Adding this in my .vimrc did the trick:

hi MatchParen ctermfg=208 ctermbg=bg

Note that vim config files are read from top to bottom, and some types of "words" are matched by several options. For example, a boolean could also be a keyword. Thus you have to pay attention to the order of these options.

How does this work?

My problem was that the background had the flashy color while the foreground had the color of the background of my terminal, which made it really confusing. Thus switching colors was the solution for me. But maybe you will have to tweak it differently.

First, you can check the current value for highlight MatchParen by entering the following command (while inside vim, in normal mode):

:hi MatchParen

You'll see hi MatchParen followed by XXX in the current style, followed by a list of argument=value separated by spaces.

The important arguments are ctermfg and ctermbg for the "terminal" vim, guifg and guibg for the "gui" vim. (Where fg means foreground and bg means background)

You can change a value and see the result in real time. Just put your cursor over a match character and enter the following command:

:hi MatchParen SomeArgument=SomeValue

This will not be saved, so don't worry. When you find a proper combination of values, you can add them in your .vimrc as shown above.

Personally, I set ctermfg to 208 (orange color) and ctermbg to bg (a keyword for the current background color, if known by vim).
If you use vim in a gui, take a look here for the available choice of colors.

Upvotes: 5

wintermute
wintermute

Reputation: 516

When showmatch is set, the cursor is actually jumping, and the following line fixes the problem:

set matchtime=0

More details: http://vimdoc.sourceforge.net/htmldoc/options.html#'matchtime'

Upvotes: 5

joharr
joharr

Reputation: 535

Running default gVim (v7.4.461) without any configuration (i.e. no .vim files) in openSUSE 13.2 Legacy 32 Bit, :set showmatch? reveals that showmatch is on at start, which is not Vim's stated default behaviour. We can account for this by adding :set noshowmatch in our .vimrc.

Upvotes: 1

FDinoff
FDinoff

Reputation: 31449

The cursor isn't jumping. The color scheme probably has defined bad colors for the MatchParen highlight group which makes it look like the cursor is jumping.

Upvotes: 2

Related Questions