0

I have set up my init.vim file to always use tabs as indentation by adding this to it:

set tabstop=4
set shiftwidth=4
set noexpandtab

Ever since I installed CoC to use Rust's language server, it indents with four spaces instead of tabs, overriding my init.vim. Whenever I open a file I have to run set noexpandtab, and if I have forgotten to do this I have to also run %retab! to fix any indentations that were made with spaces. I tried moving my tab configuration in init.vim to its very end thinking that this was probably some code in CoC that runs when the program starts, so I would override it with my config, but this didn't work.

How can I stop this?

4
  • 1
    Might be coc-prettier if you installed it, which formats documents automatically. Or maybe put set noexpandtab in vim/after/ftplugin/rust.vim.
    – Biggybi
    Jun 29 at 20:42
  • This worked! Is there a way to set this for every type of file, not just rust files? Also, do you mind posting this as an answer so i can set it as the accepted answer?
    – Katie And
    Jun 29 at 21:33
  • I'll do that soon as I can! You can add as many files as you need there, e.g c.vim, vim.vim, for each filetype.
    – Biggybi
    Jun 30 at 12:22
  • 1
    @Biggybi don't forget :setlocal, and maybe mention b:undo_ftplugin. Might even be a case for ~/.vim/after/indent/rust.vim, with corresponding b:undo_indent
    – D. Ben Knoble
    Jun 30 at 18:23

1 Answer 1

1

Vim loads multiple files as described in :h :runtimepath.

In ftplugin/, you can add files like rust.vim where you define options for this filetype only. You may need to put the config in after/ftplugin to overwrite options set by other config files.

Considering this, a basic approach for your problem is to drop everything you rust-specific config in after/ftplugin/rust.vim.

However, you can go further and use (after/)indent/, (after/)compiler/ and so on, to organize your config even more.

Note that the options you set are not automatically restored. This can be a problem when the buffer's filetype changes: an option set for the first filetype will be preserved if it's not set for the second.

To avoid this problem, it's recommended to set the b:undo_ftplugin variable, to a value that resets each option you changed to its default. For an indent plugin, use b:undo_indent instead.

Putting it all together, you could have this in after/indent/rust.vim:

" set options locally
setlocal tabstop=4
setlocal shiftwidth=4
setlocal noexpandtab

" get a pre-existing `undo_indent` variable
let b:undo_ftplugin = get(b:, 'undo_indent', '')

" add a '|' to chain commands if `undo_indent` was set
if ! empty('b:undo_indent')
  let b:undo_indent .= ' | '
endif

" `set option< `restores `option` to its global value
" `.=` appends to the string
let b:undo_indent .=  "setlocal colorcolumn< foldmethod< suffixesadd< path<"
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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