Saturday, October 29, 2011

Re: Formatting C++ files

Totte Karlsson, Sat 2011-10-29 @ 14:26:45-0700:
> That is great! It "kind" of works. One question. Would it be possible
> instead of all the tabs, position the "last" part on say position 49
> on the line? This because, sometimes the first token(s) is really
> long, causing something like the following:

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)

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment