Comments (8)
This happens because the TermOpen autocmd fires when a terminal is opened, regardless of if it's the current window or not. You can demonstrate this by sourcing this file:
-- test.lua :source % will enter insert mode
local buf = async.api.nvim_create_buf(false, true)
local chan = async.api.nvim_open_term(buf, {})
Since that is exactly what output.open() is doing, it's going to hit your autocmd and startinsert
.
You can fix this on your end by adding a guard (optionally with a delay). My config looks like this:
vim.api.nvim_create_autocmd("TermOpen", {
desc = "Auto enter insert mode when opening a terminal",
pattern = "*",
callback = function()
-- Wait briefly just in case we immediately switch out of the buffer
vim.defer_fn(function()
if vim.api.nvim_buf_get_option(0, 'buftype') == 'terminal' then
vim.cmd([[startinsert]])
end
end, 100)
end,
})
On the neotest side, you may want to do something to work around this. We expect this autocmd to be relatively popular because it's literally in the neovim help docs (see :help :terminal
):
To enter |Terminal-mode| automatically:
autocmd TermOpen * startinsert
One possibility (that is still a gross hack to be sure) is the code below.
local mode = vim.api.nvim_get_mode().mode
-- Open terminal here
vim.defer_fn(function()
local new_mode = vim.api.nvim_get_mode().mode
if new_mode ~= mode then
if string.find(new_mode, "i") == 1 then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<ESC>", true, true, true), "n", false)
-- Optionally restore visual selection if we came from visual mode (is this common?)
if string.find(mode, "v") == 1 or string.find(mode, "V") == 1 then
vim.cmd([[normal! gv]])
end
end
end
end, 10)
from neotest.
Ah yes the TermOpen command is not affectted by the nouatocmd
setting. I'm not really a fan of introducing a hacky solution. This is not unique to neotest (I use it in nvim-dap-ui, nvim-dap also uses it), it's much easier to fix the autocmd. I'll leave it up to users to adjust their config.
I'd recommend changing the autocmd to something like this
autocmd TermOpen * lua if vim.startswith(vim.api.nvim_buf_get_name(0), "term://") then vim.cmd("startinsert") end
which should then behave how you want it to
from neotest.
Thanks for the comments. I thought this is something that can be addressed by the plugin, but I agree that user's custom autocmd config is more responsible for the behavior. The startinsert
autocmd were implictly assuming the terminal is opened in the current window (or focus is gained), but this assumption is not necessarily true in some cases.
I'd recommend changing the autocmd to something like this
autocmd TermOpen * lua if vim.startswith(vim.api.nvim_buf_get_name(0), "term://") then vim.cmd("startinsert") end
which should then behave how you want it to
Can confirm this works as expected. Or a non-lua version if someone would care the backward compatibility:
autocmd TermOpen * if nvim_buf_get_name(0) =~# '^term://.*' | startinsert | endif
from neotest.
I'm not seeing the same behaviour. Do you have an autocmd to enter insert mode when a terminal buffer is opened or any thing like that? It might also be something in my setup that is hiding the issue.
I've set noatucomd
on the window settings, can you try with the latest commit?
from neotest.
Exactly -- you've got a right hunch.
This is the autocmd I have; disabling this would make the bug not happen.
" Automatically enter insert mode when entering neovim terminal buffer
augroup terminal_autoinsert
autocmd!
autocmd BufWinEnter,WinEnter * if &buftype == 'terminal' | startinsert | endif
autocmd TermOpen * startinsert
augroup END
However the latest commit (after fcc2337) still doesn't fix the issue. You could probably reproduce the bug with the autocmd settings above. What's strange is the startinsert option is supposed to work on the terminal buffer, but it affects the editor window.
from neotest.
Perfect I'll close this so 👍 Perhaps an update to neovim docs would be good, will open a PR when I get a chance if no else has
from neotest.
I just ran into this with a TermOpen autocommand as well that looks like this:
-- start terminal in insert mode
vim.api.nvim_create_autocmd("TermOpen", {
group = "custom_buffer",
pattern = "*",
command = "startinsert | set winfixheight"
})
I implemented what @stevearc suggested as a workaround. Is there a more preferred solution?
from neotest.
This seems to be working for me, do you see any issues with it?
local autocmd = vim.api.nvim_create_autocmd -- Create autocommand
-- Enter insert mode when switching to terminal
autocmd("TermOpen", {
command = "setlocal listchars= nonumber norelativenumber nocursorline",
})
autocmd("TermOpen", {
pattern = "term://*",
command = "startinsert",
})
-- Close terminal buffer on process exit
autocmd("BufLeave", {
pattern = "term://*",
command = "stopinsert",
})
from neotest.
Related Issues (20)
- Feature Request: Set Notification Levels
- [BUG] Getting error "Future already set" and output_panel is empty for python tests HOT 7
- [Feature Request] Boost.Test supporting HOT 2
- Deprecation warnings on neovim 0.11
- [BUG] Summary opens, but no help HOT 2
- Graciously handling DAP failure on multistep (build then run) languages HOT 1
- [doc] Replace README recommendation of neodev with lazydev
- New Adapter: neotest-ctest - A neotest adapter for C/C++ using CTest as a test runner HOT 1
- [BUG] ImportError while importing test module HOT 3
- [BUG] "No tests found" when opening the summary window rigth before running tests the first time HOT 1
- [BUG] Java file watcher does not work as the jdtls is marked as an invalid lsp HOT 1
- Lazy load adapters HOT 1
- [Question] Subprocess with `--clean` flag
- [Feature Request] Enable setting custom environment variables for summary runs HOT 2
- [Feature] option to hide the adapter name
- [Documentation] RunSpec stream clarification HOT 2
- [BUG] scm-1 rockspec still has nvim-treesitter in its dependencies HOT 1
- [BUG] CMD Neotest Summary freezes Neovim HOT 1
- [BUG] Async call does not work inside is_test_file function
- [BUG] Slow starup time HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from neotest.