How to highlight search words in vim permanently? - vim

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.

Set the command in .vimrc.
Use the following commands:
Open ~/.vimrc file (or create it if it didn't exist).
Add set hlsearch in the file.
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

Related

Neovim doesn't highlight search while typing %s command?

I've recently made the switch from Vim to Neovim. In Vim, when you make a global substitution by doing :%s/query/substitution/g, as you're typing query, it highlights all the matching characters in the file. But in Neovim, this is not the case.
Say you have a file that says "potato," and you type :%s/pot, it'll highlight the first three characters in potato in the file.
Why doesn't Neovim do this? I have incsearch enabled, but this seems to have no effect.
You need to have both incremental search and highlight search.
Add the following line to your rc file:
set is hls
Then source the rc file and you should see the highlighting.
If not could you retry with a blank rc file and only that line to see if there are conflicts.
After re-reading your question, It seems you are only wanting the incsearch behavior not is and hls as i assumed. By default this is working for me without any additional lines on version NVIM v0.5.0-dev+948-g2debabb08 Could you verify that the highlight color is set to something different than the background color in :hi and that it still doesn't work in an otherwise blank rc file?

Highlighting set of specific keywords in gvim

I have tried highlighting specific words by adding them in .vimrc, colorscheme file and also in syntax.vim(I changed one at a time, not altogether).
syn match mygroupwords '\c\<\(-word1\|-word2\)'
hi def link mygroupwords colo_words12
hi colo_words12 guifg=red1 gui=bold guibg=white
But somehow it seems to be getting overwritten by default syntax highlighting
i need to highlight keywords irrespective of color-scheme or file-type which have following words-
Ex; -word1 , -word2
Any suggestions?
explanation of your failed attempts
A colorscheme just provides color definitions for predefined highlight groups; that's the wrong place for actual syntax matches! ~/.vimrc is the first configuration that is read; if a filetype is detected and a corresponding syntax script is loaded, that will override your syntax definition.
syntax extensions
If your desired highlightings are extensions to an existing syntax, you can place :syntax match commands in a syntax script in the after directory. For example, to extend Python syntax, put it in ~/.vim/after/syntax/python.vim. That may still fail if the original syntax obscures the match; sometimes, this can be solved via containedin=...
independent marks
If your highlightings are independent of syntax and filetype, there's a different built-in mechanism: :match (and :2match, and additional variants via :call matchadd(...)):
:match mygroupwords /\c\<\(-word1\|-word2\)/
This goes on top of (and is independent of) syntax highlighting. However, it is local to the current window. So, if you put this into your .vimrc, it will only affect the first window (but any files viewed in there). To apply this globally to window splits and tab pages, you have to use :autocmds. It's not trivial to get this totally right. If you need such a full solution, have a look at my Mark plugin; this supports multiple colors, allows presetting with :[N]Mark /{pattern}/ (similar to :match), and highlights in all windows.

Make vim see # as a part of a word

For example, suppose we have a file with three lines:
#capitalism
caps
#cap
if the cursor is after #cap and I press c-n, I want it autocomplete to #captalism instead of offering me two options.
What belongs to a word is controlled via the 'iskeyword' option. To do this just for the current file:
:setlocal iskeyword+=#
If this is for a particular filetype, put this into ~/.vim/after/ftplugin/<filetype>.vim. To change the global default, use :set in your ~/.vimrc, but be warned that this can affect syntax highlighting.

How to turn off Vim search highlighting

i have a pre-configured .vimrc file under ~/ ,after searching a term through '/' key,all items matched are highlighted,but after i find the need position and hit enter,the highlighted terms are still highlited... it's very disappointing since when i issue --c/"--,the screen is mashed with highlightings.which setting controls search highlighting facility?thanks!
You can issue :nohl to turn off highlighting until the next search. I would recommend mapping that to a key.
:map <F4> :nohl<CR>
If you want to turn off highlighting forever, use :se nohlsearch in your .vimrc
If you type ":nohls<enter>" it will turn off the search highlighting. (The next time you do a search, it will be highlighted, this is not a permanent setting. It is useful for turning off the highlighting for the current search.)
Not sure if that's what you're looking for.
Vim clear last search highlighting

How to prevent Vim indenting wrapped text in parentheses

This has bugged me for a long time, and try as I might I can't find a way round it.
When I'm editing text (specifically latex, but that doesn't matter) files, I want it to auto-wrap at 80 columns. It does this, except if I happen to be in the middle of a parenthetical clause, it indents the text which is very annoying. For example, this works fine
Here is some text... over
two lines.
but this doesn't
Here is some text... (over
two
lines
If anyone can tell me how to turn this off (just for text/latex files) I'd be really grateful. Presumably it has something to do with the fact that this is desired behaviour in C, but I still can't figure out what's wrong.
:set nocindent
The other options do nothing, and the filetype detection doesn't change it.
There are three options you may need to turn off: set noai, set nosi, and setnocin (autoindent, smartindent, and cindent).
This may be related, when pasting from gui into terminal window, vim cannot distinguish paste modes, so to stop any odd things from occuring:
set paste
then paste text
set nopaste
I had similar issues trying to paste xml text, it would just keep indenting. :)
gvim, the gui version of vim, can detect paste modes.
You can have a look at the autoindent option :
autoindent - ai
Copy indent from current line when starting a new line (typing
in Insert mode or when using the "o" or "O" command). If you do not
type anything on the new line except and then type or
, the indent is deleted again. When autoindent is on,
formatting (with the "gq" command or when you reach 'textwidth' in
Insert mode) uses the indentation of the first line. When
'smartindent' or 'cindent' is on the indent is changed in specific
cases. The 'autoindent' option is reset when the 'paste' option is
set. {small difference from Vi: After the indent is deleted when
typing or , the cursor position when moving up or down is
after the deleted indent; Vi puts the cursor somewhere in the deleted
indent}.
From the official Vim documentation
filetype plugin indent on
This switches on three very clever
mechanisms:
Filetype detection. Whenever you start editing a file, Vim will try to
figure out what kind of file this
is. When you edit "main.c", Vim will
see the ".c" extension and
recognize this as a "c" filetype.
When you edit a file that starts with
"#!/bin/sh", Vim will recognize it as
a "sh" filetype. The filetype
detection is used for syntax
highlighting and the other two
items below. See |filetypes|.
Using filetype plugin files Many different filetypes are edited with
different options. For example,
when you edit a "c" file, it's very
useful to set the 'cindent' option to
automatically indent the lines. These
commonly useful option settings are
included with Vim in filetype plugins.
You can also add your own, see
|write-filetype-plugin|.
Using indent files When editing programs, the indent of a line can
often be computed automatically.
Vim comes with these indent rules for
a number of filetypes. See
|:filetype-indent-on| and
'indentexpr'.
:set noai
sets no auto indent tt may be smartindent though. Check out the doc and see if you can find something more
http://www.vim.org/htmldoc/indent.html

Resources