1

I would like to fix only spacing in badly formatted C/C++ source code. The Vim command = fixes only indentation. Utility indent is not suitable, because it reformats totally all source code, but want to minimize changes (for example avoid to break lines, minimize noise for diff and SCM).

For example, fix

 for( i=1; i<10; i++ ){

to

for (i = 1; i < 10; i++) {
  • "Fix spacing" means nothing. Or everything. It all depends on what rules you want to apply and how bad the situation is. – romainl Feb 1 '16 at 10:52
  • Your question is far too vague to be meaningful. What exactly do you mean with "fix only spacing"? And what sort of changes do you want to "minimize"? You should edit your question and include some examples of badly formatted code and how you would like it to look. – Martin Tournoij Feb 1 '16 at 11:02
  • Sounds like you want to do something very similar to this video: vimeo.com/15443936 You're massaging one text input into a similar text output, and want to be able to do this repeatedly for different files. Start with a bunch of global search/replace commands, use the :history command to save these commands into a function, run the function over other files. – fruglemonkey Feb 1 '16 at 23:08
2

This function almost does the job. Comment on the right side shows result

function! Fix_spacing()
    %s/\<\(if\|for\|while\|switch\)(\s*/\1 (/ge     " if (
    %s/\<\(else\){/\1 {/ge                          " else {
    %s/}\(else\)/} \1/ge                            " } else
    %s/ *){ */) {/ge                                " ) {
    %s/\((\) \(\S\)/\1\2/ge                         " no space after (
    %s/\n\n\n\+/\r\r/ge                             " single empty line
    %s/\([,;]\)\(\S\)/\1 \2/ge                      " space after ,;
    %s/\(\S\) \([,;)]\)/\1\2/ge                     " no space before ,;)
    %s/\s*$//ge                                     " no terminal space
    %s/^\([^ ^\t].*\){/\1\r{/ge                     " { on new line for functions
endfunction

This code inserts and deletes spaces around some keywords, brackets, comma, semicolon, terminal space and some line fixes.

| improve this answer | |
  • At least "space after ,;" will break that when used as punctuation in strings and comments, no? Also, using a space after a comma is typically considered to aid in readability, but I guess that's a personal opinion... ;-) Other regexes may have similar unwanted side-effects, so be sure to check the result... – Martin Tournoij Feb 1 '16 at 12:12
  • You are right. Need to check the results. At least this function makes ugly code readable. Who knows, how to avoid substitution within strings? – Costa Feb 1 '16 at 12:20
  • Using regexes for these sort of complicates fixes on a large codebase are, IMHO, the wrong tool for the job. It's okay for a simple fix, but you're probably better off using a "real" C/C++ parser which can fix it according to a set of rules. A number of those tools are listed here. – Martin Tournoij Feb 1 '16 at 12:24
  • Thank you. Real parses (I tested indent and astyle) reworks the code totally. My goal to fix ugly style with minimal changes and commit it. I want to preserve braces (Almann or K&R) inside functions. – Costa Feb 1 '16 at 12:37
  • Pretty sure you can configure those tools to use whatever brace style you prefer. – Martin Tournoij Feb 1 '16 at 12:39

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.