62
inoremap  <Up>     <NOP>
inoremap  <Down>   <NOP>
inoremap  <Left>   <NOP>
inoremap  <Right>  <NOP>
noremap   <Up>     <NOP>
noremap   <Down>   <NOP>
noremap   <Left>   <NOP>
noremap   <Right>  <NOP>

This is what I use to disable cursor navigation, to help me stick to hjkl :)

But it also disables the cursor on the command bar... normally the arrow keys let you cycle through the history

Is it possible to disable the cursor keys ONLY for navigation, and not for the history?

2
  • 5
    Thank you, this is the first time I get the answer from the question! May 20, 2013 at 17:41
  • In insert mode, my up and down keys still work. Does anyone else have this problem?
    – dylnmc
    Jun 11, 2015 at 6:18

7 Answers 7

34

Add the following in your .vimrc file:

" Disable Arrow keys in Normal mode
map <up> <nop>
map <down> <nop>
map <left> <nop>
map <right> <nop>

" Disable Arrow keys in Insert mode
imap <up> <nop>
imap <down> <nop>
imap <left> <nop>
imap <right> <nop>
0
26

You can cycle through the history using C-n and C-p (Ctrl+n and Ctrl+p, respectively).

3
  • 4
    This is about the correct solution. If you want to keep your hands on the home row, then you keep your hands there!
    – mike3996
    Apr 26, 2013 at 8:48
  • 1
    @misha I want to echo out "Stop being stupid" when using arrow keys along with disabling those keys. Can you tell me what do I need to put into .vimrc. I don't know VimScript. Nov 21, 2015 at 19:16
  • 1
    @AbhimanyuAryan you saw that talk, didn't you? :) anyway, the line you're looking for is something like: map <up> :echoerr "Stop being stupid"<CR> Mar 21, 2016 at 9:16
10

The code you have posted should not disable history navigation in command line mode, are you sure you don't have cnoremap <Up> <Nop> or noremap! <Up> <Nop> somewhere? Try verbose cmap <Up> it should show you whether <Up> key is redefined for command line mode.


If when saying «command bar» you meant command-line window, you could try the following:

nnoremap <expr> <Up> ((bufname("%") is# "[Command Line]")?("\<Up>"):(""))
3
  • Thank you for these responses...... both very usefull, and yes my history DOES actually work with the arrow keys - I had a conflicting issue that made it appear as if it didn't :)
    – user537339
    Mar 21, 2011 at 8:14
  • 1
    Alternatively, you could use the CmdWinEnter autocmd event to unmap arrow keys for the command-line window.
    – graywh
    Mar 21, 2011 at 20:45
  • cnoremap works to disable the up arrows in command line mode.. Thanks..
    – alpha_989
    Jul 5, 2018 at 17:29
7

For me, this works:

map <Left> <Nop>
map <Right> <Nop>
map <Up> <Nop>
map <Down> <Nop>

Taken from: https://github.com/garybernhardt/dotfiles/blob/master/.vimrc#L148

2

Use q: to open a split window of your command line. You can navigate within it normally, as it's a regular vim window using hjkl and the other usual vim motions, and hit enter to run the command under cursor.

Don't use the arrow keys to navigate in the command line history.

Incidentally, you can also access your search history using q/ or q?.

2

You may also consider remapping them to move between the split windows. This disables the arrow keys for directional movement inside the file but lets you move between the split windows.

noremap <up> <C-w><up>
noremap <down> <C-w><down>
noremap <left> <C-w><left>
noremap <right> <C-w><right> 
1
  • If one is a heavy user of windows, using arrows instead of the native ctlr+w j/h/k/l is gonna slow them down, imho.
    – Enlico
    May 15, 2020 at 17:24
0

Change noremap to nnoremap to apply the mappings to normal mode, otherwise they're global all-modes mappings.

1
  • 3
    -1: They are not all-modes. They are only normal, operator-pending, select and visual mode mappings.
    – ZyX
    Mar 20, 2011 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.