Roel
Roel

Reputation: 19622

Vim: search no longer highlights word under cursor

I have recently installed Vim on a new computer, and it seems the behaviour of highlighting wrt 'search' has changed. When I search for a word (either using / or with * for 'search word under cursor'), the word that the cursor is currently on (so when I jump to the next search result, the highlighting changes) is no longer highlighted. I'd like for all search results to be highlighted, regardless of where the cursor is. How do I configure this? Thanks.

Edit: in response to followup questions in an answer below, I can add:

Search xxx term=reverse ctermfg=0 ctermbg=14 guifg=#90fff0 guibg=#0030ff CurSearch xxx term=reverse

However, if (after starting vim), I do 'set background=dark', then CurSearch becomes

CurSearch xxx links to Search

and the behaviour of keyword highlighting changes to what I want.

Now, I used to have 'set background=dark' in my vimrc, but apparently I changed it somewhere in the last few years (it's commented out in the vimrc that I've taken along with me across machines since the late 1990's.). I can't find 'background' in the manual any more as an option that can be set though, I'm not sure what's going on with that.

> C:\Users\Roel>"c:\Program Files (x86)\Vim\vim90\vim.exe" --version VIM
> - Vi IMproved 9.0 (2022 Jun 28, compiled Jun 28 2022 13:10:49) MS-Windows 32-bit console version Compiled by appveyor@APPVYR-WIN Huge
> version without GUI.  Features included (+) or not (-):
> +acl                +ex_extra           +multi_lang         -tag_any_white
> +arabic             +extra_search       +mzscheme/dyn       -tcl
> +autocmd            -farsi              -netbeans_intg      +termguicolors
> +autochdir          +file_in_path       +num64              +terminal
> +autoservername     +find_in_path       +packages           -termresponse
> -balloon_eval       +float              +path_extra         +textobjects
> +balloon_eval_term  +folding            +perl/dyn           +textprop
> -browse             -footer             +persistent_undo    -tgetent
> ++builtin_terms     +gettext/dyn        +popupwin           +timers
> +byte_offset        -hangul_input       -postscript         +title
> +channel            +iconv/dyn          +printer            -toolbar
> +cindent            +insert_expand      +profile            +user_commands
> +clientserver       +ipv6               +python/dyn         +vartabs
> +clipboard          +job                +python3/dyn        +vertsplit
> +cmdline_compl      +jumplist           +quickfix           +vim9script
> +cmdline_hist       +keymap             +reltime            +viminfo
> +cmdline_info       +lambda             +rightleft          +virtualedit
> +comments           +langmap            +ruby/dyn           +visual
> +conceal            +libcall            +scrollbind         +visualextra
> +cryptv             +linebreak          +signs              +vreplace
> +cscope             +lispindent         +smartindent        +vtp
> +cursorbind         +listcmds           +sodium/dyn         +wildignore
> +cursorshape        +localmap           +sound              +wildmenu
> +dialog_con         +lua/dyn            +spell              +windows
> +diff               +menu               +startuptime        +writebackup
> +digraphs           +mksession          +statusline         -xfontset
> -dnd                +modify_fname       -sun_workshop       -xim
> -ebcdic             +mouse              +syntax             -xpm_w32
> +emacs_tags         -mouseshape         +tag_binary         -xterm_save
> +eval               +multi_byte_ime/dyn -tag_old_static    system vimrc file: "$VIM\vimrc"
>      user vimrc file: "$HOME\_vimrc"  2nd user vimrc file: "$HOME\vimfiles\vimrc"  3rd user vimrc file: "$VIM\_vimrc"
>       user exrc file: "$HOME\_exrc"   2nd user exrc file: "$VIM\_exrc"
>        defaults file: "$VIMRUNTIME\defaults.vim" Compilation: cl -c /W3 /GF /nologo -I. -Iproto -DHAVE_PATHDEF -DWIN32  -DFEAT_CSCOPE
> -DFEAT_TERMINAL -DFEAT_SOUND  -DFEAT_JOB_CHANNEL -DFEAT_IPV6    -DHAVE_SODIUM -DDYNAMIC_SODIUM -DDYNAMIC_SODIUM_DLL=\"libsodium.dll\" /I "C:\libsodium\include" -DWINVER=0x0501 -D_WIN32_WINNT=0x0501
> /source-charset:utf-8 /MP -DHAVE_STDINT_H /Ox /GL -DNDEBUG /arch:IA32
> /Zl /MT /D_CRT_SECURE_NO_DEPRECATE /D_CRT_NONSTDC_NO_DEPRECATE
> -DFEAT_MBYTE_IME -DDYNAMIC_IME -DDYNAMIC_ICONV -DDYNAMIC_GETTEXT -DFEAT_LUA -DDYNAMIC_LUA -DDYNAMIC_LUA_DLL=\"lua54.dll\" -DFEAT_PYTHON -DDYNAMIC_PYTHON -DDYNAMIC_PYTHON_DLL=\"python27.dll\" -DFEAT_PYTHON3 -DDYNAMIC_PYTHON3 -DDYNAMIC_PYTHON3_DLL=\"python310.dll\" -DFEAT_MZSCHEME -I "C:\racket\include" -DMZ_PRECISE_GC -DDYNAMIC_MZSCHEME -DDYNAMIC_MZSCH_DLL=\"libracket3m_da32rk.dll\" -DDYNAMIC_MZGC_DLL=\"libracket3m_da32rk.dll\" -DFEAT_PERL -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DDYNAMIC_PERL -DDYNAMIC_PERL_DLL=\"perl532.dll\" -DFEAT_RUBY -DDYNAMIC_RUBY -DDYNAMIC_RUBY_DLL=\"msvcrt-ruby300.dll\" -DRUBY_VERSION=30 -DFEAT_HUGE /Fd.\ObjCULYHRZi386/ /Zi Linking: link /nologo /opt:ref /LTCG oldnames.lib kernel32.lib advapi32.lib shell32.lib gdi32.lib 
> comdlg32.lib ole32.lib netapi32.lib uuid.lib user32.lib  /machine:i386
> libcmt.lib   /nodefaultlib:lua54.lib  /STACK:8388608 
> /nodefaultlib:python27.lib /nodefaultlib:python310.lib    winmm.lib
> WSock32.lib Ws2_32.lib   /PDB:vim.pdb -debug

When Googling for 'CurSearch', I found another question on here that deals with exactly my problem: gvim not highlighting searched word where cursor is located (I hadn't found this when searching before posting my question). However the solution there doesn't help me, as I don't have any custom highlight rules.

So I looked at darkblue.vim, and there is one place that does hi CurSearch term=reverse. It's only done when s:t_Co >= 0. That variable has value 16777216 for me, which is as expected. I don't understand why the colorscheme would order tests for s:t_Co from highest to lowest though. What I mean is, it starts with if s:t_Co >= 256 (then change a bunch of highlight rules), if s_t_Co >= 16 (then use other settings) and so on. So it seems that it would always use the last (if s:t_Co > 0), and thus set CurSearch to term=reverse. But all color schemes do it that way, so I'm probably just misunderstanding.

Finally, I also don't understand why set background=dark would fix my problem.

So that's where I'm at. Any hints appreciated.

Upvotes: 3

Views: 656

Answers (1)

romainl
romainl

Reputation: 196546

Historically, Vim didn't make the current match visible but it introduced a new highlight group specifically for highlighting the current match: CurSearch. It is linked to Search by default so it is expected to look just like any other match by default. Here, the current match is the second:

def

If you give it its own styling attributes or link it to another highlight group, you can get a separate highlight for the current match:

link

Now, it might be a bug in Vim or in the colorscheme you are using. Adding the full output of $ vim --version to your question, mentioning what colorscheme you are using, and if you use :help 'termguicolors' might help us narrow down the issue. Adding a screenshot might help, too.

Upvotes: 2

Related Questions