How can I disable completely diagnostics for a specific buffer?


I use null-ls combined to shellcheck to add LSP features in shell scripts. The problem is in nvim, a dot file like .env, which simply declare environment variables of the form VAR=value, is marked as an sh file, so null-ls naturally launches shellcheck on them, and it produces a great amount of wrong diagnostic errors, which are totally disturbing.

So I tried to disable the LSP client for buffer with a name matching .env pattern and to clear/hide thoses diagnostics. It fixes partially the issue. In my on_attach function, I have this:

local function on_attach(client, bufnr)
  local bufname = api.nvim_buf_get_name(bufnr)
  if string.match(bufname, '/?%.env') then
    vim.diagnostic.hide(nil, bufnr)
    vim.diagnostic.reset(nil, bufnr)

The problem is if I list the diagnostics with Telescope (lsp_document_diagnostics), I see they still exist and they still show up in floating window when I move the cursor in the buffer because I use this autocmd

    autocmd CursorHold * lua vim.diagnostic.open_float({focusable=false, scope="cursor"})

So how am I supposed to turn this diagnostics off completely ??

you can find my config here → dotfiles/init.lua at master · doums/dotfiles · GitHub

autocmd BufRead,BufNewFile .env lua vim.diagnostic.disable()

autocmd BufRead,BufNewFile .env lua vim.diagnostic.disable(<abuf>) works.