And then to automatically re-indent after that? I've tried copying code from one terminal screen to the other, but the tabulation got all messed up.

I didn't know what to call this functionality, so it was hard for me to find it on google (which generally returned things related to how to set tab size, which wasn't what I was looking for unfortunately).

  • Related: How to replace tabs with spaces? at Vim SE
    – kenorb
    Feb 19, 2015 at 14:04
  • 1
    I'm voting to close this question as off-topic because it is about a programming tool. Questions about tools are better suited to StackOverflow, or in this case, Vi.SE. However, this question is too old to migrate.
    – user22815
    Feb 19, 2015 at 14:47
  • 1
    I'm voting to close this question as off-topic because it is about the use of a specific tool and should have been asked on Stack Overflow or (if asked now) vi.se.
    – user40980
    Feb 19, 2015 at 14:47
  • 1
    Related questions at Vim.SE: Re-indenting badly indented code and How to replace tabs with spaces?
    – user22815
    Feb 19, 2015 at 23:40

3 Answers 3


Vim has a retab command that allows you to tabify or untabify your document. It uses settings such as tabstop and expandtab. So, if you want to replace tabs with spaces you could do the following:


See the retab documentation for full details.

Caveat Emptor: this also replaces tabs that are NOT at the beginning of a line. So if you use hard tabs in the middle of strings they will be replaced as well.

  • 3
    In general, it's considered a bad idea to include a true tab in a string anyway. It can still happen, but \t is preferred, just to avoid these kinds of issues. Sep 23, 2011 at 1:18

To expand on @Kaleb's answer (I don't have the rep yet to comment), before executing the retab command, you will need to :set expandtab (set et). Otherwise the retab will not replace tabs with spaces.

In additon, you may wish to specify the tabstops. Often in source code created by others, the code is written with tabstops set to 4 or even 2. Before doing the :%retab command, experiment with different tabstops. Try

:set ts=8 (the default)
:set ts=4 
:set ts=2  

to see which setting makes the most sense.

To go backwards (replacing spaces with appropriate TAB characters, use

:set noet

By un-tab, do you mean to change tabs to spaces? If so, substitute spaces for tabs in the entire buffer like this:

:%s/^I/    /g

That's four spaces on the right side of the substitution, but you can make it whatever number you want. To get the literal ^I tab character, you can hit ^V followed by the Tab key.

You can re-indent with the = command. To re-indent the entire contents of the buffer, use these commands:


This takes you to the top, then re-indents everything to the bottom.

  • -1: Replacing a tab with a fixed number of spaces is almost never useful. It will change the appearance of any line containing a tab preceded by a non-tab. Sep 24, 2011 at 3:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.