Vim: Replace tabs with spaces

When working in other peoples code I almost always notice mixed tabs and spaces. Like many programmers, this makes my skin crawl since it usually causes formatting to look horrible or incomprehensible.

First I'll double check that there are mixed tabs and spaces by highlighting the tabs.

If I see some whitespace highlighted and some not highlighted I'll look at the indentation and figure out how many spaces each tab should be, then do a replace. This will perform a replace on the entire file (%s) for all tabs (\t) into four spaces (\ \ \ \ ), the g at the end will allow the match to be done multiple times per line:
:%s/\t/\ \ \ \ /g

If your tab expand matches the file, you can also use the vim built-in retab:


Post a Comment