From 423c72e0cb1758ec0d18d1127c703849a5c718b7 Mon Sep 17 00:00:00 2001 From: Michael Fortunato Date: Tue, 18 Jun 2024 17:02:34 -0400 Subject: [PATCH] feat(autocmd): switch to terminal mode when entering a terminal buffer The mode is now switched to "terminal" when entering a terminal buffer. Before, if you entered a terminal buffer in normal mode, the mode would stay normal, requiring the user to manually switch to terminal mode. For example, if a LazyVim user has a vertically split window with a regular text file on the left hand side and a terminal buffer on the right, and the user moves focus from the file buffer to the terminal buffer via ``, they will now enter terminal mode immediately so they can use the shell, whereas before, the user stayed in Normal mode and had to enter terminal mode via `i` or an equivalent key. ## Performance For the autocmd implementation, I am concerned that pattern = "term://*" combined with "BufEnter" event will put strain on neovim. ## References It looks like addressed the issue in the neovim version 0.9 via a backport but it is still not working on my machine. --- lua/lazyvim/config/autocmds.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lua/lazyvim/config/autocmds.lua b/lua/lazyvim/config/autocmds.lua index 2fc283ba..197c35ba 100644 --- a/lua/lazyvim/config/autocmds.lua +++ b/lua/lazyvim/config/autocmds.lua @@ -113,3 +113,15 @@ vim.api.nvim_create_autocmd({ "BufWritePre" }, { vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p") end, }) + +-- Switch to terminal mode when entering a terminal buffer (term://) +-- NOTE: "BufEnter" is emitted when a user focuses to an _existing_ terminal buffer +-- "TermOpen" when a new terminal buffer is created eg. with `:term`. +-- Odd that "BufEnter" is not emitted for a "term://" buffer after `:term` +vim.api.nvim_create_autocmd({ "BufEnter", "TermOpen" }, { + callback = function() + vim.cmd("startinsert") + end, + pattern = { "term://*" }, + group = augroup("enter_term_mode_on_focus"), +})