How do I map keys to assign a value in vim? - 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.

Related

Tab is doing weird things when I edit html

Here is the relevant piece of vimscript:
inoremap <c-i> <i></i><esc>F<i
I added this to ~/.vim/ftplugin/html.vim to make writing in italics easier. For some reason, whenever I'm in insert mode (even in a non-html file), and I press the tab key, I get <i></i> in my text. Any idea what could be wrong?
First off, you should be using inoremap <buffer> <c-i>... if you don't intend to infect non-HTML files.
Secondly, Ctrl-I and Tab are equivalent. AFAIK you can't map one without affecting the other. You might want to select a different mapping. See this question for more details.
Due to the way that the keyboard input is handled internally, you cannot map <Tab> / <C-I> separately, 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 <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 release.

Remap Ctrl+' to go to end of line in Insert mode in Vim

I'm unable to remap Ctrl+' to go to the end of the line when in Insert mode. This line in my vimrc file doesn't work (doesn't seem to do anything at all):
inoremap <c-'> <end>
However, if I use other keys instead of the single quote, it works fine. For example, this works:
inoremap <c-e> <end>
Do I need to use a different syntax to use the single quote key in a key remapping like this?
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.

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.

Map backslash in VIM

I have Vim EasyMotion running by pressing:
Backslash Backslash <key>
I want to map this as Ctrl+, so I can do Ctrl+, <key>:
map <C-,> <Bslash><Bslash>
map <C-,> \\
But none of them worked. Has the backslash be escaped somehow?
TL;DR: <C-,> won't work; choose another key combination.
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.
According to :help map_backslash, you are right to use <Bslash>. The problem is that vim does not seem to recognize <C-,> in the :map command. For example,
:map <C-,> :echo 'foo'<CR>
does not have the expectred effect. Either dig into this further or choose a different key.

Resources