omgodie
omgodie

Reputation: 209

Special comment snippet for VIM

I want to add comments dependant on the column i'm writing for example if i'm adding a comment over a code which is indented 4 spaces the comment should look like

/* Comment -------------*/

as many dashes as needed to fill up the line till column 100. It should recognize where the position is and how long the comment is.

I can't figure it out with vimscript myself.

Upvotes: 1

Views: 285

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172758

You can solve this with an expression mapping; see :help :map-expr:

:inoreabbrev <expr> comsep '/* Comment ' . repeat('-', 17 - indent('.')) . '*/'

This determines the width by subtracting the current indent (via indent()) from a constant. You could use &textwidth here, too.

Whenever you type comsep, it'll be expanded. Alternatively, you could also use an :inoremap <expr> <C-g> ... instead.

To insert the comment text, you could either use input(), or first just insert dashes and re-position the cursor by appending a number of "\<Left> keycodes.

If you use a snippet plugin like snipMate or Ultisnips, those may have functionality to dynamically modify the snippet, but the built-in methods should suffice.

Upvotes: 4

Related Questions