How to get rid of search highlight in Vim - vim

I have :set hlsearch as default value.
When I search for something, search terms get highlighted. However many times I want to get rid of the highlight, so I do :set nohlsearch. In this way I get rid of highlights for the time being.
However if I do a new search, then search terms are not highlighted.
I would like to hit ESC + ESC to get rid of highlights and then set back :set hlsearch.
Any suggestions?

Try the :noh command.
vi/vim notes

I use
/pleasedisablehighlightthanks
command. Or just
/qewrufhiqwe
But you should be carefult not to mix this with the following command!
/qewrufhiqew

:noremap <silent> <c-l> :nohls<cr><c-l>
This would redraw the screen and clear any search terms with Control-L, handy :) easier than reaching up to the F keys.

I have the following in my .vimrc:
map <silent> <C-N> :let #/=""<CR>

This might suit your needs:
nnoremap <esc> :noh<return><esc>
With a little tinkering you can make it work in insert mode.

Try this:
set hlsearch!
nnoremap <F12> :set hlsearch!<CR>
and hit F12 to clear when desired. Use :noh in command mode to clear.

you could search for something not in the text file. Nothing will be highlighted in this case. (e.g. /349i5u9sgh)

This solution toggles the search:
nnoremap <silent><expr> <c-l> (&hls && v:hlsearch ? ':nohls' : ':set hls')."\n" <BAR> redraw<CR>

Related

Navigate in gvim like in most

I'd like to use gvim to view files with long lines. It's a table, so I'm not wrapping the lines.
Is this possible to configure gvim so arrows navigation will be like in "most" tool? Arrow key will move the whole screen 1 character lef/right/top/bottom?
Thanks a lot.
I think this should do what you want.
set nocompatible
set nowrap
set virtualedit=all
nnoremap <Left> zh
nnoremap <Right> zl
nnoremap <Up> <C-y>
nnoremap <Down> <C-e>
If you want the same behavior in insert mode, add the same mappings again as a second set, but use inoremap instead of nnoremap.
The virtualedit setting will allow the cursor to move beyond the end of the line and continue on as if the line had infinite whitespace to the right.
NOTE: virtualedit is only available if Vim was compiled with that feature. You can check with :version. If this feature is available, you should see a + next to it, e.g. +virtualedit.

Normal command map, tab is interpreted as literal

When I make a .vimrc entry,
nnoremap <silent> <leader>c :colorscheme <tab>
The tab is applied if I understand the terminalogy, as a literal, that is, upon typing ,c, I get in Vim command line,
:colorscheme ^I
I tried to internet the search terms, but mostly I get results about remapping Vim Tabs; the closest I found was somebody putting quotes around their <tab>, but I think that is for a different desired outcome.
I also have this, which is why I want the tab in my shortcut,
set wildmenu
set wildmode=longest:list,full
You'll need the 'wildcharm' option:
set wildcharm=<C-z>
nnoremap <silent> <leader>c :colorscheme <C-z>
See :help 'wildcharm'.
As a side note, I use that option with great effect for switching buffers:
nnoremap <leader>b :buffer <C-z><S-Tab>
and a file-opening variant would be just as easy and just as useful:
nnoremap <leader>e :edit <C-z><S-Tab>

Toggle highlight while searching in Vim

I like highlighting while searching in vim. Here's what I want:
I search for a word with /
Then, all of the results are highlighted. If I press any key other than n or N, I want the highlighting to be toggled off.
If I press n or N again after any number of commands, I want to toggle on the highlighting.
Where do I start? I'm not even sure what to google.
I have this in my .vimrc
nnoremap <CR> :noh<CR>
so that when I'm done seeing the highlighting, I just hit enter to remove it. It stays gone until I hit n or N again.
Note: If you want to keep the functionality of enter, add another <CR> on the end of the command.
I remap control-l (lower case L) so that it clears the search result as well as repaints the screen. This line in .vimrc does it:
nnoremap <silent> <C-l> :nohl<CR><C-l>
You can manually disable the last highlight with nohl.
I will let you know if I can figure out how to automate this.
One method is to setup a toggle mapping. These are some toggle mappings I have in my .vimrc:
let mapleader="\\"
noremap <silent> <Leader>th :set invhls hls ?<CR>
noremap <silent> <Leader>tn :set invnumber number ?<CR>
noremap <silent> <Leader>ts :set invspell spell ?<CR>
noremap <silent> <Leader>tw :set invwrap wrap ?<CR>
To toggle highlighting just type \th for toggle hls. The others are line number, spell checking, line wrapping. The final hls ? will display the new mode.
I prefer this, because to me it is nothing but natural.
Start searching with /<pattern> and once done, simply type <Leader>/ to stop the highlighting.
nnoremap <silent> <Leader>/ :set nohl<CR>

