Update: exit insert/visual/terminal modes with qq

This commit is contained in:
psychhim
2025-11-06 01:02:29 +05:30
parent 74e40cc7ae
commit 92695176d2

View File

@@ -687,40 +687,21 @@ vim.keymap.set('n', 'gf', function()
smart_open_file(path)
end, { desc = 'Smart gf: open file under cursor in new tab or reuse buffer' })
-- [[ Remap double Tab to exit insert/visual/command/terminal modes ]]
local last_tab_time = 0
local double_tab_timeout = 400 -- milliseconds
local tab_timer = nil
local function tab_double_escape()
local now = vim.loop.hrtime() / 1e6 -- current time in ms
if tab_timer then
tab_timer:stop()
tab_timer:close()
tab_timer = nil
local mode = vim.api.nvim_get_mode().mode
if mode:match '[iIcR]' then
return vim.api.nvim_replace_termcodes('<Esc>', true, false, true)
elseif mode:match '[vV\x16]' then
return vim.api.nvim_replace_termcodes('<Esc>', true, false, true)
elseif mode:match 't' then
return vim.api.nvim_replace_termcodes('<C-\\><C-n>', true, false, true)
else
return '' -- normal mode
end
-- [[ Remap double q to exit insert/visual/terminal modes ]]
vim.o.timeoutlen = 300 -- wait 300ms for second q
-- handler
local function double_q_exit(mode)
if mode == 't' then
-- send <C-\><C-n> to exit terminal mode
return vim.api.nvim_replace_termcodes('<C-\\><C-n>', true, false, true)
else
-- first Tab press → start a timer to send a normal Tab if no second Tab
tab_timer = vim.loop.new_timer()
tab_timer:start(
double_tab_timeout,
0,
vim.schedule_wrap(function()
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Tab>', true, false, true), 'n', false)
tab_timer:stop()
tab_timer:close()
tab_timer = nil
end)
)
return '' -- do nothing
-- send <Esc> to exit insert/visual modes
return vim.api.nvim_replace_termcodes('<Esc>', true, false, true)
end
end
vim.keymap.set({ 'i', 'v', 't' }, '<Tab>', tab_double_escape, { noremap = true, expr = true, silent = true })
vim.keymap.set({ 'i', 'v' }, 'qq', function()
return double_q_exit 'i'
end, { expr = true, noremap = true })
vim.keymap.set('t', 'qq', function()
return double_q_exit 't'
end, { expr = true, noremap = true })