tags:

views:

95

answers:

5

I find that I now often work with code bases that have different amounts of indentation. Some use 2 spaces, some use 4 space, some even use tabs! Once in a while, I have to share code between these codebases, or, sometimes I use an incorrect amount of indentation by mistake. Is there a tool or a text editor feature that will convert between different amounts of indentation?

Update: I mostly code in Javascript, HTML, CSS and Python now days. I'd prefer some thing language agnostic, because I work with a few different languages.

+2  A: 

It depends on what you're using. In recent versions of Visual Studio, for instance, ctrl-k,ctrl-d will format the entire document you're working on.

A couple quick commands in Vim will convert all the tabs in a document to spaces.

At the command line, indent will reformat C source code.

Mark Rushakoff
@Mark: I didn't know about the kbd tag until viewing source behind your answer. Can you enter the tag directly in SO?
Eric J.
@Eric you sure can. [List of SO's allowed tags](http://meta.stackoverflow.com/questions/1777/what-html-tags-are-allowed)
Mark Rushakoff
A: 

You don't say which language your developing in but I've used jalopy for Java and perltidy for Perl before.

Both of these tools you can set indent amount and brace style etc.

dalton
+1  A: 

UltraEdit is a text editor that has some excellent search/replace capability, plus you can toggle the visibility of whitespace characters. It's easy to convert 4 spaces to a tab, and vice versa. However, it's not something you would probably want to convert files in often, it's more of a text editor on steroids than a tool for just tab/space refactoring purposes.

JYelton
A: 

Here are a bunch of language-specific reformatters.

Ira Baxter
A: 

For C, C++, C#, and Java code, you can use Artistic Style (astyle) to beautify/stylize the code according to your preferences. Tabs vs. spaces (as well as how many spaces) are one of many available options.

Michael Aaron Safyan