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 `<C-l>`,
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 <https://github.com/neovim/neovim/pull/25820> addressed
the issue in the neovim version 0.9 via a backport but it is still not
working on my machine.
This commit is contained in:
Michael Fortunato 2024-06-18 17:02:34 -04:00 committed by Michael Fortunato
parent dde4a9dcdf
commit 423c72e0cb

View file

@ -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"),
})