How to always clear hlsearch on clicking enter? - vim

Right now I am using:
nnoremap <cr> :nohlsearch<cr><cr>k
But after I press Enter my cursor goes to beginning of line.
I have additional <cr>k because I want to use default <Enter> behaviour for example when I try to open file in Ack results quickview.

If you want Enter to keep the default behavior, then this mapping should take care of it.
nnoremap <CR> :nohlsearch<CR><CR>
It turns off search highlighting, moves the cursor down, and only applies to normal mode.

An alternative approach is to locally override the new behavior in the quickfix window and the command-line window, where <CR> has special meaning:
:nnoremap <CR> :nohlsearch<CR>
:autocmd BufReadPost quickfix nnoremap <buffer> <CR> <CR>
:autocmd CmdwinEnter * nnoremap <buffer> <CR> <CR>

Related

vim mapping problem in vimrc file but can not find the mapping that I set

I am using vim-markdown-toc plugin(successfully installed) and want to remapping some hotkey to specific function. I export this code autocmd Filetype markdown noremapb <silent> <C-x> :GenTocMarked to my .vimrc file. But when I type :verbose imap <C-x>, it shows can not find the mapping.
Can anyone tell me what's the problem about this?
and also I also want to ask how to map one hotkey to multiple function?
autocmd Filetype markdown noremapb <silent> <C-x> :GenTocMarked
has two obvious errors:
noremapb should be noremap, without the b:
noremap <silent> <C-x> :GenTocMarked
There should be a <CR> at the end:
noremap <silent> <C-x> :GenTocMarked<CR>
The right-hand-side of a mapping is a macro: since you press <CR> to execute the command :GenTocMarked, it should be present in the RHS.
Then comes the diagnostic mistake: the :map command, and its non-recursive buddy :noremap create mappings for normal, visual, and operator-pending modes, but :imap prints out insert mode mappings so you can't really expect it to find a mapping created with :map.
Then comes the semantic mistake: the re in noremap is part of nore (short for non-recursive), not of remap. <C-x> is not a mapping so you are not "remapping" anything.
Then comes the scoping mistake: :noremap creates mappings for three modes, which is something you probably don't want. You should be more specific:
" normal mode mapping
nnoremap <silent> <C-x> :GenTocMarked<CR>
and, finally, the autocommand abuse mistake: there already is a built-in mechanism for sourcing filetype-specific config so there is no need to reinvent the wheel in your vimrc:
" in after/ftplugin/markdown.vim
nnoremap <buffer> <silent> <C-x> :GenTocMarked<CR>

vim quickfix avoid buffer change

I'm using vimgrep with cw to open the search results in the quickfix buffer. Every time I select a line in the quickFix list the cursor shifts to the new file buffer. How do I avoid this and keep my cursor in the quickFix list always?
Assuming you are using <CR> to "select a line in the quickFix list", you can simply remap <CR> in the quickfix window so that you come back to the previous window after jumping to the matching line.
Make sure you have filetype plugin indent on and add the line below to ~/.vim/ftplugin/qf.vim:
nnoremap <buffer> <CR> <CR><C-w>p
You can use :cnext and :cprevious combined with a <C-w>p (focus on previous window).
To bind them to e.g. <Leader>cn and <Leader>cp, you could do:
nnoremap <silent> <Leader>cn :cnext<CR><C-w>p
nnoremap <silent> <Leader>cp :cprevious<CR><C-w>p

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>

Vim: Remove mapping created by vim-plugin

I use the Vimwiki-Plugin a lot, but remapping <Backspace> and <CR> is just anoying. If I use :nmap, the mapping is shown:
n <CR> #<Plug>VimwikiFollowLink
n <Backspace> #<Plug>VimwikiGoBackLink
If I try to remove tha mapping with :nunmap <CR> I get an "E31: No such mapping" error. Is there a way to give <CR> and <Backspace> its's normal behaviour back?
if you want to just disable it, you could give
:nunmap <buffer> <CR>
because it is a buffer-local mapping.
or
:h vimwiki_<cr>
you found:
<CR> Follow/create wiki link (create target wiki page if
needed).
Maps to |:VimwikiFollowLink|.
To remap: >
:nmap <Leader>wf <Plug>VimwikiFollowLink
if you remap that to another key, e.g. the keys in example <leader>wf, the <cr> would be reset to normal.
because in its code, vimwiki has:
if !hasmapto('<Plug>VimwikiFollowLink')
nmap <silent><buffer> <CR> <Plug>VimwikiFollowLink
endif
same for the <BS>

Navigating in Vim's Command Mode

I am a long time emacs user learning Vim. Emacs lets me navigate in the mini-buffer (where I issue commands like C-x C-s) using the same navigation keyboard shortcuts as in any other buffer. For example, I can navigate forward one character using C-f, even while in the mini-buffer. I could also use the arrow keys, but they are too far away.
Is there any keyboard shortcut to navigate in Vim's command mode (:), without using the arrow keys -- equivalent to emacs C-f, C-b? Thanks.
Adding to Greg Hewgill's answer, you can use q: to open the command-line window, where you have any Vim editing power at your hand.
Some from the Vim help:
CTRL-B or <Home>
cursor to beginning of command-line
CTRL-E or <End>
cursor to end of command-line
CTRL-H
<BS> Delete the character in front of the cursor (see |:fixdel| if
your <BS> key does not do what you want).
<Del> Delete the character under the cursor (at end of line:
character before the cursor).
CTRL-W Delete the |word| before the cursor. This depends on the
'iskeyword' option.
CTRL-U Remove all characters between the cursor position and
the beginning of the line.
I have these in my .vimrc
cnoremap <C-a> <Home>
cnoremap <C-e> <End>
cnoremap <C-p> <Up>
cnoremap <C-n> <Down>
cnoremap <C-b> <Left>
cnoremap <C-f> <Right>
cnoremap <M-b> <S-Left>
cnoremap <M-f> <S-Right>
With the default key bindings, vim does not offer non-arrow-key navigation of the command line editing. However, see :help cmdline-editing for an example of how to use the :cnoremap command to set up alternate key bindings.
I achieved that with <C-p> and <C-n> to navigate previous and next commands respectively.
P.S I'm not making any custom binding like Tassos did.

Resources