Using oil.nvim as a sidebar
Use <C-n>
to toggle the sidebar. The code assumes an oil window that's 30 wide is
a sidebar window. Sidebar windows have different keybindings than a regular oil window.
<CR>
opens the file in a different window, <C-v>
and <C-s>
opens it splitting the
window on the right vertically or horizontally respectively.
/home/vector/.config/nvim/lua/plugins/oil.lua
return {
"stevearc/oil.nvim",
dependencies = {
"nvim-tree/nvim-web-devicons",
},
config = function()
local oil = require("oil")
oil.setup({
buf_options = {
buflisted = true,
bufhidden = "hide",
},
keymaps = {
-- ["<C-s>"] = { "actions.select", opts = { horizontal = false, vertical = true } },
["<CR>"] = function()
local cur_win_nr = vim.fn.winnr()
local right_win_nr = vim.fn.winnr('l')
-- Check if the width is 20
local win_width = vim.api.nvim_win_get_width(0)
local fpath = oil.get_current_dir() .. oil.get_cursor_entry()["name"]
-- P({ fpath, vim.fn.isdirectory(fpath) })
if win_width == 30 and vim.fn.isdirectory(fpath) ~= 1 then
local buf = vim.fn.bufadd(fpath)
vim.fn.bufload(buf)
-- Check if there is a window to the right
if right_win_nr ~= cur_win_nr then
-- Get the buffer of the right window
local right_win_id = vim.fn.win_getid(right_win_nr)
vim.api.nvim_win_set_buf(right_win_id, buf)
end
else
require("oil.actions").select["callback"]()
end
end,
["<C-v>"] = function()
local cur_win_nr = vim.fn.winnr()
local right_win_nr = vim.fn.winnr('l')
-- If width of oil window is 20, we assume its the sidebar
local win_width = vim.api.nvim_win_get_width(0)
local fpath = oil.get_current_dir() .. oil.get_cursor_entry()["name"]
if win_width == 30 and vim.fn.isdirectory(fpath) ~= 1 then
-- If there is a window to the right, we will split it instead
if right_win_nr ~= cur_win_nr then
local right_win_id = vim.fn.win_getid(right_win_nr)
local buf = vim.fn.bufadd(fpath)
vim.api.nvim_open_win(buf, false, { split = 'left', win = right_win_id })
end
else
require("oil.actions").select["callback"]({ vertical = true, split = "topleft" })
end
end,
["<C-s>"] = function()
local cur_win_nr = vim.fn.winnr()
local right_win_nr = vim.fn.winnr('l')
-- If width of oil window is 20, we assume its the sidebar
local win_width = vim.api.nvim_win_get_width(0)
local fpath = oil.get_current_dir() .. oil.get_cursor_entry()["name"]
if win_width == 30 and vim.fn.isdirectory(fpath) ~= 1 then
-- If there is a window to the right, we will split it instead
if right_win_nr ~= cur_win_nr then
local right_win_id = vim.fn.win_getid(right_win_nr)
local buf = vim.fn.bufadd(fpath)
vim.api.nvim_open_win(buf, false, { split = 'below', win = right_win_id })
end
else
require("oil.actions").select["callback"]({ horizontal = true, split = "botright" })
end
end
}
})
vim.keymap.set("n", "-", ":Oil<CR>", { desc = "Open parent directory" })
vim.keymap.set('n', '<C-n>', function()
if vim.api.nvim_get_option_value('filetype', { buf = 0 }) ~= "oil"
then
-- Get the window to the left of the current window
local cur_win_nr = vim.fn.winnr()
local cur_win_id = vim.fn.win_getid(cur_win_nr)
local left_win_nr = vim.fn.winnr('h')
local left_win_id = vim.fn.win_getid(left_win_nr)
-- Check if there is a window to the left
if left_win_nr ~= cur_win_nr then
-- Get the buffer of the left window
local left_buf = vim.api.nvim_win_get_buf(left_win_id)
-- Get the file type of the buffer
local filetype = vim.api.nvim_get_option_value('filetype', { buf = left_buf })
-- Check if the file type is 'oil'
if filetype == 'oil' then
-- Close
vim.cmd(left_win_nr .. 'hide')
else
local sidewin = vim.api.nvim_open_win(0, false,
{ split = 'left', win = cur_win_id, width = 30, style = 'minimal' })
vim.fn.win_execute(sidewin, 'Oil', true)
vim.fn.win_execute(sidewin, 'set winfixwidth', true)
vim.fn.win_execute(sidewin, 'set winfixheight', true)
end
else
local sidewin = vim.api.nvim_open_win(0, false,
{ split = 'left', win = cur_win_id, width = 30, style = 'minimal' })
vim.fn.win_execute(sidewin, 'Oil', true)
vim.fn.win_execute(sidewin, 'set winfixwidth', true)
vim.fn.win_execute(sidewin, 'set winfixheight', true)
-- Open
end
else
vim.cmd("hide")
end
end
)
end,
}