Restore your easier window navigation mappings on Netrw buffers in Vim

If you are like me and have added the below mappings into your .vimrc file as mappings to window commands meant for navigation...

" Easier window navigation.
map <C-h> <C-w>h
map <C-j> <C-w>j
map <C-k> <C-w>k
map <C-l> <C-w>l

... you may have figured out that they do not work on Netrw buffers.

Below is the way to fix that...

First off you need the following function:

function NetrwMappings()
	nnoremap <buffer> <C-h> :wincmd h<cr>
	nnoremap <buffer> <C-j> :wincmd j<cr>
	nnoremap <buffer> <C-k> :wincmd k<cr>
	nnoremap <buffer> <C-l> :wincmd l<cr>

And finally the following auto commands group that lets you set these remaps when on netrw buffer.

augroup netrw_mappings
  autocmd filetype netrw call NetrwMappings()
augroup END

Add these two last code blocks to your .vimrc then re-source it and or restart your Vim and enjoy!


Answer on a Stack Overflow's thread