Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable the arrow keys in insert mode?

Tags:

vim

I have the following lines in my .vimrc, which I thought would be more than sufficient:

nnoremap <left> <nop>
nnoremap <right> <nop>
nnoremap <up> <nop>
nnoremap <down> <nop>

inoremap <left> <nop>
inoremap <right> <nop>
inoremap <up> <nop>
inoremap <down> <nop>

The arrow keys don't do anything (as expected) in normal mode, but continue to function in insert mode. I've also tried imap and that doesn't work either. Querying the mapping with inoremap <left> confirms that the binding isn't getting overwritten by something else. I'm not sure what's going on.

As one last test, I started up vim with no vimrc with vim -u NONE, then only ran the mappings. If I enter insert mode and hit an arrow key now, it inserts the letters A, B, C or D on a new line where my cursor is, which is completely not what I was expecting.

like image 683
Alex Avatar asked May 08 '14 17:05

Alex


People also ask

Can I disable my arrow keys?

Tap 'Layout & keys' Check or uncheck 'Arrow Keys'

How do I get rid of the cursor with arrows?

You might have gotten it by pressing the Alt key and the spacebar together, releasing them, and then pressing the M key. (The Alt+space shortcut opens the “system menu” from the little program icon in the top left corner, and M is the letter that selects Move in that menu.) To get rid of it, just press the Esc key.


2 Answers

Based on this page, you can do it with the following mappings.

 noremap  <Up> ""
 noremap! <Up> <Esc>
 noremap  <Down> ""
 noremap! <Down> <Esc>
 noremap  <Left> ""
 noremap! <Left> <Esc>
 noremap  <Right> ""
 noremap! <Right> <Esc>

I tested them and they work, but I do not currently understand why this magic works.

In particular, I am not sure why it is necessary to map twice, but it should be effective for solving your problem.

like image 118
merlin2011 Avatar answered Sep 22 '22 06:09

merlin2011


That insertion of A B C D rings a bell. You seem to have the problem described in Fix broken arrow key navigation in insert mode and Fix arrow keys that display A B C D on remote shell, and possibly a workaround implemented.

That interferes with the :imaps to <Nop>.

like image 33
Ingo Karkat Avatar answered Sep 21 '22 06:09

Ingo Karkat