4

I want to automatically generate block comments for documentation using vim.

Something like:

/**
 * comment
 */

are there any plugins for this?

chovy
  • 72,281
  • 52
  • 227
  • 295

2 Answers2

5

Vim has this functionality built-in. See :help format-comments for details. Basically if you have filetype plugin on in your ~/.vimrc and are using a bracket language (like C, C++, Java, javascript, etc.), you can type /**<cr> and it will expand to:

/**
 * _

where _ is the cursor position. When you're done with the comment block just hit / to end it.

Conner
  • 30,144
  • 8
  • 52
  • 73
  • @chovy You need `filetype plugin on` in your `~/.vimrc` near the top. Beyond that it depends on what filetype you're using. You can check with `:se ft?`. – Conner Nov 10 '12 at 07:03
2

A low-budget way of simplifying a Vimmer's life with C- or Java-style block comments is to add the following mapping to .vimrc.

autocmd FileType c,java inoreabbrev <buffer> /** /**<CR>/<Up>

That way, whenever you type /**<Enter> in your C or Java source it will be expanded to the following, with the cursor at _:

/**
 * _
 */

Edit: As @Conner mentioned, you need to have filetype plugin indent on in your vimrc to make this work.

glts
  • 21,808
  • 12
  • 73
  • 94
  • @rudolph9 Well, once you've put the autocommand in you vimrc and edit a Java or C source file, what happens when you do `/**`? – glts Nov 10 '12 at 00:55
  • I didn't actually put it in my `.vimrc`, shouldn't running `autocmd FileType c,java inoreabbrev /** /**/` from an open C or Java source file does not have the same effect? – rudolph9 Nov 10 '12 at 23:09
  • @rudolph9 No. That line of code only *registers* the autocommand. What you can do after registering it, is run `:e!` to reopen the current Java/C file. Then it should work without being in your vimrc. – glts Nov 11 '12 at 12:54
  • That worked! but the formatting of the comment ends up wrong (in both the case where it is in my vimrc and loaded into register and applied the current file as to you described), can you explain the logic of `/**/`? – rudolph9 Nov 11 '12 at 19:14
  • @rudolph9 You need to have the essential line `filetype plugin indent on` in your vimrc (strongly recommended!) to make this work. When you have it on and the abbreviation is triggered, some spaces and stars will automatically be added to make it look right. The right-hand side is just exactly what you would type by hand. – glts Nov 11 '12 at 22:27