Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Esc and cursor keys in vim

Tags:

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?

like image 742
bessarabov Avatar asked Dec 13 '11 11:12

bessarabov


People also ask

How do I change the Esc key in Vim?

If you use Vim in a terminal, simply press alt/meta+normal_mode_key. Most terminals send an escape character followed by the normal_mode_key that you pressed, removing the need to press escape yourself. Thus in insert mode pressing alt+h alt+j alt+k alt+l all take you to normal mode and move in the expected direction.

When using vi vim the Esc key always returns you to which mode?

Vi has the concept of modes. You are always working in one of three modes: Command mode , Insert mode or Line mode . The modes in vi are Command mode which you can enter by hitting the Esc key, Insert mode by using i , I , a , or A .

How do you press Esc without Esc?

While you cannot bring back a missing physical Escape key, you can replicate its functionality with a little help from a nifty universal keyboard shortcut, and here's how: To invoke Escape, press Command + period (.) on the keyboard. This useful system-wide synonym for Escape works across iOS, iPadOS, and macOS.

What type of key is Esc?

The Esc key, simply “Esc” on a keyboard, is a control key. Like the function keys or the Alt and Ctrl keys, it doesn't generate a character when writing a text. The Escape key originates from command line-based operating systems like DOS and UNIX.


1 Answers

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.

like image 166
Randy Morris Avatar answered Sep 24 '22 06:09

Randy Morris