There are many questions + answers on defining your own indentation style within vim
for specific files. For example the default on Ubuntu for python
is set in ftype/python.vim
which can be overwritten with something of custom liking, with a statement in ~/.vimrc
:
aug python
au FileType python setlocal ts=3 sts=3 sw=3 noexpandtab
aug end
This is fine if I am writing my own code, but if I am editing someone else's files I would like to use their indentation style.
What's the way to automatically apply the existing indentation of a file in ~/.vimrc
and at the same time use my own indentation for new files?
Vim doesn't have built-in automatic indent detection/adjustment. vim-sleuth and YAIFA are two "install-and-forget" plugins (there are a few others) that work pretty well.
If you don't want a third-party plugin or don't want to write your own, you can try something like this:
command! -nargs=1 Spaces execute "setlocal shiftwidth=" . <args> . " softtabstop=" . <args> . " expandtab" | set shiftwidth? softtabstop? expandtab?
command! -nargs=1 Tabs execute "setlocal shiftwidth=" . <args> . " softtabstop=" . <args> . " noexpandtab" | set shiftwidth? softtabstop? expandtab?
That you can use like that:
:Space 4 " 4 spaces for indentation
:Tabs 3 " 3 chars-wide tabs for indentation