2

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?

2 Answers 2

1

Another option you could do is set the settings you want in your init.lua unconditionally, and then create the file .after/ftplugin/help.lua that unsets those options. e.g.:

-- ~/.config/nvim/after/ftplugin/help.lua
vim.wo.cursorline = false
vim.wo.colorcolumn = ''
vim.wo.relativenumber = false
1
  • Interesting. I might give this a try.
    – Jethro Cao
    Oct 24 at 23:35
0

Ok, so I came up with a workaround, which I'm not entirely satisfied with, since it doesn't make my intentions as clear. But it does now behave as I want it to. The workaround is to wrap the vim.wo settings in the callback function in a conditional block that checks the filetype, like so:

vim.api.nvim_create_autocmd({ 'VimEnter', 'WinEnter', 'BufWinEnter'}, {
  callback = function()
    if vim.bo.filetype ~= 'help' then
      vim.wo.cursorline     = true
      vim.wo.colorcolumn    = '100'
      vim.wo.relativenumber = true
    end
  end,
  group = focused_window_group,
  pattern = '*',
})

This leaves the help text windows unaffected, i.e. they don't display relativenumber nor any number column, which how they are by default even if you were to set vim.o.number = true in your init.lua. Later on, shall I find other filetypes I'd like to omit these autocmd actions on, I could just add them into the conditional check.

Ideally, I'd still like to know if it's possible to add this constraint to the pattern key instead. So better answers are still welcome.

2
  • 1
    Vim’s autocommands don’t support sophisticated pattern contraints (:help {aupat}). Conditionals are the answer, and I don’t really see anything wrong with that?
    – D. Ben Knoble
    Sep 20 at 13:16
  • Great thanks for clarifying.
    – Jethro Cao
    Sep 20 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.