0

So I want to disable the arrow keys in insert mode. So I added the following lines at the end of ~/.vimrc:

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

Now, every time I open a file the following effect happens: The left and right arrow are disabled in insert mode, like they should. The up and down arrows are still working though. Only after :source ~/.vimrc the up and down arrows are disabled.

Why does this happen? Why aren't the up and down keys disabled automatically right after vim starts?

2

Fileypte plugins, etc., can silently overwrite mappings you've defined in your vimrc. :map <unique> exists to prevent this, but not all plugins bother.

In the general case you can prevent your mappings being overwritten by defining mappings in an autocommand, such that your mappings are set after those defined by a plugin:

fun! FinalMappings()
    inoremap <buffer> <up> <nop>
    inoremap <buffer> <down> <nop>
    inoremap <buffer> <left> <nop>
    inoremap <buffer> <right> <nop>
endfun

augroup FinalMappings
    au!
    au VimEnter,BufNewFile,BufReadPost,FileReadPost,BufFilePost,
            \BufEnter,BufWinEnter,BufNew,FileType,WinEnter,
            \WinLeave * call FinalMappings()
augroup END

That's probably overkill with the autocommand events. Note that this needs to go after the line in your vimrc that sets up the autocommands for filetype detection, e.g. filetype plugin indent on, since autocommands are executed in the order in which they are registered.

This isn't foolproof, but should work 99% of the time.

1

Thanks everybody.

With the command :verbose :imap <up> and :verbose :imap <down> if found out, that both arrows are used by YouCompleteMe.

The default keys for selecting a suggestion are tab and up, and for selecting a previous suggestion are s-tab and down. Since I only use tab and s-tab anyways, I was able to set the selection keys to only tab and s-tab with:

let g:ycm_key_list_select_completion = ['<TAB>']
let g:ycm_key_list_previous_completion = ['<S-TAB>']

Now the disabling of the arrow keys works just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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