Is there any tool I can use to convert tabs with 2 spaces to 4 spaces?

It'll be great if it also can change code formatting like positions of { } and strip unwanted spaces (eg. > 2 empty lines)

link|improve this question

73% accept rate
2  
Not subjective at all. – Craige Jan 12 '11 at 4:25
Most code editor now support the function to change the tab indent size. You could google to see how it be done. To me, I use IntelliJ and it works well. – Hoàng Long Jan 12 '11 at 4:48
IS this a move to Stackoverflow. – Loki Astari Jan 12 '11 at 5:00
1  
I'd actually have to ask why you want to do this in the first place. I've found over the years that indenting 4 spaces leads to code creeping too far over the screen so it becomes difficult to read. (For plain C, anyhow). All you need is a couple of ifs and a for loop... indenting 2 spaces has been my "sweet spot" for some time now. I assume you are not trying to change hard tab characters - a different and hideous beast. – quickly_now Jan 12 '11 at 8:09
After afew comments and some considering I think 2 spaces is good. I was actually thinking at first that seems like 4 spaces is the "default" way to do tab in most IDE's by default – Jiew Meng Jan 12 '11 at 10:18
show 2 more comments
feedback

migrated from programmers.stackexchange.com May 18 '11 at 17:24

This question came from our site for professional programmers interested in conceptual questions about software development.

3 Answers

There's a devious sequence of operations in vim and relatives that will convert from '2 space' indenting to '4 space' indenting.

First, set tabstops to 2, shiftwidth to 2, and ensure expandtab is off:

:set ts=2 sw=2 noet

Now, from the top of the file, shift the file one unit right, then one unit left. This converts the blanks into tabs:

:1
>G<G

Verify that you have tabs where you expect them.

Now set tabstops to 4, shiftwidth to 4, and expand tab back on:

:set ts=4 sw=4 et

And repeat the shifting:

:1
>G<G

This avoids reformatting anything except the leading space on each line. You could encode it all in a single (complex) mapped instruction:

:map v :set ts=2 sw=2 noet^V^M:1^V^M>G<G:set ts=4 sw=4 et^V^M>G<G

If you are formatting C code, or C++, you may be better of with one of the specialized formatting programs - the classic one is (GNU) indent, but there are many others, including astyle. These can be controlled to deal with the indentation too - so you wouldn't have to go through the machinations.

Trailing space is trivial:

:g/[ ^I][ ^I]*$/s///

However, I do it so often (other people's code) that I have a script or program called stb (strip trailing blanks) to do the job. It works well for me; it is not strictly necessary, but is much less typing than the (trivial?) regex above. The script version is a sed script:

sed 's/[ ^I][ ^I]*$//' "$@"
link|improve this answer
feedback

I believe what you are looking for is astyle. I'm fairly sure that others exist, but that is the one I have seen used most for what you are trying to do. It also integrates well with a number of IDEs, in the off chance that your IDE does not support such things in and of itself.

link|improve this answer
feedback

If you want to convert 2 spaces to 4 spaces, you can use a sed trick like this. The pattern matches all the leading white space, and then doubles it.

$ sed 's/^\(\s*\)/\1\1/g' test.txt
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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