Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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).

share|improve this question

closed as off-topic by Snowman, MichaelT, GlenH7, Thomas Owens Feb 19 at 14:50

  • This question does not appear to be about software development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Related: How to replace tabs with spaces? at Vim SE –  kenorb Feb 19 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. –  Snowman Feb 19 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. –  MichaelT Feb 19 at 14:47
1  
Related questions at Vim.SE: Re-indenting badly indented code and How to replace tabs with spaces? –  Snowman Feb 19 at 23:40

3 Answers 3

up vote 6 down vote accepted

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:

:%retab

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.

share|improve this answer
2  
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. –  Spencer Rathbun Sep 23 '11 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
:%retab!
share|improve this answer

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:

gg
=G

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

share|improve this answer
    
-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. –  kevin cline Sep 24 '11 at 3:08

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