f



Vim: Indent and wrap C comments

Hello all,

There is something in vim that has been bugging me for a while now.  I
want to reformat and indent a long C-style comment (/* */) while still
respecting the textwidth setting.  For example, say I have the following
text (my apologies for the long lines; I believe they are necessary to
frame my question):

int fn(int x, int y) {
    /* Determine whether the qwerty count exceeds the asdf threshold.  If it does, signal a uiop.  Otherwise, let the zxcv module schedule an hjkl 1 2 3 procedure. */
    return x > y;
}

I don't want any lines to exceed 79 columns, so the comment needs to be
split across multiple lines.  The way I do this is by selecting the
comment lines and typing "gq".  After doing this, I have the following:

int fn(int x, int y) {
    /* Determine whether the qwerty count exceeds the asdf threshold.  If it
 * does, signal a uiop.  Otherwise, let the zxcv module schedule an hjkl 1 2 3
 * procedure. */
    return x > y;
}

That's better, but I want the second and third comment lines be indented
like the first one is.  I can fix the indentation by selecting all the
comment lines and typing "=".  This results in:

int fn(int x, int y) {
    /* Determine whether the qwerty count exceeds the asdf threshold.  If it
     * does, signal a uiop.  Otherwise, let the zxcv module schedule an hjkl 1 2 3
     * procedure. */
    return x > y;
}

That's almost correct, but now the second comment line exceeds 79
columns!

Is there a good way to split the comment into multiple lines and add the
appropriate indentation without exceeding the textwidth?  I always end
up doing this by hand, but I always feel like there must be a better
way.

I thought this would be a common problem for vim users, but I wasn't
able to find anyone else asking the same question.  I apologize if this
has been asked and answered many times already.

Thanks,
Chris
0
Christopher
8/8/2016 12:08:58 AM
comp.editors 4871 articles. 0 followers. Post Follow

5 Replies
242 Views

Similar Articles

[PageSpeed] 1

with
<slrnnqfjcq.40v.ccollins476ad@pseudoephedrine.nightmare-heaven.no-ip.biz>
Christopher Collins wrote:

> There is something in vim that has been bugging me for a while now.  I
> want to reformat and indent a long C-style comment (/* */) while still
> respecting the textwidth setting.  For example, say I have the
> following text (my apologies for the long lines; I believe they are
> necessary to frame my question):

As usual, you have three options:

[1] Look for scripts that already do what you want.  Some of them might
    be pointy-clicky installable (or even already installed, I don't
    know what's an OS).  Long time ago I've discovered NERD-Commenter in
    near proximity.  But then an upgrade has came and things changed
    from good-engough to worse.  And I'm terribly low on upgrades.  So I
    don't recommend it, look for yourself.

[2] Now, vim has bindings!  You can roll your own.  I've tried vim-basic
    and failed miserably.  Today 'map' and registering user-defined
    funcitons and commands is my limit.  But I have perl to do things a
    way I want.

[3] How about not giving a fsck?  I mean, there might be more important
    things to do.

*CUT*

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom
0
Eric
8/8/2016 11:07:25 AM
On 2016-08-08, Eric Pozharski <whynot@pozharski.name> wrote:
> with
><slrnnqfjcq.40v.ccollins476ad@pseudoephedrine.nightmare-heaven.no-ip.biz>
> Christopher Collins wrote:
>
>> There is something in vim that has been bugging me for a while now.  I
>> want to reformat and indent a long C-style comment (/* */) while still
>> respecting the textwidth setting.  For example, say I have the
>> following text (my apologies for the long lines; I believe they are
>> necessary to frame my question):
>
> As usual, you have three options:
>
> [1] Look for scripts that already do what you want.  Some of them might
>     be pointy-clicky installable (or even already installed, I don't
>     know what's an OS).  Long time ago I've discovered NERD-Commenter in
>     near proximity.  But then an upgrade has came and things changed
>     from good-engough to worse.  And I'm terribly low on upgrades.  So I
>     don't recommend it, look for yourself.
>
> [2] Now, vim has bindings!  You can roll your own.  I've tried vim-basic
>     and failed miserably.  Today 'map' and registering user-defined
>     funcitons and commands is my limit.  But I have perl to do things a
>     way I want.
>
> [3] How about not giving a fsck?  I mean, there might be more important
>     things to do.

Those options apply generally to pretty much any vim issue.  Perhaps I
was wasn't explicit enough; I am looking for a solution that actually
applies to my specific problem :).

Chris
0
Christopher
8/8/2016 6:37:54 PM
In comp.editors, Christopher Collins  <ccollins476ad@gmail.com> wrote:
> Hello all,
> 
> There is something in vim that has been bugging me for a while now.  I
> want to reformat and indent a long C-style comment (/* */) while still
> respecting the textwidth setting.  For example, say I have the following
> text (my apologies for the long lines; I believe they are necessary to
> frame my question):
> 
> int fn(int x, int y) {
>     /* Determine whether the qwerty count exceeds the asdf threshold.  If it does, signal a
> uiop.  Otherwise, let the zxcv module schedule an hjkl 1 2 3 procedure. */
>     return x > y;
> }
> 
> I don't want any lines to exceed 79 columns, so the comment needs to be
> split across multiple lines.  The way I do this is by selecting the
> comment lines and typing "gq".  After doing this, I have the following:

My settings have it working fine right away, but I forget what I set for
it to just work. Select the comment lines and type "gq" (with tw=68) and
the output looks like this:

    /* Determine whether the qwerty count exceeds the asdf
     * threshold.  If it does, signal a uiop.  Otherwise, let the
     * zxcv module schedule an hjkl 1 2 3 procedure. */

Set textwidth to 35 and try again:

    /* Determine whether the qwerty
     * count exceeds the asdf
     * threshold.  If it does,
     * signal a uiop.  Otherwise,
     * let the zxcv module schedule
     * an hjkl 1 2 3 procedure. */

It doesn't look like I explicitly set formatoptions (value is "tcq" and
"c" is important here, see ":help fo-table"). It also looks like
"comments" is important, and (again), I appear to have the default
value of "s1:/*,mb:*,ex:*/,://,b:#,:%,:XCOMM,n:>,fb:-"

