From de488027227b2496faebc747ff39411cef144eac Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Thu, 7 Sep 2023 22:51:25 -0500 Subject: [PATCH] fix(nvim): ensure toggleterm exits when toggled --- .../nvim/lua/plugins/configs/toggleterm.lua | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/dots/.config/nvim/lua/plugins/configs/toggleterm.lua b/dots/.config/nvim/lua/plugins/configs/toggleterm.lua index 8511c7e0..766da489 100644 --- a/dots/.config/nvim/lua/plugins/configs/toggleterm.lua +++ b/dots/.config/nvim/lua/plugins/configs/toggleterm.lua @@ -2,7 +2,28 @@ return { { "akinsho/toggleterm.nvim", keys = { - { "", "ToggleTerm", desc = "ToggleTerm: Toggle" }, + { + "", + function() + local mode = vim.fn.mode(1) + local curr_buffer = vim.api.nvim_buf_get_name(0) + local toggleterm_match = function(buf_name) + return buf_name:find("toggleterm#", 1, true) + end + if toggleterm_match(curr_buffer) then + -- Try to exit with toggleterm + vim.cmd.ToggleTerm() + + -- We didn't successfully exit, use `q` to do so. + if toggleterm_match(vim.api.nvim_buf_get_name(0)) then + vim.cmd(":q") + end + else + vim.cmd.ToggleTerm() + end + end, + desc = "ToggleTerm: Toggle" + }, }, opts = { start_in_insert = false,