I want to highlight the search keyword in Vim. Mainly I use vim to debug the logs.

I always have to use :se hlsearch to highlight the search keyword.

So, I want the solution that it should set command permanently, and I should not use the command always when vim starts.

4

Best Answer


Set the command in .vimrc.

Use the following commands:

  1. Open ~/.vimrc file (or create it if it didn't exist).
  2. Add set hlsearch in the file.
  3. Save the file.

Now your search will always be highlighted in vim.

For single time use, just use :set hlsearch in vim, which will be in effect for that instance only.

If you want to highlight multiple searches (in parallel, with different colors), check out my Mark plugin; it also allows to persist the highlightings across Vim sessions through the viminfo file; cp. :help mark-persistence.

For those wanting to visually keep highlighted their search:

:syn match stupid "ctrl + /"

:hi stupid ctermbg=Red guibg=Red

Explanation:

The first line add your regex to a syntax type called "stupid" (note that ctrl + / means you must press ctrl+R then / to get the content of the search registry).

The second line gives a red color to the "stupid" syntax regex.

My SelX plugin allows you to highlight and search for many different things at once on a per-tab basis, with different colours. The highlight config can be saved to a vim session

https://www.vim.org/scripts/script.php?script_id=5875