127 lines
3.7 KiB
Lua
127 lines
3.7 KiB
Lua
local map = vim.keymap.set
|
|
local last_file_buf
|
|
local previous_file_buf
|
|
|
|
local function is_real_file_buffer(buf)
|
|
return vim.api.nvim_buf_is_valid(buf)
|
|
and vim.bo[buf].buflisted
|
|
and vim.bo[buf].buftype == ''
|
|
and vim.bo[buf].filetype ~= 'netrw'
|
|
and vim.api.nvim_buf_get_name(buf) ~= ''
|
|
end
|
|
|
|
local function is_netrw_buffer(buf)
|
|
if not vim.api.nvim_buf_is_valid(buf) then
|
|
return false
|
|
end
|
|
|
|
return vim.bo[buf].filetype == 'netrw' or vim.api.nvim_buf_get_name(buf):match('NetrwTreeListing$') ~= nil
|
|
end
|
|
|
|
local function switch_to_buffer(buf)
|
|
vim.cmd('buffer ' .. buf)
|
|
end
|
|
|
|
local function switch_to_native_alternate()
|
|
pcall(vim.cmd, 'buffer #')
|
|
end
|
|
|
|
local function jump_to_alternate_file()
|
|
local alternate = vim.fn.bufnr('#')
|
|
|
|
if is_real_file_buffer(alternate) then
|
|
switch_to_buffer(alternate)
|
|
return
|
|
end
|
|
|
|
if is_netrw_buffer(alternate) and is_real_file_buffer(previous_file_buf) then
|
|
switch_to_buffer(previous_file_buf)
|
|
return
|
|
end
|
|
|
|
switch_to_native_alternate()
|
|
end
|
|
|
|
-- Netrw configuration for file browsing
|
|
-- Native Neovim file explorer (no plugins required)
|
|
|
|
-- Hide the banner (that annoying help text at the top)
|
|
vim.g.netrw_banner = 0
|
|
|
|
-- Tree view by default (3 = tree style)
|
|
-- Options: 0=thin, 1=long, 2=wide, 3=tree
|
|
vim.g.netrw_liststyle = 3
|
|
|
|
-- Preview in horizontal split (underneath)
|
|
vim.g.netrw_preview = 0
|
|
|
|
-- Open preview splits below (1=below, 0=above)
|
|
vim.g.netrw_alto = 0
|
|
|
|
-- Window size for preview splits (percentage for preview window)
|
|
-- 50% split when pressing 'p'
|
|
vim.g.netrw_winsize = 50
|
|
|
|
-- Keep working directory unchanged when browsing (1 = don't change directory)
|
|
-- Setting to 1 prevents netrw from changing vim's working directory
|
|
vim.g.netrw_keepdir = 1
|
|
|
|
-- Open files in the same window (replace netrw buffer)
|
|
-- Options: 0=same window, 1=horizontal split, 2=vertical split, 3=new tab, 4=previous window
|
|
-- Using 0 so Enter opens file in netrw window itself
|
|
vim.g.netrw_browse_split = 0
|
|
|
|
-- Sort order: name (empty), time (t), size (s), exten (e)
|
|
vim.g.netrw_sort_by = 'name'
|
|
|
|
-- Hide dotfiles by default (toggle with 'gh' in netrw)
|
|
vim.g.netrw_list_hide = [[^\.\.\=/\=$]]
|
|
|
|
-- Use human-readable file sizes
|
|
vim.g.netrw_sizestyle = 'H'
|
|
|
|
-- Netrw file explorer keymaps
|
|
-- Open netrw in new tab at current file's directory
|
|
map('n', '<leader>nt', function()
|
|
local current_file_dir = vim.fn.expand('%:p:h')
|
|
vim.cmd('tabnew')
|
|
vim.cmd('Explore ' .. vim.fn.fnameescape(current_file_dir))
|
|
end, { desc = 'Netrw: Tab at current file dir', silent = true })
|
|
|
|
-- Open netrw in new tab at project root (stored at startup)
|
|
map('n', '<leader>nT', function()
|
|
vim.cmd('tabnew')
|
|
vim.cmd('Explore ' .. vim.fn.fnameescape(vim.g.project_root))
|
|
end, { desc = 'Netrw: Tab at project root', silent = true })
|
|
|
|
-- Open netrw in current window at project root (stored at startup)
|
|
map('n', '<leader>nr', function()
|
|
vim.cmd('Explore ' .. vim.fn.fnameescape(vim.g.project_root))
|
|
end, { desc = 'Netrw: Open at project root', silent = true })
|
|
|
|
map('n', '<C-6>', jump_to_alternate_file, { desc = 'Buffer: Alternate file skipping netrw', silent = true })
|
|
map('n', '<C-^>', jump_to_alternate_file, { desc = 'Buffer: Alternate file skipping netrw', silent = true })
|
|
|
|
vim.api.nvim_create_autocmd('BufEnter', {
|
|
desc = 'Track recent real file buffers for alternate-buffer jumps',
|
|
callback = function(args)
|
|
local buf = args.buf
|
|
|
|
if is_real_file_buffer(buf) and buf ~= last_file_buf then
|
|
previous_file_buf = last_file_buf
|
|
last_file_buf = buf
|
|
end
|
|
end,
|
|
})
|
|
|
|
-- Enable line numbers in netrw buffers
|
|
vim.api.nvim_create_autocmd('FileType', {
|
|
pattern = 'netrw',
|
|
desc = 'Configure netrw buffer-local options',
|
|
callback = function()
|
|
vim.bo.buflisted = false
|
|
vim.opt_local.number = true
|
|
vim.opt_local.relativenumber = true
|
|
end,
|
|
})
|