Say I want <C-*>
to provide me the functionality of the:set nohlsearch
command. How do I accomplish this? The map command only seems to be able to map a set of keystrokes with another set. How can a key combination be mapped to a command?

- 6,477
- 14
- 53
- 76
-
You should change the question to note the desire that the command include a 'capital letter' (i.e. modified) – New Alexandria Sep 20 '13 at 12:44
-
@NewAlexandria : I do not understand your suggestion, could you clarify? – Amal Antony Sep 20 '13 at 14:00
-
"Setting a keyboard shortcut in Vim" is, itself, too trivial of a task to ask on this site — which is why you're bing downvoted. You need to make the question be the more-correct and non-obvious matter: the use of modifiers like `shift` – New Alexandria Sep 20 '13 at 14:21
-
@NewAlexandria : Even without the rephrasing, the question seems okay to me. The remap works for key combination to key combination mapping. My doubt was, how could I do it for combination to command mapping. I missed the
in the command, which was partly why the map was not happening. The fact that some symbols cannot be mapped is a new thing I learned today! If you still would like the question to be rephrased, please feel free to edit it! Also if you're the person who down-voted me, I request you to kindly reconsider your vote. Thanks much! – Amal Antony Sep 20 '13 at 14:34 -
@NewAlexandria : I will not be editing the question since it looks okay to me the way it is now. Thanks anyways for your suggestion. – Amal Antony Sep 20 '13 at 14:56
3 Answers
You'd do it like this:
:nnoremap <C-*> :set nohlsearch<CR>
<C-*>
means pressing Ctrl and Shift and 8 (on an English keyboard layout, at least) simultaneously. Unfortunately, that particular combination won't work.
Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today, even in GVIM. Some key combinations, like Ctrl + non-alphabetic cannot be mapped, and Ctrl + letter vs. Ctrl + Shift + letter cannot be distinguished. (Unless your terminal sends a distinct termcap code for it, which most don't.) In insert or command-line mode, try typing the key combination. If nothing happens / is inserted, you cannot use that key combination. This also applies to <Tab>
/ <C-I>
, <CR>
/ <C-M>
/ <Esc>
/ <C-[>
etc. (Only exception is <BS>
/ <C-H>
.) This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.
Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals.
But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.
What you can do is choose another key combination, e.g. one of the function keys:
:nnoremap <F5> :set nohlsearch<CR>

- 822,522
- 95
- 1,334
- 1,252

- 167,457
- 16
- 250
- 324
-
BTW, I'd really like to have that fixed; I'm running out of key combos :-) With the standard reply, I'll be able to find all those questions when I field a request to Bram to finally tackle this problem (when I feel the time is ready). – Ingo Karkat Sep 20 '13 at 12:47
As others have already pointed out, all types of keybindings are not possible due to the way the strokes are sent to the terminal. However, to accomplish what you are asking for (:nohlsearch) this code below allows you to toggle the highlighting by pressing space.
set nocompatible
let g:highlighting = 0
function! Highlighting()
if g:highlighting == 1 && @/ =~ '^\\<'.expand('<cword>').'\\>$'
let g:highlighting = 0
return ":silent nohlsearch\<CR>"
endif
let @/ = '\<'.expand('<cword>').'\>'
let g:highlighting = 1
return ":silent set hlsearch\<CR>"
endfunction
nnoremap <silent> <expr> <Space> Highlighting()

- 119
- 6
You should be able to do so in your .vimrc :
nnoremap <C-*> :set nohlsearch<CR>
though I am not sure is always a supported shortcut.