3

Is there a tool to convert indentations across all specified text files? From tabs to spaces, spaces to tabs, 2 spaces to 4 spaces, etc.? I mainly need it for JavaScript files.

I use Sublime Text, and the conversion can be done with that program on a per-file basis, but I want to do this in bulk for many files.

A simple search and replace has side effects. It replaces tabs/spaces everywhere, even those that are not for indentation purpose. I want it to affect indentation only.

A complex regex might be the solution but it doesn't work with any tools I've tried it in so far (grepWin, Notepad++, Sublime).

A command-line tool would be acceptable, as long as it works in Windows cmd or Git bash.

It should be gratis.

Thanks for the question @mguassa!

  • Do you need it for a particular programming language? Do you use an IDE? Would it be OK a command line tool? Does the program need to be gratis? It can all help to find a more targeted solution. – mguassa Jan 28 '16 at 9:13
3

I've tried different solutions, from regular expressions in Sublime Text to dedicated JavaScript beautifiers such as js-beautify and UglifyJS2 (with the --beautify option). Regarding regex, it seemed too complicated to find an expression that could cover all the cases you mentioned, while for the other two tools, they look good, but even with specific options, they changed more than just indentation and needed some extra work for bulk processing.

In the end, the tool I suggest to use is Artistic Style (a.k.a. AStyle):

Artistic Style is a source code indenter, formatter, and beautifier for the C, C++, C++/CLI, Objective‑C, C# and Java programming languages.

It's a command line tool, free, open-source (here's the SourceForge page), cross-platform, well-documented and easy to setup and configure. It's not specific to JavaScript, but for what I could test it works well with that programming language too. The bulk processing is a feature of the program.

Here are some use cases based on your requirements:

From tabs to spaces: AStyle.exe C:\tmp\*.js --indent=spaces=2

From spaces to tabs: AStyle.exe C:\tmp\*.js --indent=force-tab

From 2 spaces to 4 spaces: AStyle.exe C:\tmp\*.js --indent=spaces=4

More options are available to:

  • prevent the creation of a backup file
  • run recursively in all subdirectories
  • perform a dry run
  • apply several code formattings, not just indentation
  • etc.

In addition, on the website, you can have a look at the page dedicated to ready-to-use scripts and the list of GUI programs/plugins that use AStyle (among them there's also a Sublime Text plugin, which I haven't tried but I presume it cannot process files in bulk).

  • What's the guarantee that after astyle has had a whack at your source code, that it it still works properly? If one is going to run this on hundreds of files, you don't want to find out the hard way. – Ira Baxter Apr 17 '16 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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