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)
|
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
| |||||||||||||||
feedback
|
This question came from our site for professional programmers interested in conceptual questions about software development.
|
There's a devious sequence of operations in First, set tabstops to 2, shiftwidth to 2, and ensure expandtab is off:
Now, from the top of the file, shift the file one unit right, then one unit left. This converts the blanks into tabs:
Verify that you have tabs where you expect them. Now set tabstops to 4, shiftwidth to 4, and expand tab back on:
And repeat the shifting:
This avoids reformatting anything except the leading space on each line. You could encode it all in a single (complex) mapped instruction:
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) Trailing space is trivial:
However, I do it so often (other people's code) that I have a script or program called
| |||
|
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. | |||
|
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.
| |||
|
feedback
|