1

I don't use arrow keys, but to avoid the cursor moving should I hit them by mistake, I have this in my vimrc:

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

I also don't like the idea of hitting ZZ or ZQ in normal mode by mistake, so I also have

nnoremap ZZ          <NOP>
nnoremap ZQ          <NOP>

And what about :help opening if I mistakenly hith F1?

noremap  <F1>        <NOP>
lnoremap <F1>        <NOP>
tnoremap <F1>        <NOP>

Eventually I ended up adding the following too

nnoremap <CR>        <NOP>

but soon discovered that was not a good idea, as I couldn't use Enter in the command window anymore. So I added

augroup NormalModeCarriageReturnIsNoOpExceptInCmdwin
  autocmd!
  autocmd CmdwinEnter * nunmap <CR>
  autocmd CmdwinLeave * nnoremap <CR> <NOP>
augroup END

but doing some code navigation I've also noticed another place where I need Enter to keep its original meaning is in quickfix windows. However there's no such event as QuickfixWinEnter/QuickfixWinLeave.

So the question is: how can I deactivate Enter only when its effect would be the same as j (well, not exactly the same, but I don't care about their little difference)?

1 Answer 1

1

Using an expression mapping is probably the easiest here:

:nnoremap <expr> <CR> &ft == 'qf' \|\| getcmdwintype() != '' ? '<CR>' : ''

If the filetype is qf or getcmdwintype() is not empty then the current context is the quickfix window or commandline window, so we return a <CR>, and if it's anything else we return an empty string which is identical to <NOP>.

Your Answer

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

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