Remapping keys in vim’s “directory view”

directoryvim

The "Netrw Directory Listing" you get by opening a directory in vim (e.g. :e .) seems to use a different set of keybindings than plain vim for navigation

I have my hjkl keys remapped thus:

noremap d h
noremap h gj
noremap t gk
noremap n l
noremap e d
noremap l n

But when I press 't' (aka arrow-up) in the directory listing, it actually opens the file under the cursor, instead of going up. How do I implement similar key mappings under this directory listing mode?

Best Answer

I'm afraid I don't understand.

[deleted some rambling I babbled on with before I figured out the root cause]

What are you trying to accomplish? Your noremap list there doesn't make sense to me anyways.

Aha! Got it!

You're using a DVORAK keyboard layout, aren't you? NOW the mappings begin to make sense. (Forgot to mention that.. didn't we!)

Looking at the source of the NETRW plugin, it looks like the netrw buffer is setting up BUFFER ONLY nnoremaps... which, of course, override yours. I'm looking for a solution.

From what I can tell, you'd have to do some major surgery to the $VIMRUNTIME/autoload/netrw.vim file, which would make upgrading a pain, and would spew netrw mappings all over your nice clean floor. But another idea struck me:

The netrw plugin sets the filetype to 'netrw' when it opens the buffer, so you could set up an autocommand in your .vimrc to set your needed key conversions as needed in the netrw window.

augroup netrw_dvorak_fix
    autocmd!
    autocmd filetype netrw call Fix_netrw_maps_for_dvorak()
augroup END
function! Fix_netrw_maps_for_dvorak()
    noremap <buffer> d h
    noremap <buffer> h gj
    noremap <buffer> t gk
    noremap <buffer> n l
    noremap <buffer> e d
    noremap <buffer> l n
    " and any others...
endfunction

Sort of has to be done like that since you can't concatenate map commands. Should work for you.

I suppose if you're using the dvorak noremap's throughout, then you could remove the <buffer> modifier.