vim - how to select and yank all comments?

Viewed 142

I use doxygen documentation for c code. Now I want to move these documentation to a saparate dox file. Is there any quick and simple solution to yank these doxygen comments in vim?

/** 
 *....
 *....
 */ 

/*!
 *...
 *...
 *...
 */

I know I can use y% to yank all code in block when cursor is on first /. But our documentation is all over the file and it's hard to yank one by one.

2 Answers

This is pretty standard and general technique.

Press qaq to clear register "a". Then enter command:

:g/regex1/ .,/regex2/ yank A

Where regex1 is a regular expression to match the start of a block, and regex2 matches the end of a block.

Now all relevant lines should get into register "a".

I thik we need a non-greedy search to avoid coping everything from the first comment to the end of the second or last one. Something like this:

:let @a='' 
:g/\v^\/\*\_.{-}\*\//yank A

And as we are appending content to the register A it is recommended to clear it first, that's why :let @a=''.

Related