Autocommand: callback function with if statement

I am trying to create an auto command that works for all filetypes but markdown. I know that vim.bo.filetype gives me the filetype, so I have created this:

-- https://stackoverflow.com/a/60338380/2571881
-- autocmd WinEnter,BufEnter * if &ft != "md" | do things
vim.api.nvim_create_autocmd("ModeChanged", {
    pattern = { "*:i*", "i*:*" },
    group = group,
    callback = function()
        if not vim.bo.filetype == "markdown" then
            vim.o.relativenumber = vim.v.event.new_mode:match("^i") == nil
        end
    end,
})

It is not working properly for markdown, it keeps changing something on this filetype. If I remove the if it will work for all filetypes but I have this on my ~/.config/nvim/after/markdown.lua:

vim.opt_local.number = false
vim.opt_local.relativenumber = false

That’s why I am trying to use an if statement inside my callback function

May have found the problem:
(a is vim.bo.filetype and b is "markdown")
not a == b always returns false independent of filetype.
This is because Lua evaluates not a == b as (not a) == b and not not(a == b).
I would recommend replacing not a == b with a ~= b, but you can also replace it with not(a == b)

1 Like