Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to hack the c-vim plugin because I don't like the way it inserts comments. I want this:

/* comment */

but it gives me this:

/* comment
 */

I'm sure it has its reasons, but I don't like it. So I'm digging through the c.vim file and I've found this:

function! C_CodeComment( mode, style )
   if a:mode=="a"
     if a:style == 'yes'
       silent exe ":s#^#/\* #"
       silent put = ' */'
     else
       silent exe ":s#^#//#"
     endif
   endif
   if a:mode=="v"
     if a:style == 'yes'
       silent exe ":'<,'>s/^/ \* /"
       silent exe ":'< s'^ '\/'"
       silent exe ":'>"
       silent put = ' */'
     else
       silent exe ":'<,'>s#^#//#"
     endif
   endif
 endfunction

I have not been able to find any documentation on the put = statement, which I'm pretty sure is the one I have to change in order to get vim to append */ to the last line of the comment. Can anyone point me in the right direction, please?

share|improve this question
    
I think you'd have better luck dropping the put lines and changing the s... (substitute) lines to include the */ –  frabjous Aug 18 '11 at 2:19

1 Answer 1

The function C_CodeComment is not defined in any of the files that come with recent versions of Vim. I searched a bit and it looks like you are using Fritz Mehner’s C/C++ plugin. It provides a C/C++ > Comments > code -> comment /* */ menu item (and a \c* mapping1) that uses C_CodeComment.

The command put = ' */' appends a new line (see :help :put) that contains the contents of the special register =, which just evaluates whatever comes after it as a Vim expression. The particular expression used here is just a simple three character string (space, asterisk, forward-slash).

You can arrange for */ to be at the end of the last line2 by adding a line like

silent normal kJ^

after the silent put = ' */' lines. It moves up to last commented line (k), joins it with the */ line (J), and then moves the cursor to first non-blank character of the line (^); the \co mapping1 itself includes a j to move the cursor to next line after the newly commented region.

However, doing this will break the assumptions of the C_RemoveCComment function (which is used by the C_CommentCode “inverse” function): it expects to find and process */ at the beginning of the line (after optional whitespace).
If you never use the C/C++ > Comments > comment -> code menu item or the \co mapping1, then you may be able to live without the ability to use this plugin’s uncomment feature.


1 The \ “leader” of the mappings might be different if you have redefined localmapleader.
2 This (in my opinion) makes for odd looking multiline comments (i.e. selecting more than one line via GUI selection or visual mode before invoking the commenting functionality):

With the middle two lines selected,

    foo();
    bar();
    baz();
    quux();

becomes

    foo();
/*     bar();
 *     baz(); */
    quux();

which seems like an odd style (typically, if * leaders are used on intervening lines, then the asterisks from the /* opener, the * leaders, and the */ ender are put in the same column).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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