Files
nvim-config/lua/config/keymaps.lua
T
2026-10-12 10:02:14 +08:00

67 lines
2.0 KiB
Lua

-- Keymaps are automatically loaded on the VeryLazy event
-- Default keymaps that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/keymaps.lua
-- Add any additional keymaps here
-- -- F6:选择当前标签页中可见的窗格
local function choose_window()
local api = vim.api
local current = api.nvim_get_current_win()
local items = {}
for _, win in ipairs(api.nvim_tabpage_list_wins(0)) do
local config = api.nvim_win_get_config(win)
if config.focusable ~= false and not config.hide then
local buf = api.nvim_win_get_buf(win)
local ft = vim.bo[buf].filetype
local name = api.nvim_buf_get_name(buf)
local label
if vim.bo[buf].buftype == "terminal" then
label = "终端"
elseif ft == "neo-tree" or ft == "NvimTree" then
label = "文件树"
elseif name ~= "" then
label = vim.fn.fnamemodify(name, ":~:.")
else
label = ft ~= "" and ft or "未命名文件"
end
items[#items + 1] = {
win = win,
label = string.format("%s [%d] %s", win == current and "●" or " ", api.nvim_win_get_number(win), label),
}
end
end
vim.ui.select(items, {
prompt = "切换到窗格",
format_item = function(item)
return item.label
end,
}, function(item)
if not item then
return
end
-- 等选择窗口关闭、恢复焦点后,再进入目标窗格
vim.schedule(function()
if api.nvim_win_is_valid(item.win) then
api.nvim_set_current_win(item.win)
local buf = api.nvim_win_get_buf(item.win)
if vim.bo[buf].buftype == "terminal" then
vim.cmd.startinsert()
end
end
end)
end)
end
vim.api.nvim_create_user_command("ChooseWindow", choose_window, { force = true })
local opts = { desc = "选择窗格", silent = true }
vim.keymap.set("n", "<F6>", "<Cmd>ChooseWindow<CR>", opts)
vim.keymap.set("i", "<F6>", "<Esc><Cmd>ChooseWindow<CR>", opts)
vim.keymap.set("t", "<F6>", "<C-\\><C-n><Cmd>ChooseWindow<CR>", opts)