bessarabov
bessarabov

Reputation: 11851

How to disable Esc and cursor keys in 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?

Upvotes: 11

Views: 8114

Answers (3)

shennyg
shennyg

Reputation: 161

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>

Upvotes: 3

Randy Morris
Randy Morris

Reputation: 40927

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.

  1. Open up vim with no startup files: vim -u NONE --noplugin -N.
  2. Enter insert mode.
  3. 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:

  1. :inoremap <esc> NO ESCAPE FOR YOU
  2. Enter insert mode.
  3. 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.

Upvotes: 19

holygeek
holygeek

Reputation: 16185

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.

Upvotes: 8

Related Questions