Convert vi/vim tabs to spaces

vim tabs/spaces FAQ: How do I convert vi/vim tabs to spaces?

You can convert tabs to spaces in vim using the usual 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 (convert each tab to two spaces)
  • Do this globally 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 you really just want to convert the first tab on each line to spaces, just use the same command, but omit the 'g' at the end, like this:

:1,$s/\t/  /

That's all you need to convert tabs to spaces in vim.

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.