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<"
coc-prettier
if you installed it, which formats documents automatically. Or maybe putset noexpandtab
invim/after/ftplugin/rust.vim
.c.vim
,vim.vim
, for each filetype.:setlocal
, and maybe mentionb:undo_ftplugin
. Might even be a case for~/.vim/after/indent/rust.vim
, with correspondingb:undo_indent