1

I've got highlighting of searches on and have the following set up to clear it in my vimrc

nnoremap <silent> <Space> :nohlsearch<Bar>:echo<CR> 

I've also got the haskell plugin type information plugin hdevtools installed. This keeps a highlight/selection up once you've asked for the type of an expression in case you want to expand that expression using the type info command again.

I'd like to be able to clear both lots of highlighting with the same key press, previously I'd had it set up to have a different shortcut but it's fiddly.

However my plugin shortcuts are set up with autocmds like this:

autocmd FileType haskell nnoremap <buffer> <silent> <space> :HdevtoolsClear<CR>

I know I could just do copy and paste and a in between but that means if I change this stuff I have to change it in two places.

I'd like some way to do something like this:

clearSearchHighlight = :nohlsearch<Bar>:echo<CR>
nnoremap <silent> <Space> clearSearchHighlight
autocmd FileType haskell nnoremap <buffer> <silent> <space> :HdevtoolsClear<CR> <bar> clearSearchHighlight

2 Answers 2

3

You could do this by setting up your actions as a user command.

You could also use a normal-mode map (which you'd then invoke with :normal), but in this instance you can't use a function because you can't clear highlighting from a function! (See :help :nohlsearch.)

command! ClearSearchHighlight nohlsearch | echo

nnoremap <space> :ClearSearchHighlight<CR>
autocmd FileType haskell nnoremap <buffer> <silent> <space> :HdevtoolsClear<CR>:ClearSearchHighlight<CR>
3
  • Awesome, thanks, exactly what I was looking for. How come you've included '!' after command?
    – JonnyRaa
    Nov 6, 2017 at 14:04
  • 1
    @JonnyLeeds Without the !, if the command already exists that line will throw an error. So if you :so ~/.vimrc you'll get an error. With the !, if the command already exists, it just overwrites it.
    – Rich
    Nov 6, 2017 at 14:04
  • sorry shouldn't have deleted that comment! I managed to find some information about it in help
    – JonnyRaa
    Nov 6, 2017 at 14:04
1

You can bind those commands to Ctrl+L:

nmap <C-l> :nohls<CR>:HdevtoolsClear<CR>:redraw!<CR>

Ctrl+L usually just redraws the screen. Now it will, in addition, clear the highlighting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.