Question

What I want to do is to make a clean structure for adding comments in my file, like this :

/*********************
 * This is a comment *
 *********************/

I've managed to add the initial structure required to start commenting which is :

/**********
 * [TEXT] *
 **********/

using snipMate. But now, whenever I enter any text in the region [TEXT], for each character I enter, an asterisk should be added in the line above and below (hope you can get my point).

I think I can achieve this using the key mapping imap but not sure how to approach this. Even then, I think it will be a bit slow, so is there any way I can achieve this by vim programming? I just want someone to point me in the right direction.

If there is any plugin available for this, mention that too.

Était-ce utile?

La solution

Though the snipMate plugin provides mirroring (i.e. copying the typed contents of the current placeholder to another position), it has no ability to run transformations on it (and this is needed here: you want each typed character to be translated into an asterisk). This should be possible with the more modern UltiSnips - The ultimate snippet solution for python enabled Vim plugin.

Nonetheless, I would ask you to reconsider your approach to "clean" commenting. If your style requires special setup with a custom plugin in a particular editor, chances are that your colleagues / collaborators won't have the same setup, and are therefore likely to ignore your scheme. That could be even you, when you're forced to make edits on another computer / editor. IMO, you don't lose much in beauty, but gain a lot in maintainability if you stick to simpler formatting, like this:

/****************************************
  This is a comment
****************************************/

In particular, dropping the trailing, right-aligned * enables you to quickly reformat comments with gq.

Autres conseils

I have a 75-wide block like this:

/***********************************/
/***********************************/
/***********************************/

Saved in a named register ("e" but could be anything). I then just paste it when needed and overwrite ("r"replace) the center line with comment text.

Not scripted but fast and effective. If I need a different comment method, say "#" for Python, it's trivial to swap the contents of the "e" register.

I use this mapping:

nnoremap <leader>g I// <Esc>A //<Esc>yyp0llv$hhhr-yykPjj

to turn

hsdjsfdsgdfsdgfsdgf

into

//---------------------//
// hsdjsfdsgdfsdgfsdgf //
//---------------------//

I'm sure you could change it easily to match your banner style.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top