I'm adding autoformatting on write to my neovim config, but it's only working partially.

vim.api.nvim_create_autocmd('BufWritePre', {
    pattern = {"*.c", "*.cpp"},
    callback = function()
        print("formatter called")
            opts = { 
                tabSize = 4,
                trimTrailingWhitespace = true,
                insertSpaces = false

When hitting :w my c and c++ code is reformatted, but not according to the options in the opts table (it's still indented with tabs of 2 spaces which is the clangd default). Furthermore, there is nothing printed, which leads me to believe the callback is not... called back. How do I make the autoformatter format correctly?

I got my info about the vimlsp.buf.format function from the docs and the info about the options from what the docs reference.

1 Answer 1


For C++ the LSP format is relying on [cland-format] and it seems to me that it only consider the cland-format option there are no way to override it by the parameters given to the format method.

For Python the LSP format is relying on Black and only considers the global Black options.

Black as very limited number of formatting options by design:

  • line_length
  • skip_string_normalization
  • skip_magic_trailing_comma

In particular the tabSize, insertSpaces and trimTrailingWhiteSpace are not options considered by Black (see this issue for reference)

In general the number of specified formatting options in the format method are either quite limited compare to what the formatter can do or not matching. To avoid conflict I suppose the choice have been made to keep the formatter option and ignore the format formatting options argument.

Your Answer

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

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