How to disable Esc and cursor keys in vim - vim

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>

Related

How to remap arrow keys in Vim in insert mode?

When I am in insert mode I want to use the keys h, j, k and l to behave as the arrow keys.
I want to achieve this by holding the ctrl key and then ctrl + k would move the line upwards in insert mode. Since these four keys are already mapped to move the lines when not in insert mode, how can I achieve this behavior when I am in insert mode?
You can use imap for that like this:
inoremap <C-h> <Left>
inoremap <C-j> <Down>
inoremap <C-k> <Up>
inoremap <C-l> <Right>

MacVim how to disable arrow navigation?

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}

What mode is vim in when I press a single 'r' and is it possible to have a mapping here?

For a quick overview, I map <s-space> to <esc> so I can more easily cancel out of things without moving my hand to the escape key. For example, when I want out of insert mode, or to cancel something.
inoremap <esc> <nop> " to force me to stop using <esc>
cnoremap <esc> <nop>
nnoremap <s-space> <nop>
onoremap <s-space> <esc>
inoremap <s-space> <esc>
However, if I press 'r' vim is waiting for a character, and when I press <s-space>, I end up replacing with a space, instead of canceling the replace operation. Is it possible for mappings to work after pressing 'r' once, while waiting for a character?
Thanks!
If you find you've accidentally pressed r, how about just tapping uu?
This will make the replacement but immediately undo it again, without you having to move your hands from the letter keys.

Vim UTF8 characters in terminal

So I am editing a UTF8 file in terminal vim and all of a sudden these weird characters come up when I press then navigation keys. So the image attached shows one such character being printed onto the screen after moving up from the end of the file. These guys are merely printed but never saved into the buffer. When that line moves out of view and then back, the character is not printed again. This is a completely random event and never seems to happen at the same location. One exception is I am at the end of the file and happen to press the down key repeatedly.
EDIT: New image for :Set term=cons25
These escape characters appear when Vim is confused about what key presses it receives from the terminal emulator. The arrow keys are received as Escape followed by a character from A to D:
^]0A is <up>,
^]0B is <down>,
^]0C is <right>
^]0D is <left>.
Editing an UTF-8 file has nothing to do with your issue.
Here is what I have in my /.vimrc to work around that problem:
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>
I'm not aware of a better solution.

Vim: arrow keys to move within a line in insert mode

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.

Resources