Friday, September 28, 2012

Re: Changing C comments

On Friday, September 28, 2012 3:01:03 PM UTC-5, Bob von Knobloch wrote:
> On 28/09/12 20:39, Ben Fritz wrote:
>
> <snip>
>
> > If this is too complicated (regular expressions are a powerful tool but can be hard to use; "now you have two problems" and all that) then you can try out a commenting plugin. I've heard good things about "NERD Commenter", which appears to have a "switch comment style" command:
>
> >
>
> > http://www.vim.org/scripts/script.php?script_id=1218
>
> >
>
> > For some reason I cannot currently find any others which seem regularly maintained. I thought I remembered seeing a wide variety of them at some point.
>
> >
>
>
>
> Ben,
>
> your regex works like a charm.
>
> It replaces all occurrences in the file, not just 1 line, as you thought
>

I meant, it will work for this:

/* this is a single-line C-style comment */

But not this:

/* this is
* a multi-line
* C-style comment
*/

> and it leaves 1 space at the end of each converted comment (not a
> problem to globally remove).
>

Oops. Here's a tweak to not include the trailing whitespace within the comment:

%s#\v/\*(%(%(\*/)@!.){-})\s*\*/\s*$#//\1

(untested)

As you can see, you can easily spend WAY to long tweaking these.

Here I'm using a "non-greedy" regular expression, see :help /\{-

> Now I need to align them. The NERD Commenter didn't work well for me.
>
> Pity, it looked as though it could solve my aligning problem.
>
> Now I need a K&R style to "braces horizontally aligned" brace conversion :=)
>

I'd understand your alignment problem better if you gave an example. But probably it's not something you should use regex to accomplish. There is an "Align" plugin by Dr. Chip but since I don't really understand your problem I'm not sure if it's useful:

http://vim.sourceforge.net/scripts/script.php?script_id=294

--
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