Dealing with code indentation in Vim?
-
28-09-2019 - |
Question
I work on an engineering team of 4 people, mostly writing javascript while occasionally dabbling in ruby and python. So, as a team, we share code all the time, and as most programmers do, each member of the team has his favorite level of indentation & related settings. I am one of 2 members of the team who use and love Vim as my primary code editor. I love my team but I also love my indentation, which happens to use 4-space tab characters. For more context, here's what I use in my .vimrc:
set ts = 4 sts = 4 sw = 4 expandtab " 4 space tabs
With so much code-sharing and collaborative editing going on in the team, the main code files usually start to appear as a mass of mixed tab & space mayhem, so that even the classic Vim trick of selecting all and pressing =
to smart indent doesn't have much effect.
Anyway, my question is this: In Vim (MacVim specifically) is there a better (more reliable) way of converting a code file from messy, mixed indentation to my preferred indentation? Whether it be a .vimrc setting or a command I enter while editing the file, I don't care.
Thanks for any suggestions in advance!
Solution
Use :retab
.
Having said that, I strongly suggest that you, as a team, agree on and use an indentation style when working collaboratively on a certain project.
OTHER TIPS
We have the same case of using javascript and ruby in the same shop.
autocmd FileType * set tabstop=4|set shiftwidth=4
autocmd FileType ruby set tabstop=2|set shiftwidth=2
set expandtab
I find I like 4 spaces for javascript but ruby looks much better with just two spaces.
I do agree with Yaser, you need to set the standard (spaces FTW)
Once you decided to get rid of all the tab chars use grep to find the files to :retab
grep -P '\t' * -R -c