3

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.

0

1 Answer 1

2

To the first question, use

:%normal gqq

That will execute gqq on each line individually.

I don't know the answer to the second question, but it could be the result of an autocommand triggered by the .txt suffix that changes 'formatoptions' or 'comments' without setting the 'filetype'. You can execute

:verbose set fo? com?

to see where those options were set last. Some GNU/Linux distributions put their own Vim configuration commands in /etc/vimrc or /usr/share/vim/vimrc.

3
  • Thanks! Oddly it doesn't do the whole file; it stops 70% of the way through and says "6 more lines" (though there's far more than 6 lines remaining). Repeating the command takes it to 95% and says "5 more lines", and once more completes the file. There's no indication that any error occurred to make it stop.
    – Miles
    Feb 2, 2011 at 9:58
  • Try this instead then: :g/./normal gqq. I think that % applies the command to every line as it goes, whereas g/pattern/ marks each matching line first, then goes back and applies the command to each marked line. Because your command is changing the number of lines in the file, the g/pattern/ may work better.
    – garyjohn
    Feb 2, 2011 at 16:34
  • I couldn't reproduce the "different wrapping behavior" I mentioned in my question--user error, I guess. And the g/./ approach did work on the entire file, this time with the message "84 more lines"--which I guess is the number of lines added to the file, not number of lines remaining or anything. Thanks!
    – Miles
    Feb 3, 2011 at 0:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .