How to get matching brace highlighting without cursor jump in vim - 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)

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'

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.

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.

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.

Related

VIM colors in Ubuntu20.04 terminal

After going down the wrong rabbit hole, looking at themes for terminal in Ubuntu20.04 I realized what I really needed to change was the colorscheme in vim. I have found they are listed in /usr/share/vim/vim81/colors/*.vim
One of the things I don't understand, is that Putty seems to have the colors I like, am used to, and can most easily see, when when I type ":colorscheme" while a file is open in the vim editor, it outputs "default". I DO NOT understand why the default theme looks correct to me in PuTTy, but not in Terminal. If I had the option of changing the font family and size in PuTTy in Ubuntu20.04 I would likely just use it. As it is, it is too small, I can barely see the font. Thus, I need to use terminal. However, the colors make it hard for me to see.
The colors I want (specifically for Python, but putty just always has the right colors) are the ones that PuTTy seems to be using when I type ":colorscheme default" but I want these colors for terminal.
Does anyone know how to get "terminal" to display the same colors that PuTTy displays? Or has anyone created a custom "*.vim" color file that I can download and use as the colorscheme in VIM for my terminal?
Terminal, using ViM81's :colorscheme=default:
VIM, using ViM81's :colorscheme=default:
If any one can provide a link to a page that tells me what to do, or to a colorscheme.vim file for terminal's VIM, I'd really appreciate it.
A colorscheme is essentially a sequence of Ex commands like this one:
highlight Foo cterm=NONE ctermbg=red ctermfg=224 gui=NONE guibg=#ff0000 guifg=#ffdfdf
that define, for a given highlight group (for Foo, here):
a text style for terminal emulators with cterm,
a background color for terminal emulators with ctermbg,
a foreground color for terminal emulators with ctermfg,
a text style for GUI Vim with gui,
a background color for GUI Vim with guibg,
a foreground color for GUI Vim with guifg.
Since you are talking colors and terminal emulators, only ctermbg and ctermfg are relevant.
ctermbg and ctermfg can be given different kinds of values whose effect might differ depending on your environment:
1-15 would mean "use color at given index in the so-called ANSI palette",
red would mean "use color with given name in the so-called ANSI palette",
16-255 would mean "use color at given index 224 in the so-called xterm palette".
There are literally thousands of custom colorschemes in existence and colorscheme authors may favor one notation over the other, mix them, or even don't use them at all because they only care about GVim. "YMMV" is not a joke when talking about Vim colorschemes.
What notation is used by the default colorscheme mostly depends on your environment, which has thus a lot of influence on how things look.
For example, if your $TERM is something like xterm, Vim assumes that your terminal emulator can only display 8-16 colors so it uses the first notation. This means that the colors you see are all taken from your terminal emulators so-called ANSI palette, which a) changes from terminal emulator to terminal emulator, and b) can generally be modified by the user anyway. In this context, there is no way whatsoever for Vim to make any guarantee about what colors will be used in the end.
If your $TERM is something like xterm-256color, Vim assumes that your terminal emulator can display 256 colors so it uses, and that's really annoying, a mix of the first and third notations. Colors 16-255's big advantages over colors 0-15 are a) that they are not easily changed by the user, and b) that they can reasonably be expected to be the same on most terminal emulators that implement them. The annoying bit is that Vim uses lots of colors in the 0-15 range, which can't be trusted, in combination with colors in the 16-255 range, which can be trusted.
The ultimate consequence is that default can't be expected to be consistant across terminal emulators without some fiddling.
Where to go from there?
First, if you expect n Vims, in n terminal emulators, on n platforms to behave the same, the very least you have to do is:
make sure $TERM matches,
make sure it actually is the exact same Vim version with the exact same patches applied,
make sure colors 0-15 of all terminal emulators are the exact same,
and, since you mentioned font size, make sure you have the exact same font installed everywhere and all your terminal emulators set to use it at the exact same size.
If you want to stick with default, then checking all of the above should get you closer to where you want to be. If you can't, then you can give up right away.
Second, Vim comes with lots of alternative colorschemes out of the box, so you could try them and see if there is one that works better for you:
:colorscheme <Tab>
Third, as mentioned above, you could try some of the many custom colorschemes publicly available. Be aware that they may come with different sets of features and different requirements.
The default colors for Vim are specified in terms of the 16-color ANSI palette. This palette goes back to EGA, and it roughly defines eight colors, possibly with a bold (bright) version of each. The difference that you're seeing is that the definition is approximate. For example, color 1 is red and color 4 is blue, but nobody mandates what color that red and blue are. Specifically, in many cases, the regular (non-bright) yellow is brown.
If you want to set the colors to be different in Ubuntu, you should be able to configure the terminal emulator in its settings. GNOME terminal, which is what it looks like you're using, should allow this. The Wikipedia page on ANSI terminal codes contains information on which actual colors are mapped to different color numbers.
Thanks to romainl I got it all working the way I wanted it to.
In VIM, when I typed in ":colorscheme" it output "default",... YET when I typed ":colorscheme default" it actually changed the colors to ALMOST match what was in PuTTy. Then, I just had to add a few adjustments to my ~/.vimrc and ~/.vim/syntax/python.vim files. Below are those adjustments...
In my .vimrc file, I had to move:
syntax on
filetype detect
filetype plugin on
From the bottom of my .vimrc file, to almost the very the top, as it was getting overwritten by something. Then, I added one line above that (:colorscheme default (to force it to switch to the "real" default")), and 5 lines below it. Now my .vimrc contains (amongst MANY other customizations):
colorscheme default
syntax on
filetype detect
filetype plugin on
hi Comment ctermbg=black ctermfg=green cterm=None
hi String ctermbg=black ctermfg=1 cterm=None
hi Statement ctermbg=black ctermfg=yellow cterm=None
hi StatusLine ctermbg=0 ctermfg=7
hi StatusLineNC ctermbg=252 ctermfg=238 guibg=#d0d0d0 guifg=#444444
Additionally, my python syntax file (located at ~/.vim/syntax/python.vim) needed these adjustments:
hi Statement ctermbg=black ctermfg=yellow cterm=bold
hi pythonStatement ctermbg=black ctermfg=yellow cterm=None
hi Conditional ctermbg=black ctermfg=yellow cterm=None
hi pythonComment ctermbg=black ctermfg=green cterm=None
hi pythonString ctermfg=1 cterm=None
hi pythonQuotes ctermfg=blue cterm=bold
Again, special thanks to the answer above from: romainl with his/her help, and the above customizations, I was able to get my VIM colors in Ubuntu20.04 Terminal to match my VIM colors in PuTTy that I was used to.
P.S. My term-type is xterm
I'm not sure if this is exactly what you are looking for, but... I ran into a problem that the color schemes in my terminal did not look like they looked on the previews on https://vimcolorschemes.com.
Adding the following lines solved to ~/.vimrc solved problem for me:
if !has('gui_running')
set t_Co=256
endif
set termguicolors

How to change Operator highlight color in Vim?

I just switched to iTerm2 on macOS and found some of its options go to Vim as well. That's the case for fonts, I was able to select a Vim font that I like from iTerm options.
It seems iTerm color options also apply to Vim highligh. But in this case highlight situation is not good enogh for my taste. So I tried to change:
:hi Comment ctermfg=gray
Changes font color of comments.
I would like to change =, ::, +, - color, I think these are Operators, so I tried:
:hi Operator ctermfg=blue
But it seems nothing changes. How should I do that?
If it matters I'm playing with .cpp/.h files.
The =, +, -, etc. are not defined as Operator in
the default C syntax, which C++ inherits from. Usually
Operator means keywords like sizeof, typeof, and so
on.
Since you describe your terminal settings "leaking" to Vim,
it's very likely that your colorscheme is not defining the
ctermfg and bg. Either for a custom matched Operator
group or for Normal (most likely). You can test that
easily by setting Normal to some color, like in your
example:
:hi Normal ctermfg=blue
If the + et all changes, sure they are not in fact being
matched as Operator. To fix it, if you agree on setting
your Normal group color then that's all there is to it —
Normal is the group for default text, unmatched syntax
elements. When your colorscheme does not set it, it will use
the terminal colors.
If however you want to have the operators in a different and
exclusive color, you will need to match them manually by
defining a new syntax item (:h syn-define).

How to change the color of Vim's (straight) underline?

I've tried everything changing the color of this:
call s:h("Underlined", {"fg": s:norm, "gui": "underline", "cterm": "underline"})
As well as all the colors of the link texts.
Does anybody know how to change it?
Here's a picture:
For anyone finding that in 2021, you can use guisp=red in neovim at least.
For example I use
:hi CocErrorHighlight gui=undercurl guisp=red
to have red curly lines.
I am using NeoVim in the Xfce-Terminal.
For a long time, you couldn't; the underline color always equaled the text color. AFAICT, in terminals this wasn't supported, and for consistency, Vim also didn't offer this in GVIM.
With Vim 8.2.0863, the ctermul attribute allows setting a separate color for underline / undercurl now. Apparently, this still isn't supported in GVIM, though (which I find odd, because Vim usually values consistency in features over fancy stuff (as explained by :help design-not)).
In GVIM, you could switch to the (GUI-only) undercurl attribute (mostly used for spell checking), which supports a separate "special color", set via guisp={color-name}; see :help highlight-guisp.
You can colorize underline only with ctermul(independent with ctermfg).
See this commit enabling that.

How to make vim's replacement selection blinking

In latex files I have the on-fly spelling switched so you can imagine that together with the syntax highlighting there is already a lot of colors in the terminal screen. When in such environment I am running a string replacement, say with :%s/x/y/gc I need a lot of time to identify where the string-to-be-replaced is located in all this color mess. Is it possible to make vim's current selection blink?
You change the highlighting of the to-be-replaced text with highlight IncSearch.
For example,
:highlight IncSearch ctermfg=Red guifg=Red
highlights the text with a red background (despite it being fg not bg).
However, I don't think there's any way to make it blink (and are you sure you really want that?). You might be able to use italics or underlines instead. See :h highlight for the options that are available.

Is it possible to not display a ~ for blank lines in Vim/Neovim?

Is it possible to not display a ~ for blank lines in Vim?
This confuses Mac Vim's scrollbar, and I quite don't like these tildes.
:hi NonText guifg=bg
That command should set the color of non text characters to be the same as the background color.
Vim 8.x:
You can now change the color just for the end of the buffer ~:
highlight EndOfBuffer ctermfg=black ctermbg=black
See changelog for Vim 8.x.
As jamessan said, you can’t disable them. The scrolling behavior isn’t specific to MacVim, either — it works the same way in the terminal and in gvim:
Instead of seeing this as a problem, what you should do is learn to see this as part of Vim’s flexibility. For example, you can use the zt command to scroll the current line to the top of the screen, regardless of where in the file it is. This can make it easier to write macros that do some work and then scroll back to where you were. The commands <C-E> and <C-Y> are made simpler because of this, as is the 'scrolloffset' option.
If you must, retrain your brain to think of Vim’s scrollbar as mapping to which line is on top, instead of which screenful is visible.
For NeoVim, you can set the fillchars value for eob to a space character and that will effectively hide it. (This might not work for plain Vim).
In Lua (Nvim 0.5+):
vim.wo.fillchars='eob: '
In VimScript:
set fillchars=eob:\
Note: Calling the above will override your fillchars value for other items as well (if set), so use this as a reference to set multiple values together:
set fillchars=eob:\ ,fold:\ ,vert:\│
Or use set fillchars+=... to append it your existing values.
You can't disable them, but you can change your colorscheme such that the NonText highlight group is colored the same as the Normal highlight group. However, this affects more than just the end of document tildes.
I doubt that it's actually "confusing" MacVim's scrollbar and if it is, then that's a bug in the patching that MacVim does.
The tilde ~ characters are meant to remind the user that those lines are not part of buffer content.
The above highlight trick will hide the ~ character, but it is still there. For some terminals, this may not even work. If you happen to be a Neovim user, you can use fillchars option to change the end of buffer symbol like this:
set fillchars=fold:\ ,vert:\│,eob:\ ,msgsep:‾
This will use space instead of ~ for end of buffer, effectively hiding the annoying ~.
You may also be interested in discussions here.

Resources