By Alvin Alexander. Last updated: February 27, 2018
vi/vim editor FAQ: How do I convert tabs to spaces in vim?
You can convert tabs to spaces in vim using the vim search and replace command. Specifically, this vim “tabs to spaces” command is all you need:
:1,$s/\t/ /g
In short, this command can be read like this:
- From the first line to the last line (
1,$
) - Swap each tab (
\t
) with two spaces (i.e., convert each tab to two spaces) - Do this globally (
g
) on each line, meaning that if there is more than one tab on any line, make sure you convert all of them.
Regarding that last line, if for some reason you just want to convert the first tab on each line to spaces, use the same command, but omit the g
at the end, like this:
:1,$s/\t/ /
In summary, that command is all you need to convert tabs to spaces in the vim editor