Hard wrapping in vim without joining

Posted by Miles on Super User See other posts from Super User or by Miles
Published on 2011-02-02T05:04:21Z Indexed on 2011/02/02 7:27 UTC
Read the original article Hit count: 456

Filed under:

Vim newbie here. How can I hard wrap plain text in vim (inserting actual linebreaks), respecting word boundaries, without joining existing lines?

For example, given this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

- Nulla cursus accumsan faucibus.
- Donec dapibus dignissim ullamcorper.

Integer
nec
malesuada
diam.

I'd like to get (with textwidth=30):

Lorem ipsum dolor sit amet,
consectetur adipiscing elit.

- Nulla cursus accumsan
  faucibus.
- Donec dapibus dignissim
  ullamcorper.

Integer
nec
malesuada
diam.

instead of this (which I can get with gggqG)

Lorem ipsum dolor sit amet,
consectetur adipiscing elit.

- Nulla cursus accumsan
  faucibus.
- Donec dapibus dignissim
  ullamcorper.

Integer nec malesuada diam.

Also, for bonus points: when I create a brand new buffer, I get different wrapping behavior (lines beginning with - aren't wrapped specially) than when I open a file ending in .txt. What controls this? I don't notice any difference in the output of :set filetype? or :filetype.

© Super User or respective owner

Related posts about vim