Skip to content

Instantly share code, notes, and snippets.

@LaptopDev
Created October 14, 2025 01:47
Show Gist options
  • Select an option

  • Save LaptopDev/eebe422d8c43e6fd609a7ac68e95f9f5 to your computer and use it in GitHub Desktop.

Select an option

Save LaptopDev/eebe422d8c43e6fd609a7ac68e95f9f5 to your computer and use it in GitHub Desktop.
Explore the parent directory of a file in netrw and escape it easily
function OpenParentDirectory()
if vim.bo.filetype == "netrw" then
vim.cmd("bd!") -- close netrw if already open
return
end
local path = vim.fn.expand("%:p:h")
if path == "" then return end
vim.cmd("Ex " .. vim.fn.fnameescape(path))
local buf = vim.api.nvim_get_current_buf()
vim.defer_fn(function()
if vim.bo[buf].filetype == "netrw" then
vim.keymap.set("n", "<Esc>", "<cmd>bd!<CR>", { buffer = buf, silent = true })
end
end, 30)
end
vim.keymap.set({ "n", "v" }, "<leader>e", OpenParentDirectory, { desc = "Toggle netrw in parent dir" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment