Setting a keyboard shortcut for a vim command - vim

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?

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>

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()

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.
See another exemple here

Related

Map <C-CR> in vim

Attempting to add a normal mode mapping in vim for Control + Enter to insert line break above cursor positon
:nmap <C-CR> O<Esc>
I am finding this does not work - what am I missing here?
This is probably based on your terminal rather than vim. You can tell what key your terminal sends by <C-Enter> via C-V C-Enter. Most likely this is a newline which you can use as <NL> in vim.
:nmap <NL> O<Esc>
Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today; though this particular mapping should work in GVIM, but not in most terminals. 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.

Vim -- Mapping of <c-<> not working?

I want to map c-< to be <c-w> <, so I put these in my .vimrc:
noremap <c-<> <c-w><
And it doesn't work.
:verbose map <c-<> shows:
<C-<> * <C-W><
Which means the mapping has succeeded.
If I try noremap <c-.> <c-w><, it doesn't work either; but if I try noremap <c-e> <c-w><, it actually works.
I don't understand.. does vim disallow kind of mapping>
You need to find different keys for your mapping - those 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, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8
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.

How do I map keys to assign a value in vim?

I am using highlighted search in vim. So when I search for a word, all occurrences of the word get highlighted. After I'm done, I want to remove the highlighting.
According to the Vim Documentation, :let #/="" clears the last searched pattern and removes the highlighting.
So I am trying to map Ctrl+/ to :let #/="".
I've added the following line to my .vimrc
nnoremap <C-/> :let #/=""<CR>
But it doesn't work. Ctrl+/ doesn't clear the search patter. On the other hand, if I manually type in :let #/="", it works fine.
What am I doing wrong?
I use this in my vimrc:
nnoremap <Esc> :noh<CR>
Mapping this to Esc seems natural to me.
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, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8
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.

.vimrc mapping for control key not working

I have the below mapping in my .vimrc for mapping control key + 1, 2, 3.. for switching tabs. I am using gnome terminal in ubuntu 11.10, the control key mappings does not seem to work. could any one tell what I am doing wrong.
VIM - Vi IMproved version 7.3.154
map <C-S-]> gt
map <C-S-[> gT
map <C-1> 1gt
map <C-2> 2gt
map <C-3> 3gt
map <C-4> 4gt
map <C-5> 5gt
map <C-6> 6gt
map <C-7> 7gt
map <C-8> 8gt
map <C-9> 9gt
map <C-0> :tablast<CR>
syntax on
set shiftwidth=2
First ensure that your terminal emulator (Gnome Terminal) doesn't swallow the key combinations for its own functionality; by default Ctrl + number switches tabs. But I'm afraid you still won't be able to use all those combinations...
Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today. 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.

map `ctrl +` to tabn

I am trying to map CTRL-+ to :tabn. But it does not work.
inoremap <C-+> :tabn<CR>
inoremap <C--> :tabp<CR>
nnoremap <C-+> :tabn<CR>
nnoremap <C--> :tabp<CR>
It does not work in insert mode also not in normal mode.
If I try the following (without CTRL), then it works, but then I can't switch tabs when I am in insert mode:
nnoremap + :tabn<CR>
nnoremap - :tabp<CR>
How can I map CTRL-+ to :tabn so I can switch tabs when in insert mode?
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.
For me (in English Windows GVIM), it is possible to map Ctrl + - as <C-_>, though.
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.
Only a few control-printable key chords can be detected (and therefore mapped) by Vim, and these are listed in the FAQ. Unfortunately <CTRL-+> and <CTRL--> are not mentioned in this list, so it looks like such mappings are impossible.
There is on-going discussion about whether to redesign Vim's key model, but this probably have to wait until Vim 8.0. I am not optimistic.

Resources