1

Is there a good macro or regex expression that I can use in Vim to generate a .h file from a .c file?

For example, take the following .c file

// temp.c
// comment 1
// comment 2
void function1(int a, int b) {
    int x = 0;
    if (x == 0) {
        x = 1;
    }
    else { x = 2; }
}

and generate the following .h file

// temp.h
// comment 1
// comment 2
void function1(int a, int b);

My current method involves copying the .c file, renaming it as a .h file, then just deleting lines until it makes a header file

2 Answers 2

2

If it's always in that format, you could do the following regex:

:%s/\v^\S.*\zs \{$\n(.*\n)*\}$/;

But this isn't a foolproof method because it relies on proper formatting. It's also pretty verbose and rather hard to understand. I would prefer a global command:

:g/^\S.*{$/norm $hc%;

This basically just says on any line where ^\S.*{$ matches (e.g. the start of a function) run the following command. The command to run is $hc%;, which in normal mode, will go to the end of the line, back one character, and change everything until the matching } with a ;.

1
  • 1
    Might want to add :keeppatterns so that the search register, "/, if this is going to be automated Feb 28, 2018 at 21:01
0

Many thanks to @DJMcMayhem for his answer. For anyone else who comes to this post, I changed the regex a little bit and stuck it in a function like the one below.

" convert a .c file to a .h file
fun! s:convertC2H()
    w %:r.h
    tabe %:r.h
    execute 'g/^\S.*{$/norm $h%s;'
endfun
command! ConvertC2H :call s:convertC2H()

I edited the function to now also include copying the .c file out to a .h file (with the same name except the file extension).

6
  • 1
    Why not add the bit that creates the .h file into your function, too: make it fully automated!
    – Rich
    Feb 28, 2018 at 22:31
  • 1
    Nice! I have more suggestions, though: You could replace the first line of your function with %y, which is a bit more concise, and doesn't require a mark. However, I'd probably replace the first three lines with the following two: 1. w %:r.h 2. tabe %:r.h. This also means your function doesn't affect any registers, and is possibly a bit simpler than using normal mode commands.
    – Rich
    Mar 1, 2018 at 10:01
  • Yeah it worked great! Could you tell me what the first command you suggested does?
    – J-Win
    Mar 1, 2018 at 14:21
  • 1
    Sure! It calls the ex command :yank with a range of %, i.e. It yanks the entire file. See help :yank and :help ranges.
    – Rich
    Mar 1, 2018 at 14:35
  • 1
    That line creates the header file. See :help :w. Although it occurs to me now that that's not the right place for it! It would be better to :write the file after the :global command. (Or even just manually after the :ConvertC2H command completes).
    – Rich
    Mar 1, 2018 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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