Translating this excellent Vimscript answer for creating an autocmd that applies certain options only to the focused window into Lua, I have the following:
local focused_window_group = vim.api.nvim_create_augroup('CursorLine', { clear = true })
vim.api.nvim_create_autocmd({ 'VimEnter', 'WinEnter', 'BufWinEnter'}, {
callback = function()
vim.wo.cursorline = true
vim.wo.colorcolumn = '100'
vim.wo.relativenumber = true
end,
group = focused_window_group,
pattern = '*',
})
Which works as I expect. With the cursorline
, colorcolumn
and relativenumber
only applying to my focused window split. However an unintended side effect of this was that now Vim help texts also have these options activated whenever I enter them. And when I leave the help window, its relativenumber
column disappears, which jarringly ends up shifting the text in the help window. Therefore I'd like to be able to exclude buffers with filetype=help
from my above autocmd.
Using this Vimscript answer as an example, I tried to augment the pattern
key in the passed options table as:
pattern = {'*', vim.bo.filetype ~= "help"}
But when reloading this change, I get the error: Invalid 'pattern' item: expected String, got Boolean
when starting Neovim. This makes sense since the documentation for vim.api.nvim_create_autocmd()
says the value for the pattern
key is (string|array) optional: pattern(s) to match literally |autocmd-pattern|.
Wrapping the filetype conditional check in single quotes suppresses the Invalid 'pattern' item
error, but does not remove help files from activating the autocmd.
How can I achieve this?