To remap Netrw's navigation keys to your Colemak layout (neio instead of hjkl), you can configure Neovim to override Netrw's key bindings specifically within the Netrw buffer. Here's how to do it:
Step-by-Step Solution:
- Add the following code to your Neovim configuration (e.g., in
lua/custom/after/plugin/netrw.lua or your init.lua):
-- Remap Netrw keys for Colemak layout (hjkl → neio)
vim.api.nvim_create_autocmd("FileType", {
pattern = "netrw",
callback = function()
-- Unmap default hjkl keys in Netrw buffer to avoid conflicts
vim.keymap.set("n", "h", "<Nop>", { buffer = true, silent = true })
vim.keymap.set("n", "j", "<Nop>", { buffer = true, silent = true })
vim.keymap.set("n", "k", "<Nop>", { buffer = true, silent = true })
vim.keymap.set("n", "l", "<Nop>", { buffer = true, silent = true })
-- Map new keys (neio) to Netrw's original functions
-- Using Netrw's internal Plug mappings for reliability
vim.keymap.set("n", "n", "<Plug>NetrwDirUp", { buffer = true }) -- Back (previously 'h')
vim.keymap.set("n", "o", "<Plug>NetrwEnter", { buffer = true }) -- Enter directory (previously 'l')
vim.keymap.set("n", "e", "<Plug>NetrwNextLine", { buffer = true }) -- Move down (previously 'j')
vim.keymap.set("n", "i", "<Plug>NetrwPriorLine", { buffer = true }) -- Move up (previously 'k')
end
})
Explanation:
FileType netrw Autocmd: Triggers when the Netrw file explorer opens.
- Unmapping Original Keys: Disables the default
hjkl keys in the Netrw buffer to prevent conflicts.
- New Key Mappings:
n (previously h): Go back/up a directory.
o (previously l): Enter a directory/file.
e (previously j): Move down in the file list.
i (previously k): Move up in the file list.
- Plug Mappings: Uses Netrw's internal
<Plug> commands for reliable functionality, ensuring compatibility with future updates.
Notes:
- This configuration is local to Netrw buffers, so your global key mappings (outside Netrw) remain unaffected.
- If you have other key remappings globally (e.g., for other plugins), this won’t interfere with them.
After Adding the Code:
- Save the configuration file.
- Restart Neovim or run
:source % in the configuration file to apply changes.
- Open Netrw with
:Explore and test the new keys neio.
Let me know if you need further adjustments! 😊