Using vim, how do I fix word wrapping with leading comment markers like #
How do I fix the word wrapping in the following text, so that the comment # characters are also fixed up?
Given this:
# This is documentation
# that
# is very helpful. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc sagittis eget dolor sed ultricies.
I would like this:
# This is documentation that is very
# helpful. Lorem ipsum dolor sit amet,
# consectetur adipiscing elit. Nunc
# sagittis eget dolor sed ultricies.
(using an arbitrary :set tw=42
)
As muru points out in comments, the command gq}
would do this, if the cursor is initially located on the first line of the text that you want to reformat and you want to reformat the rest of the paragraph (which is what the movement command }
covers). The cursor would be moved to the end of the paragraph.
You could also use gw}
to do the same thing, but without moving the cursor as a result of the operation. The gw
command also does not use formatprg
or formatexpr
, if these are set, which gq
would do.
To reformat the current paragraph, without having to first reposition the cursor at the start of the paragraph, use gqap
or gwap
(equivalent to gq}
and gw}
respectively).