How to convert the amount of indentation in a source code file? [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
开发者_高级运维Closed last year.
Improve this questionI 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.
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.
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.
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.
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.
Here are a bunch of language-specific reformatters.
精彩评论