This a common question on the internet, but after a lot of research I still can't do it.
This is what I have on my .vimrc file:
map <up> <nop>
map <down> <nop>
map <left> <nop>
map <right> <nop>
imap <up> <nop>
imap <down> <nop>
imap <left> <nop>
imap <right> <nop>
And I still can move my courser using the arrow keys.
What am I doing wrong ?
You should use noremap and inoremap instead of map and imap, because those disallows nested and recursive use of mappings, so the original meaning of <left>, <right>, ... will be disabled. It needs nocompatible mode.
Explanation in the help of noremap:
Map the key sequence {lhs} to {rhs} for the modes
where the map command applies. Disallow mapping of
{rhs}, to avoid nested and recursive mappings. Often
used to redefine a command. {not in Vi}
Related
I'm learning vim and I would like to unbind h and l (left and right movements).
What should I include in my .vimrc?
Note: By doing that I'm forcing myself to use w, W, b, B, e, and other movements to move horizontally.
Use nop to suppress the effect of a command:
nnoremap h <NOP>
nnoremap l <NOP>
You could study/enhance the "dogmatic.vim: Arrow keys are heretical" plugin, which not only disables the keys but also displays a message to remind you. You would need to duplicate the following lines:
noremap <silent> <right> <Esc>:call dogmatic#TrackArrowPress('right')<cr>
noremap <silent> <left> <Esc>:call dogmatic#TrackArrowPress('left')<cr>
noremap <silent> <up> <Esc>:call dogmatic#TrackArrowPress('up')<cr>
noremap <silent> <down> <Esc>:call dogmatic#TrackArrowPress('down')<cr>
If you follow this approach you could consider providing a patch/pull request to the plugin author, possible by creating an option which allows the plugin user to enable/disable your modifications.
I am having trouble to activate the following mappings:
nnoremap <buffer> <C-K> <c-w>w
nnoremap <buffer> <C-S-K> <c-w>W
Where, I want have "pressing Ctrl+K in normal mode" to send me to the next buffer (of the same screen), in a clockwise way. <c-w>W in Vim will go the other direction. I want to map this action to be Ctrl+Shift+w.
Any idea why the mapping fails? I found only the latex-suite.vim has a mapping of <c-w> and none <c-s-w> has been mapped. The plugin mapping is buffer only.
Thank you for your input.
All the best,
-Linfeng
With the <buffer> keyword, your mappings only apply to the current buffer. So, when you have these in ~/.vimrc, they aren't effective, and if you type them in, you would need to have the same current buffer visible in multiple window splits. I don't think that's what you want. Instead, define them to be global mappings:
nnoremap <C-K> <c-w>w
nnoremap <C-J> <c-w>W
Edit: I've replaced <C-S-K> with <C-J>, because shifted control sequences are generally not available in Vim, unfortunately.
They could still be overwritten by some filetype plugins using nnoremap <buffer> <C-K>.
Alternative
I'd prefer the following set of directional mappings. They provide more precision, at the cost of wasting more keys:
nnoremap <C-j> <c-w>j
nnoremap <C-k> <c-w>k
nnoremap <C-h> <c-w>h
nnoremap <C-l> <c-w>l
I'm trying to map <Esc> to turn off search highlighting in Vim. The problem is that keys simulated by the terminal with +Esc are affected.
The terminal sends characters much fast than I type. Is there perhaps a way to map key + timeout in vim?
The same question was asked 4 years ago and the answer was that it can't be done. Is this (still) true?
Mapping :nohlsearch to escape key
Your troubles are being caused by some plugin or other, native vim handles this fine. Start vim with vim --noplugin, or if that's not enough then bypass your vimrc with vim -u NONE (or gvim -U NONE) and :source this:
set nocp " life's too short for pure vi-compatibility mode
set timeout ttimeout " enable separate mapping and keycode timeouts
set timeoutlen=250 " mapping timeout 250ms (adjust for preference)
set ttimeoutlen=20 " keycode timeout 20ms
nno <ESC> :nohls<CR>
I've never seen and can't reproduce the interference you're describing so I don't know what's causing it, all I can suggest is binary search with your plugin set.
Yes, it's still not possible for the reason given by ZyX in his answer.
<Esc> is "special" because its behavior sits between a "normal" key like a (you can map it to whatever you want) and a modifier key (it's used by the terminal to represent a lot of special keys like <Up>).
Safely mapping <Esc> to do anything else/more than <Esc> is possible but you'll have to noremap all the affected keys. Here is what I have in my vimrc to mitigate that side effect:
nnoremap <Esc>A <up>
nnoremap <Esc>B <down>
nnoremap <Esc>C <right>
nnoremap <Esc>D <left>
inoremap <Esc>A <up>
inoremap <Esc>B <down>
inoremap <Esc>C <right>
inoremap <Esc>D <left>
There is an opinion that when working in vim you should not use Esc key (use ctrl+c instead) and don't use arrow keys (use h,j,k,l) on you keyboard. But it is difficult to not to use those keys. I thought that there is a way to disable those keys in .vimrc so there will be no other option but to use ctrl+c and hjkl.
I've searched a bit and found a solution on this link.
So I've inserted the following in my .vimrc file:
inoremap <Up> <NOP>
inoremap <Down> <NOP>
inoremap <Left> <NOP>
inoremap <Right> <NOP>
inoremap <Esc> <NOP>
noremap <Up> <NOP>
noremap <Down> <NOP>
noremap <Left> <NOP>
noremap <Right> <NOP>
noremap <Esc> <NOP>
But this does not work. Adding this to my .vimrc breaks my mapping to the
function keys. The another problem is that it does not block the function of arrow keys rather when I press Down in normal mode multiple actions are performed - the cursor goes up one line, the new line is created and the character 'B' is inserted.
How can I disable in my vim 7.2 the cursor keys and Esc key without breaking anything else?
If you're using vim in a terminal you should absolutely not remap Escape. Because of the way keys are handled in vim (and probably terminals in general), remapping it will break all kinds of keys you didn't intend on changing. To see what I mean, do the following.
Open up vim with no startup files: vim -u NONE --noplugin -N.
Enter insert mode.
Press Ctrl-v followed by any of the function keys, such as <F2>.
Notice the sequence that is entered. It very likely begins with ^[ which is a literal Escape.
Now open try the following:
:inoremap <esc> NO ESCAPE FOR YOU
Enter insert mode.
Press any of the function keys, like <F2>.
If the previous sequence showed the escape character as part of the <F2> key press, you'll now see our new string printed to the screen. In fact, now that you have the mapping, try to move around using the cursor keys. You'll probably notice the same bizarre behavior.
In conclusion, don't remap escape, I almost guarantee you will have unexpected consequences.
Here's a non-geeky way of achieving what you want: Crumple pieces of paper to the size of your thumb and tape them to the keys. The moment your finger tries to reach them you'll bump into the paper instead. They'll become a good reminder. Keep them taped there until you stopped bumping into them.
What you had was close:
inoremap <esc> <NOP>
inoremap <Left> <NOP>
inoremap <Right> <NOP>
inoremap <Up> <NOP>
inoremap <Down> <NOP>
nnoremap <Left> <NOP>
nnoremap <Right> <NOP>
nnoremap <Up> <NOP>
nnoremap <Down> <NOP>
This line was causing you trouble:
noremap <Esc> <NOP>
I have <Up> and <Down> nnoremapped to gk and gj but this won't let me use them while in edit mode. I tried using inoremap but that just types out gk or gj.
So I could certainly do something like inoremap <Up> <ESC>gki. Is this the best and only reasonable way to do it? I don't like this method because it isn't apparent to somebody reading the settings file what it does. Not that I could say that about any bit of vim setting file I have ever seen.
To execute a normal mode command in insert mode, use
Control+o. Straight from the help:
CTRL-O execute one command, return to Insert mode *i_CTRL-O*
So something like this:
inoremap <Up> <C-O>gk
inoremap <Down> <C-O>gj
Might be more readable.