views:

388

answers:

2

I realize there's a way in Vim to hide/fold lines, but what I'm looking for is a way to select a block of text and have Vim wrap lines at or near column 80.

Mostly I want to use this on comments in situations where I'm adding some text to an existing comment that pushes it over 80 characters. It would also be nice if it could insert the comment marker at the beginning of the line when it wraps too. Also I'd prefer the solution to not autowrap the entire file since I have a particular convention that I use when it comes to keeping my structured code under the 80 character line-length.

This is mostly for Python code, but I'm also interested in learning the general solution to the problem in case I have to apply it to other types of text.

Thanks

+6  A: 

gq

It's controlled by the textwidth option, see ":help gq" for more info.

gq will work on the current line by default, but you can highlight a visual block with CTRL-V and format multiple lines / paragraphs like that.

gqap does the current "paragraph" of text.

Phil
+1  A: 

Take a look at ":help =" and ":help 'equalprg"

:set equalprg=fold

and in normal mode == filters the current line through the external fold program. Or visual-select something and hit =