install whichkey.lua
This commit is contained in:
@@ -62,6 +62,15 @@ create_cmd({"BufRead", "BufNewFile"}, {
|
||||
end,
|
||||
})
|
||||
|
||||
-- disable list chars in specific filetypes, e.g. netrw
|
||||
create_cmd({"FileType"}, {
|
||||
desc = "Disable list chars for some filetypes",
|
||||
pattern = { "netrw" },
|
||||
callback = function(args)
|
||||
vim.opt_local.list = false
|
||||
end,
|
||||
})
|
||||
|
||||
-- automatically remove trailing whitespace on save file
|
||||
create_cmd({"BufWritePre"}, {
|
||||
desc = "Remove trailing whitespace when saving",
|
||||
|
||||
@@ -36,3 +36,67 @@ map("t", "<Esc><Esc>", "<C-\\><C-n>", { desc = "exit terminanl" })
|
||||
|
||||
-- Pressing ESC removes search highlights
|
||||
map("n", "<Esc>", "<Cmd>nohlsearch<Cr>")
|
||||
|
||||
-- Smart buffer switch (to alternative, previous, or next) if possible
|
||||
local function smart_buffer_switch(current)
|
||||
local alternative = vim.fn.bufnr("#")
|
||||
|
||||
-- get all listed buffers
|
||||
local buffers = vim.fn.getbufinfo({ buflisted = 1 })
|
||||
|
||||
-- find index of the current buffer
|
||||
local current_index = nil
|
||||
for index, buffer in ipairs(buffers) do
|
||||
if buffer.bufnr == current then
|
||||
current_index = i
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
-- try to switch to alternate buffer
|
||||
if alternative > 0 and vim.fn.buflisted(alternative) == 1 and alternative ~= current then
|
||||
vim.api.nvim_set_current_buf(alternative)
|
||||
-- try to switch to previous buffer
|
||||
elseif current_index and current_index > 1 then
|
||||
vim.api.nvim_set_current_buf(buffers[current_index -1].bufnr)
|
||||
-- try to switch to next buffer
|
||||
elseif current_index and current_index < #buffers then
|
||||
vim.api.nvim_set_current_buf(buffers[current_index + 1].bufnr)
|
||||
end
|
||||
end
|
||||
|
||||
local function smart_buffer_delete()
|
||||
local current = vim.api.nvim_get_current_buf()
|
||||
smart_buffer_switch(current)
|
||||
-- delete current buffer
|
||||
vim.cmd(":confirm bdelete " .. current)
|
||||
end
|
||||
map("n", "<leader>bd", smart_buffer_delete, { desc = "Close Current Buffer" })
|
||||
|
||||
local function smart_buffer_wipeout()
|
||||
local current = vim.api.nvim_get_current_buf()
|
||||
smart_buffer_switch(current)
|
||||
-- delete current buffer
|
||||
vim.cmd(":confirm bwipeout " .. current)
|
||||
end
|
||||
map("n", "<leader>bw", smart_buffer_wipeout, { desc = "Wipeout Current Buffer" })
|
||||
|
||||
local function delete_other_buffers()
|
||||
local current = vim.api.nvim_get_current_buf()
|
||||
for _, buffer in ipairs(vim.api.nvim_list_bufs()) do
|
||||
if buffer ~= current and vim.bo[buffer].buflisted and not vim.bo[buffer].modified then
|
||||
vim.api.nvim_buf_delete(buffer, {})
|
||||
end
|
||||
end
|
||||
end
|
||||
map("n", "<leader>bD", delete_other_buffers, { desc = "Close All Other Buffers" })
|
||||
|
||||
local function wipeout_other_buffers()
|
||||
local current = vim.api.nvim_get_current_buf()
|
||||
for _, buffer in ipairs(vim.api.nvim_list_bufs()) do
|
||||
if buffer ~= current and vim.bo[buffer].buflisted and not vim.bo[buffer].modified then
|
||||
vim.cmd("bwipeout " .. buffer)
|
||||
end
|
||||
end
|
||||
end
|
||||
map("n", "<leader>bW", wipeout_other_buffers, { desc = "Wipeout All Other Buffers" })
|
||||
|
||||
Reference in New Issue
Block a user