How do I force-convert all indentation including tabs in the middle of lines throughout an entire file?
Answer
:%retab!
Explanation
The :retab command converts leading whitespace according to the current tabstop and expandtab settings, but it only touches indentation at the start of lines. The ! flag extends this to ALL tab characters anywhere in the file — including those used for column alignment in the middle of a line.
How it works
:retab— converts leading whitespace (tabs→spaces or spaces→tabs based onexpandtab):retab!— converts ALL whitespace that contains tabs, including inline tabs%— applies to the whole file- The new whitespace uses the current
tabstopvalue (settabstopfirst if needed)
Example
A file with tabs used for column alignment:
Name\tAge\tCity
Alice\t30\tLondon
Bob\t25\tParis
After :set expandtab tabstop=4 | :%retab!:
Name Age City
Alice 30 London
Bob 25 Paris
With plain :retab (no !), the tabs in the middle of these lines would be left unchanged.
Tips
- Always set
tabstopandexpandtab(ornoexpandtab) before running:%retab! - To convert spaces back to tabs:
:set noexpandtab tabstop=4 | :%retab! - Use
:'<,'>retab!to apply only to a visual selection - Be cautious with source files — inline tabs in strings or heredocs may be intentional