The first I need to do is related to C++ headers:
a lot of them look has lines like this
<some white space>void<some white space>AFunction();
where the <some whitespace> is just some whitespace.
that I need to convert to a more fixed and defined format:
<two tabs>void<8 tabs>AFunction(); or
<8 spaces>void<32 spaces>AFuntion();
I wonder how do I create a substitute expression for the above?
cheers,
Totte
:%s/\v^\s+([a-zA-Z_* ]+)\s+(\*?)(\s*\w+\(.*\);)$/\t\t\1\2\t\t\t\t\t\t\t\t\3/
It's perhaps a little more complex than it needs to be, because it
handles cases where you might have something like this:
int *my_function();
where the '*' is considered part of the return type, not the function
name, and you would presumably want it separated from the function name
when you convert the whitespace.
There may be corner cases in your header files that I didn't think of,
so if this doesn't apply to any of the lines that you need converted,
then you may need to tweak it. Or you can post the problematic lines
here and we can give you additional help.
mtkVirtualHole* GetVirtualBond(const int& i);
void DeleteVirtualBond();
where desired result is
mtkVirtualHole* GetVirtualBond(const int& i);
void DeleteVirtualBond();
(used fewer tabs here for simplicity)
-totte
Sure, anything is possible if you write a complicated enough expression!
:)
:%s/\v^\s+([a-zA-Z_* %]+)\s+(\*?)(\s*\w+\(.*\);)$/\="\t\t".submatch(1).submatch(2).repeat(' ',49-(2*&ts+len(submatch(1))+len(submatch(2)))).submatch(3)
This puts 2 tabs at the beginning of the line, followed by the return
type, then pads with spaces out to column 49, then finishes with the
function name and argument list.
If you plan on reusing this command with any regularity, it would
probably make sense to define a custom command so you can easily repeat
it. Because let's face it, the above is not something you really want to
type more than once!
If you add the following lines to your .vimrc, you can then just use
`:FormatPrototypes` to perform this operation in the current buffer:
command! FormatPrototypes
\ %substitute/\v^\s+([a-zA-Z_* ]+)\s+(\*?)(\s*\w+\(.*\);)$/\=
\ "\t\t" . submatch(1) . submatch(2) .
\ repeat(' ', 49 - (2 * &tabstop + len(submatch(1)) + len(submatch(2)))) .
\ submatch(3)