How to convert vi/vim tabs to spaces

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

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.