Map key to toggle between normal mode and insert mode in Vim

I'd like to use two "controls" as a toggle key to switch between normal mode and insert mode in Vim. So I add the following two lines into my .vimrc
nmap <C-><C-> i
imap <C-><C-> <ESC>
But it doesn't work. What's wrong with the above two lines?
It seems you are trying to map Ctrl+Space to toggle insert mode.
nnoremap <C-space> i
imap <C-space> <Esc>
(Came from this Vim tip (marked obsolete, but there's a link to a more rich document on avoiding which includes the tip).)
Remember that this is not guaranteed to work across all terminals and platforms. Some terminals and platforms may eat a given Ctrl+something shortcut, while others don't, so find one that works in your environment.
nnoremap <silent><C-space> :startinsert
inoremap <silent><C-space> <C-O>:stopinsert
That's definitely not going to work. You could use an F key instead.
nnoremap <C-SPACE> i
inoremap <C-SPACE> <ESC>l
works perfectly with GVIM 7.4

How do you make Vim unhighlight what you searched for? [duplicate]

This question already has answers here:
Vim clear last search highlighting
(32 answers)
Closed 3 years ago.
I search for "nurple" in a file. I found it, great. But now, every occurrence of "nurple" is rendered in sick black on yellow. Forever.
Forever, that is, until I search for something I know won't be found, such as "asdhfalsdflajdflakjdf" simply so it clears the previous search highlighting.
Can't I just hit a magic key to kill the highlights when I'm done searching?
:noh (short for nohighlight) will temporarily clear the search highlight. The next search will still be highlighted.
Just put this in your .vimrc
" <Ctrl-l> redraws the screen and removes any search highlighting.
nnoremap <silent> <C-l> :nohl<CR><C-l>
/lkjasdf has always been faster than :noh for me.
" Make double-<Esc> clear search highlights
nnoremap <silent> <Esc><Esc> <Esc>:nohlsearch<CR><Esc>
Then I prefer this:
map <F12> :set hls!<CR>
imap <F12> <ESC>:set hls!<CR>a
vmap <F12> <ESC>:set hls!<CR>gv
And why? Because it toggles the switch: if highlight is on, then pressing F12 turns it off. And vica versa. HTH.
Append the following line to the end of your .vimrc to prevent highlighting altogether:
set nohlsearch
*:noh* *:nohlsearch*
:noh[lsearch] Stop the highlighting for the 'hlsearch' option. It
is automatically turned back on when using a search
command, or setting the 'hlsearch' option.
This command doesn't work in an autocommand, because
the highlighting state is saved and restored when
executing autocommands |autocmd-searchpat|.
Same thing for when invoking a user function.
I found it just under :help #, which I keep hitting all the time, and which highlights all the words on the current page like the current one.
I think the best answer is to have a leader shortcut:
<leader>c :nohl<CR>
Now whenever you have your document all junked up with highlighted terms, you just hit , + C (I have my leader mapped to a comma). It works perfectly.
I search so often that I've found it useful to map the underscore key to remove the search highlight:
nnoremap <silent> _ :nohl<CR>
I think this answer in "Vim clear last search highlighting" is better:
:let #/ = ""
There is hlsearch and nohlsearch. :help hlsearch will provide more information.
If you want to bind F12 to toggle it on/off you can use this:
map <F12> :nohlsearch<CR>
imap <F12> <ESC>:nohlsearch<CR>i
vmap <F12> <ESC>:nohlsearch<CR>gv
I have this in my .vimrc:
nnoremap ; :set invhlsearch<CR>
This way, ; will toggle search highlighting. Normally, the ; key repeats the latest t/T/f/F command, but I never really used that functionality. I find this setting much more useful, because I can change search highlighting on and off very quickly and can easily get a sense of where my search results are, at a glance.
Also, if you want to have a toogle and be sure that the highlight will be reactivate for the next time you search something, you can use this
nmap <F12> :set hls!<CR>
nnoremap / :set hls<CR>/
I add the following mapping to my ~/.vimrc
map e/ /sdfdskfxxxxy
And in ESC mode, I press e/

Resources