You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have some code I'd like to run when most buffers enter a window, so I create an autocmd for BufWinEnter. But there are some buffers on which I do not want to run this code. I can exclude most of these cases by checking the filetype or buftype. One case that I'd like to exclude is terminal buffers. However when a terminal buffer is created via :terminal and my BufWinEnter autocmd is executed the first time, the buftype is still empty. I've also checked other terminal buffer related variables: b:term_title, the channel local option and getwininfo(buf).[1].terminal, but these are also empty during the initial BufWinEnter. Note: they are present in subsequent BufWinEnters.
This might be fine if I wanted to avoid this code running for any buffer that has empty buftype, filetype, event.file, event.match (as terminal buffers do during initial BufWinEnter), but I do want this code to run on normal new buffers like with :enew (which have the same state).
My current hack is to run the code with vim.defer_fn() after x ms, after which buftype has usually then been set to 'terminal'.
Steps to reproduce:
Write the below code to a file 'test.lua'
nvim --clean test.lua
:source %
check the messages to see if buftype et al. have values initially and after the delay.
vim.api.nvim_create_autocmd({ 'BufWinEnter' }, {
group=vim.api.nvim_create_augroup('DetectTerminalBufWin', { clear=true }),
desc='Find a way to detect if this bufer is (or is going to be) a terminal buffer',
pattern='*',
callback=function(evt)
vim.print(
evt.event,
('filetype:%s'):format(vim.bo[evt.buf].filetype),
('buftype:%s'):format(vim.bo[evt.buf].buftype),
('b:term_title:%s'):format(vim.b.term_title),
('channel:%s'):format(vim.bo.channel),
('getwininfo.terminal:%s'):format(vim.fn.getwininfo(vim.api.nvim_get_current_win())[1].terminal)
)
ifevt.file=='' andevt.match=='' andvim.bo[evt.buf].filetype=='' andvim.bo[evt.buf].buftype=='' thenvim.defer_fn(
function()
vim.print(
'',
'defered',
('filetype:%s'):format(vim.bo[evt.buf].filetype),
('buftype:%s'):format(vim.bo[evt.buf].buftype),
('b:term_title:%s'):format(vim.b.term_title),
('channel:%s'):format(vim.bo.channel),
('getwininfo.terminal:%s'):format(vim.fn.getwininfo(vim.api.nvim_get_current_win())[1].terminal)
)
end,
200
)
endend,
})
vim.cmd.terminal()
System: Windows 11
nvim: v0.11.0
Expected behavior
Either buftype is set to 'terminal' prior to initial BufWinEnter autocmds or there is some other variable/mechanism to detect a terminal buffer at this time.
The text was updated successfully, but these errors were encountered:
stringTrimmer
changed the title
A way to detect weather a buffer is (or is going to be) a terminal buffer during a BufWinEnter autocmd
A way to detect whether a buffer is (or is going to be) a terminal buffer during a BufWinEnter autocmd
Jun 19, 2024
justinmk
changed the title
A way to detect whether a buffer is (or is going to be) a terminal buffer during a BufWinEnter autocmd
terminal buffer should set buftype=terminal before BufWinEnter autocmd
Jun 20, 2024
when a terminal buffer is created via :terminal and my BufWinEnter autocmd is executed the first time, the buftype is still empty. I've also checked other terminal buffer related variables: b:term_title, the channel local option and getwininfo(buf).[1].terminal, but these are also empty during the initial BufWinEnter
Surely the name is set? Terminal buffer name always starts with term:// (though it can be renamed later).
BufWinEnter is triggered by the :enew, not the termopen().
justinmk
changed the title
terminal buffer should set buftype=terminal before BufWinEnter autocmd
terminal :enew buffer should set buftype=terminal before BufWinEnter autocmd
Jun 20, 2024
Problem
I have some code I'd like to run when most buffers enter a window, so I create an
autocmd
forBufWinEnter
. But there are some buffers on which I do not want to run this code. I can exclude most of these cases by checking thefiletype
orbuftype
. One case that I'd like to exclude is terminal buffers. However when a terminal buffer is created via:terminal
and my BufWinEnter autocmd is executed the first time, thebuftype
is still empty. I've also checked other terminal buffer related variables:b:term_title
, thechannel
local option andgetwininfo(buf).[1].terminal
, but these are also empty during the initial BufWinEnter. Note: they are present in subsequent BufWinEnters.This might be fine if I wanted to avoid this code running for any buffer that has empty buftype, filetype, event.file, event.match (as terminal buffers do during initial BufWinEnter), but I do want this code to run on normal new buffers like with
:enew
(which have the same state).My current hack is to run the code with
vim.defer_fn()
after x ms, after which buftype has usually then been set to 'terminal'.Steps to reproduce:
System: Windows 11
nvim: v0.11.0
Expected behavior
Either
buftype
is set to 'terminal' prior to initial BufWinEnter autocmds or there is some other variable/mechanism to detect a terminal buffer at this time.The text was updated successfully, but these errors were encountered: