Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remap arrow keys vim

Tags:

vim

I'm trying to make my arrow keys in vim useless to get used to hjkl.

After adding a few lines to my .vimrc file everything worked but the insert mode remap.

nnoremap <Down> :echo "No down for you!"<CR>
vnoremap <Down> :<C-u>echo "No down for you!"<CR>
inoremap <Down> :<C-o> echo "No down for you!"<CR>

nnoremap <Up> :echo "No up for you!"<CR>
vnoremap <Up> :<C-u>echo "No up for you!"<CR>
inoremap <Up> :<C-o>echo "No up for you!"<CR>

nnoremap <Left> :echo "No left for you!"<CR>
vnoremap <Left> :<C-u>echo "No left for you!"<CR>
inoremap <Left> :<C-o>echo "No left for you!"<CR>

nnoremap <Right> :echo "No right for you!"<CR>
vnoremap <Right> :<C-u>echo "No right for you!"<CR>
inoremap <Right> :<C-o>echo "No Right for you!"<CR>

The problem is, every time one arrow key is pressed it inserts the following string into my file:

:echo "No **** for you!

like image 346
Luiz Guilherme Ribeiro Avatar asked Sep 08 '25 12:09

Luiz Guilherme Ribeiro


1 Answers

We have the vi.SE for Vim questions, it is always better to post directly there. Anyhow:

Sardorbek's answer is correct, mapping to <nop> is the right solution. i.e. this:

noremap <Up> <nop>
noremap <Down> <nop>
noremap <Left> <nop>
noremap <Right> <nop>

inoremap <Up> <nop>
inoremap <Down> <nop>
inoremap <Left> <nop>
inoremap <Right> <nop>

Yet, the reason that your inoremap was printing the lines into the file is because you did use <c-o> after : at the beginning of your mappings, therefore the normal mode command that was being run was e (not :echo). I believe that the lines you were seeing were actually:

:cho "No **** for you!

And not

:echo "No **** for you!

Moreover, <c-o> allows for only one normal mode command but you need two: echo the message, and negate the arrow key movement.

In essence, the following is horrible (really horrible, please do not do this) but would have worked:

inoremap <Down> <esc>:echo "No down for you!"<CR>ki
inoremap <Up> <esc>:echo "No up for you!"<CR>ji
inoremap <Left> <esc>:echo "No left for you!"<CR>li
inoremap <Right> <esc>:echo "No Right for you!"<CR>hi

In general, using <esc> is preferable to <c-o> and c-u (in visual mode) wherever possible.

like image 63
grochmal Avatar answered Sep 10 '25 04:09

grochmal