aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2023-09-08 17:57:37 +0800
committercrupest <crupest@outlook.com>2023-09-13 00:35:23 +0800
commite1cf1bfebeca544abb34e860d86a89900b74df09 (patch)
treef63eab18ea75e9abdc219aa0d1a22256785cdf05
parent653f9440d8002f6b290a8a107d5832cf395b9188 (diff)
downloadcrupest-e1cf1bfebeca544abb34e860d86a89900b74df09.tar.gz
crupest-e1cf1bfebeca544abb34e860d86a89900b74df09.tar.bz2
crupest-e1cf1bfebeca544abb34e860d86a89900b74df09.zip
Update nvim config.
-rw-r--r--configs/nvim/init.lua80
1 files changed, 79 insertions, 1 deletions
diff --git a/configs/nvim/init.lua b/configs/nvim/init.lua
index 1529495..1b8f0f2 100644
--- a/configs/nvim/init.lua
+++ b/configs/nvim/init.lua
@@ -19,7 +19,7 @@ vim.opt.shiftwidth = 4;
vim.opt.expandtab = true;
vim.opt.wrap = false;
vim.opt.number = true;
-vim.keymap.set('t', '<esc>', [[<C-\><C-n>]])
+vim.keymap.set('t', '<leader><esc>', [[<C-\><C-n>]])
if vim.g.neovide then
vim.opt.guifont = "FiraCode Nerd Font";
@@ -286,6 +286,84 @@ require("catppuccin").setup{
vim.cmd.colorscheme "catppuccin"
+-- custom keymaps
+
+vim.keymap.set("n", "<c-tab>", "<cmd>bnext<cr>")
+vim.keymap.set("n", "<c-s-tab>", "<cmd>bNext<cr>")
+vim.keymap.set("n", "<s-tab>", "<c-o>")
+
+local list_listed_bufs = function ()
+ local bufs = vim.api.nvim_list_bufs()
+ local result = {}
+ for _, v in ipairs(bufs) do
+ if vim.fn.buflisted(v) ~= 0 then
+ table.insert(result, v)
+ end
+ end
+ return result
+end
+
+local get_previous_buffer = function (buf)
+ local bufs = list_listed_bufs()
+
+ -- no buffers at all
+ if #bufs == 0 then return nil end
+
+ -- find the buf in bufs
+ local index = 0
+ for i, v in ipairs(bufs) do
+ if buf == v then
+ index = i
+ break
+ end
+ end
+
+ -- it's the only one
+ if #bufs == 1 and index == 1 then
+ return nil
+ end
+
+ -- it's the first one
+ if index == 1 then
+ return bufs[2]
+ end
+
+ return bufs[index - 1]
+end
+
+-- Delete current buffer and jump back.
+-- If no previous jump, switch to previous buffer.
+-- If no previous buffer (no other buffers), create a unnamed one. (So the window does not quit.)
+vim.keymap.set("n", "<c-q>", function ()
+ local current_buffer = vim.api.nvim_get_current_buf()
+ local jumps_info = vim.fn.getjumplist()
+
+ local old_jump_list = { unpack(jumps_info[1], 1, jumps_info[2]) }
+ while #old_jump_list ~= 0 do
+ local last_jump = old_jump_list[#old_jump_list]
+ if last_jump.bufnr ~= current_buffer and vim.fn.bufexists(last_jump.bufnr) ~= 0 and vim.fn.buflisted(last_jump.bufnr) ~= 0 then
+ break
+ end
+ table.remove(old_jump_list, #old_jump_list)
+ end
+
+ if #old_jump_list ~= 0 then
+ local last_jump = old_jump_list[#old_jump_list]
+ vim.api.nvim_win_set_buf(0, last_jump.bufnr)
+ vim.api.nvim_win_set_cursor(0, {last_jump.lnum, last_jump.col})
+ else
+ local previous_buf = get_previous_buffer(current_buffer)
+ if previous_buf then
+ vim.api.nvim_win_set_buf(0, previous_buf)
+ else
+ local new_buf = vim.api.nvim_create_buf(true, false)
+ vim.api.nvim_win_set_buf(0, new_buf)
+ end
+ end
+
+ vim.api.nvim_buf_delete(current_buffer, {})
+end)
+
vim.keymap.set("n", "<esc>", function ()
local wins = vim.api.nvim_list_wins()
for _, v in ipairs(wins) do