mapping function keys in vim - vim

I want to map my F2 for nerdtree with the following entry:
map <F2> :NERDTreeToggle<CR>
But even before that, and after saving the vimrc , whenever i press F2, it just switches the case of the letters on which the cursor is present. Later found out that any function key does it. F5 switches case of 5 characters and so on. Is this because of some other plugin? I presently use c.vim , snippetsEmu , surround , nerdtree , and minibufexpl
There are no keymappings to any function key in my vimrc.

Your problem is that vim does not know what does terminal emit when you press <F2>. On some terminals it emits something like <Esc>[12~, so the vim quits current mode (or just beeps if it can't) (<ESC>), does nothing ([1: there must be some key after [, but not 1, so it does nothing) and changes case of two letters (2~). So, you should open .vimrc and write there the following:
set <F2>=<C-v><F2>
where <C-v><F2> means that you must press <C-v> and then <F2>. This line should tell the Vim the exact sequence of codes which is emitted by terminal when you press <F2>. After that, use noremap <F2> whatever and it should work. If it is not the only terminal that you are using, then you may want to put if $TERM==#"<C-r>=$TERM<CR>" before this line and endif after.

:map <F2> :NERDTreeToggle<CR>
After starting Vim you can look with
:map <F2>
what F2 is mapped to. It is possible that the plugins change the mapping (not visible in .vimrc)

Related

VIM: set term=xterm changes <BS> to <Del>, is it reversible?

I have added set term=xterm to my vimrc to be able to use 256-color vim schemes in terminal, but it comes at a price (at least for me). It also replaces (sort of) the BackSpace with Delete (i.e. BackSpace starts to delete forward just like Delete does) in insert mode and makes it "BackSpace" instead of moving left (or h) in normal mode. I have nothing against Ctrl-H as a way "to Backspace", but I simply don't want to have two delete buttons and ability "to BackSpace" (delete backward) in normal mode.
How can I reverse that change while retaining the setting I need?
PS I've read :h CTRL-h and a bit of :h xterm, but I couldn't find the solution.
Vim's inoremap and nnoremap commands can be used to adjust how keys are interpreted in Vim.
A solution is documented here for your specific context: https://conemu.github.io/en/VimXterm.html
The relevant quote:
"If you have problems with BS in Vim (BS acts like Delete key) under ConEmu when term=xterm, you may try to remap BS key:
inoremap <Char-0x07F> <BS>
nnoremap <Char-0x07F> <BS>
"
In general, when a key does not do what you want, the trick is to find out what it actually sends to Vim. Sometimes hitting Ctrl-V followed by that key in insert mode might help figure it out. Then inoremap and nnoremap as shown above can be used to reassign it to the behaviour you want in insert and normal modes, respectively.

VIM un-mapped keys enter text into the buffer

When I press a key such as F2 while in insert mode, <F2> will be entered into the buffer. I would like to use the function keys as hotkeys however this is impracticable when I always need to go and delete the piece of text.
Is there a way to disable this in vim? I couldn't find anyone else with this problem so I am quite stuck with this one.
Edit: ps Im using VIM 8.0 in gui mode.
You can map the key to no operations for doing nothing if pressed in insert mode. So, i guess this would do the trick:
inoremap <F2> <Nop>
If you prefer that F2 should do nothing (vim specific) in all vim modes it would be:
noremap <F2> <Nop>

Gtk vim remove mapping of <C-Y>

I used classic vim, but recently i moved to gtk-vim on ubuntu, but emmet plugin don't working, it is because when I press <C-Y> it copies above char to cursor position, i failed to disable it. When i set unmap or iunmap <C-Y> it writes that there is no maping, also this shortcut is not noted in imap.
EDIT:
I find out that i'm unable to imap, when i press any shortcut it just print it out, for instance
map <F2> :echo 'Current time is ' . strftime('%c')<CR>
is working well but
imap <F2> :echo 'Current time is ' . strftime('%c')<CR>
is printing <F2> to text
It looks like you've :set paste, which disables insert mode mappings and abbreviations. This option is meant for pasting text from the terminal (e.g. via middle mouse button); it should only be set during the paste itself, not permanently (and you can easily toggle this via the 'pastetoggle' option).
So, check via :verbose set paste?, then find the place where this is set, and disable it.
PS: Your demo imap needs <C-o> before it; else (if imaps are working, that is), you'd just insert the :echo ... into the buffer.
imap <F2> <C-o>:echo 'Current time is ' . strftime('%c')<CR>

Setting mouse=a and mapping <esc> to :noh breaks mouse working on vim? [duplicate]

I am a happy VIM user, although I admit I'm quite far from being fluent. I found this nice post:
Vim clear last search highlighting and I thought I'd become a better person if I didn't have to hammer away a random character sequence every time I did a search. Now, I'm also using the vimrc config from here:
http://amix.dk/vim/vimrc.html
and the problem I have is that when I add the line nnoremap <esc> :noh<return><esc> to it (it doesn't seem to make a difference where I put it) I get awkward behaviour when I use arrows in command mode, namely letters from A to D appear in a newline and I get switched to insert mode.
There has to be some mapping conflict but for the life of me I can't figure out where it is.
EDIT: As it follows from the answers it turns out the Ultimate vimrc part is not relevant, the mentioned nnoremap command will cause altered arrow behaviour even if it's the only vimrc entry. Changing title to a more informative one.
PS. I know I shouldn't use arrows, hopefully I'll get there one day.
The mapping
nnoremap <esc> :noh<return><esc>
will conflict with so called "grey keys" and I believe that it should be used either in GVim only or in terminal Vim by someone who does not use special keys like arrows.
From what I know (and guess) how Vim processes keys, I would say that it's impossible to do anything with this. For Vim to recognize special key all its components should go in a row, so when you press Arrow Left Vim gets the following sequence of codes:
<esc> [ D
But after your mapping Arrow Left becomes:
: n o h l <cr> <esc>
[ D
Vim sees two separate sequences and treats <esc> as a single press of Escape key, thus next two codes of Left Arrow key lose their special meaning.
So I suggest you to map :noh to some other key sequence (e.g. to one starting with <leader>, see :help mapleader; I don't recommend you to use F-keys, using them is as bad as using of arrow keys).
The cause had been explained well, but solution was not mentioned. However there is a straight one.
If you’ll tell to Vim explicitly that there are key sequences starting from <esc>[
:nnoremap <silent><esc> :noh<CR>
:nnoremap <esc>[ <esc>[
than when single <esc> will be pressed Vim will wait for a second (or different time, see :h 'timeoutlen') or for a next key (second <esc> for example) and only then replace it with :noh<CR>.
This solution preserves the ESC mapping to :nohlsearch.
The comment on this answer explaining why this is happening tells us that the root cause is the TermResponse behavior of vim. This can be compensated for by wrapping the mapping in an autocommand for the TermResponse event.
This ensures that the binding doesn't happen until after the term response is set, which prevents Esc from also sending a string like ]>1;3201;0c to vim.
Change your line in vimrc to this:
augroup no_highlight
autocmd TermResponse * nnoremap <esc> :noh<return><esc>
augroup END
The augroup commands are not strictly necessary, but they prevent multiple mappings when you reload your vimrc without quitting vim.
EDIT: If you also use a graphical vim like Gvim or Macvim, the TermResponse event will not fire. Assuming you use a single vimrc, you'll need some additional code like
if has('gui_running')
nnoremap <silent> <esc> :nohlsearch<return><esc>
else
" code from above
augroup no_highlight
autocmd TermResponse * nnoremap <esc> :noh<return><esc>
augroup END
end
Problem is that when you press an arrow terminal emits something like <Esc>OA. Vim part that supports terminal seems to use the same mapping mechanism to do the job as you are using: while nmap <Esc>OA will tell you nothing, call feedkeys("\eOA") will move one line up and call feedkeys("\eOA", 'n') will add letter A beyond current line. With your mapping being noremappable you forbid vim to use <Esc> as a part of the key. The problem is that you need remappable mapping here, but can have remappable mapping without it being recursive as well only if it starts with {lhs}, but <Esc>:noh<CR>OA is not going to work. I thought the following code will (it uses <expr> and function with side effect to make <Esc> be the first character of the actual {rhs} and still launch :noh), but in fact it does not:
function s:NoHlSearch()
nohlsearch
return "\e"
endfunction
nmap <expr> <Esc> <SID>NoHlSearch()
. I have no other idea how to solve the problem of having non-recursive remappable mapping which includes {lhs} but not at the start.
I have had good luck with this
if $TERM =~ 'xterm'
set noek
endif
nnoremap <silent> <esc> <esc>:noh<cr>
The disadvantage is that function keys can not be used in insert mode.
:h ek

Why won't Vim use my key mapping when I start a new session?

After mapping the F2 key in Vim in the following sequence, it does not work in a new Vim session:
:map <F2> :ls<CR>
Pressing F2 here displays the files.
:mkexrc!
Close Vim.
Reopen Vim.
Pressing F2 now does nothing.
The mapping is visible in the _exrc file as:
map <F2> :ls
What am I missing?
I think that you are simply missing a literal <CR>. Type this command in the command line followed by Enter.
:map <F2> :ls<CR>
If it doesn't work…
See if that _exrc file is executed by Vim: :scriptnames should display it near the top.
If it is executed, see if <F2> is mapped elsewhere: :verbose map <F2>.
Anyway, Vim is not Vi. You should use :mkvimrc!. Or a normal $HOME/_vimrc.
edit
FYI I just created a ~/.exrc file using :mkexrc! and it's not executed by Vim at startup. You should definitely use a normal $HOME/_vimrc.

Resources