Recently I started using and learning Vim. I turned off movement keys in my .vimrc file like this:

noremap <Up> <NOP>
noremap <Down> <NOP>
noremap <Left> <NOP>
noremap <Right> <NOP>
noremap h <NOP>
noremap j <NOP>
noremap k <NOP>
noremap l <NOP>

However, I'd like to use netrw file browser, but I can't navigate in it without movement keys (h, j, k, and l, or arrow keys), since I disabled them. Is there a way to allow movement keys in netrw (basically, allow movement only when a cursor in it)?

  • 1
    I would recommend a different approach, keep h, j, k, and l. They are small movement keys, but often necessary – Peter Rincker Mar 2 '18 at 15:21
  • This might be sidestepping the question a little bit, but why have disabled all of those? I can't really see much benefit in removing a fundamental feature. – DJMcMayhem Mar 2 '18 at 18:43
  • I don't understand, what do you use for movement if all your movement keys are locked? – klaus Mar 3 '18 at 3:39
  • @EnanAjmain I'm guessing: f, t, F, T, w, b, W, B, e, E, ge, gE, /, +, -, G, H, M, L, |, etc. See also: vim hard-mode. ...although, all those commands also work in netrw, so ¯\_(ツ)_/¯ – Rich Mar 5 '18 at 16:03

My real answer is "don't do that", but if you insist, you could do something like:

noremap <expr> h &filetype ==? 'netrw' ? 'h' : ''
noremap <expr> j &filetype ==? 'netrw' ? 'j' : ''
noremap <expr> k &filetype ==? 'netrw' ? 'k' : ''
noremap <expr> l &filetype ==? 'netrw' ? 'l' : ''

These commands create mappings that check the filetype, and then either perform an hjkl movement or do nothing, accordingly.

See :help :map-expression for details on how this works.


Since you said you're learning vim, I'd point you to learn your way through autocmd. Take a look at this.

I myself am pretty beginner at vimming, so if the solution doesn't work don't think you couldn't pull it off, it's probably because my solution is wrong. But try this anyway.

Put these lines in your .vimrc :

augroup FiletypeSpecificMappings
    autocmd FileType netrw noremap <buffer> j j
    autocmd FileType netrw noremap <buffer> k k
    autocmd FileType netrw noremap <buffer> h h
    autocmd FileType netrw noremap <buffer> l l
augroup END

Here if netrw is opened its local buffer will have the above key mappings, not any other buffers. I searched for ways to compress these four lines into one but couldn't find any.

  • Rather use unmap – Christian Brabandt Mar 3 '18 at 7:30
  • Yeah, like I said, I don't know much about vim myself. You can put in another answer maybe? – klaus Mar 3 '18 at 7:50
  • @ChristianBrabandt You can't do a buffer-local unmap, so that wouldn't really work, I think. – Rich Mar 5 '18 at 16:20

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.