Here's what I see from :set in my test:

--- Options ---
  autoindent          keywordprg=dict     shiftwidth=2        ttymouse=sgr
  exrc                modelines=2         syntax=c            t_Co=
  filetype=c          modified            terse
  helplang=en         ruler               textwidth=35
  history=200         scroll=11           ttyfast
  backspace=indent,eol,start
  cpoptions=aABceFs%
  fileencodings=ucs-bom,utf-8,default,latin1
  matchpairs=(:),{:},[:],<:>
  pastetoggle=<Insert>
  printoptions=paper:letter
  runtimepath=/dev/null
  shortmess=filnxtToOs
  suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg
,.inx,.out,.toc

That "runtimepath" is how I am explicitly excluding all vim scripts that
shipped with my distro (Ubuntu). 

Elijah
------
pastetoggle is best new setting since ":set paste"
0
Eli
8/8/2016 11:40:51 PM
with
<slrnnqhkc2.j29.ccollins476ad@pseudoephedrine.nightmare-heaven.no-ip.biz>
Christopher Collins wrote:

*SKIP*
> Those options apply generally to pretty much any vim issue.

Point is it applies to anything.

> Perhaps I was wasn't explicit enough; I am looking for a solution that
> actually applies to my specific problem :).

And Eli made me thinking here.  You don't seem to be bitten by a
special-vim-magic, yet (or you'll state clean runtimepath upfront).  So
(I'm speculating here), do you have filetype plugin on?  Because I've
glanced upon ftplugin/c.vim, it might be what you need.

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom
0
Eric
8/9/2016 10:13:35 AM
On 2016-08-08, Eli the Bearded <*@eli.users.panix.com> wrote:
> My settings have it working fine right away, but I forget what I set for
> it to just work. Select the comment lines and type "gq" (with tw=68) and
> the output looks like this:
>
>     /* Determine whether the qwerty count exceeds the asdf
>      * threshold.  If it does, signal a uiop.  Otherwise, let the
>      * zxcv module schedule an hjkl 1 2 3 procedure. */
>
> Set textwidth to 35 and try again:
>
>     /* Determine whether the qwerty
>      * count exceeds the asdf
>      * threshold.  If it does,
>      * signal a uiop.  Otherwise,
>      * let the zxcv module schedule
>      * an hjkl 1 2 3 procedure. */
>
> It doesn't look like I explicitly set formatoptions (value is "tcq" and
> "c" is important here, see ":help fo-table"). It also looks like
> "comments" is important, and (again), I appear to have the default
> value of "s1:/*,mb:*,ex:*/,://,b:#,:%,:XCOMM,n:>,fb:-"
>
> Here's what I see from :set in my test:
>
> --- Options ---
>   autoindent          keywordprg=dict     shiftwidth=2        ttymouse=sgr
>   exrc                modelines=2         syntax=c            t_Co=
>   filetype=c          modified            terse
>   helplang=en         ruler               textwidth=35
>   history=200         scroll=11           ttyfast
>   backspace=indent,eol,start
>   cpoptions=aABceFs%
>   fileencodings=ucs-bom,utf-8,default,latin1
>   matchpairs=(:),{:},[:],<:>
>   pastetoggle=<Insert>
>   printoptions=paper:letter
>   runtimepath=/dev/null
>   shortmess=filnxtToOs
>   suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg
> ,.inx,.out,.toc

It appears "autoindent" is what makes it work.  I get the desired
behavior as well when I set that.  I don't particularly like
auto-indentation in general, but I think I'll give it another chance.

> That "runtimepath" is how I am explicitly excluding all vim scripts that
> shipped with my distro (Ubuntu). 

That's a nice trick.  I'll be borrowing that one!

> Elijah
> ------
> pastetoggle is best new setting since ":set paste"

So is that :).  Perhas autoindent isn't so bad afterall.

Thanks,
Chris
0
Christopher
8/9/2016 3:07:01 PM
Reply: