Rook
Rook

Reputation: 62588

Reformatting in Vim, the sensible way

I don't often reformat text, apart from the plain gq so this is probably something simple, but just don't seem to have the luck of finding it in the help.

Anyways, I have the text that looks like this

Funnily enough, that was exciting. 
"I've just about had enough of this," said a voice beside him. 
He looked up. A girl had come down the other path. Her face was red with exertion under the pale make-up, her hair hung over her eyes in ridiculous ringlets, and she wore a dress which, while clearly made for her size, was designed for someone who was ten years younger and keen on lace edging. 
She was quite attractive, although this fact was not immediately apparent. 
"And you know what they say when you complain?" she demanded. This was not really addressed to Victor. He was just a convenient pair of ears. 

And that's a pain to read in Vim. So I tried to reformat it with gq and that gives me this

Funnily enough, that was exciting.  "I've just about had enough of this,"
said a voice beside him.  He looked up. A girl had come down the other path.
Her face was red with exertion under the pale make-up, her hair hung over
her eyes in ridiculous ringlets, and she wore a dress which, while clearly
made for her size, was designed for someone who was ten years younger and
keen on lace edging.  She was quite attractive, although this fact was not
immediately apparent.  "And you know what they say when you complain?" she
demanded. This was not really addressed to Victor. He was just a convenient
pair of ears. 

which is rather useless, since the original line endings have special meaning in this case. What I'm trying to accomplish is this

Funnily enough, that was exciting. 
"I've just about had enough of this," said a voice beside him. 
He looked up. A girl had come down the other path. Her face was red with
exertion under the pale make-up, her hair hung over her eyes in ridiculous
ringlets, and she wore a dress which, while clearly made for her size, was
designed for someone who was ten years younger and keen on lace edging. 
She was quite attractive, although this fact was not immediately apparent. 
"And you know what they say when you complain?" she demanded. This was not
really addressed to Victor. He was just a convenient pair of ears. 

i.e. to keep the original line endings, but to "break" every line longer than textwidth into several lines. So it fits the predefined column width limits.

Anyone have any ideas on how to do that? It is a rather large-ish document, and I need some way of handling it in one piece.

Upvotes: 4

Views: 242

Answers (5)

ZyX
ZyX

Reputation: 53674

You can make gq think that a series of lines belongs to one paragraph if every line of the series except the last one ends with a space:

set formatoptions+=w

. After this setting gq won’t join lines in your example (unless you have trailing spaces there) and you will still be able to join them back using :%s/ \n/ /. Alternative is to add empty lines between each current line.

I also suggest doing

set list listchars+=trail:-

in order not to only make vim see where the paragraph ends, but to be able to see this by yourself (this setting will show you trailing whitespaces).

Upvotes: 0

Kevin Vaughan
Kevin Vaughan

Reputation: 15220

Do you just want to do this for reading purposes? If so, you should consider just turning on line wrapping at word breaks. In command mode:

:set wrap
:set linebreak

Upvotes: 2

Fatih Arslan
Fatih Arslan

Reputation: 17157

Select visually all lines then execute in ex mode:

:norm gqq

gqq reformats a single line. :norm with a range applies a normal code to each in individually in the range. That means you apply gqq on each single line individually. And because your textwidth is set to a certain length (for example 80) that means shorter lines will not be joined/wrapped.

I've tested this on your example text and it just gives what you want.

Btw, you can use vim's :formatprg to modify it with an external prg. That gives more control of what you want modify with an external application. For more info read :h formatprg

Upvotes: 5

Rook
Rook

Reputation: 62588

A primitive way, but in general managed to do it with

tw=80
qa (recording a macro)
  Vgq
q (stop recording)

nmap <C-p> :execute "normal! @a"<cr>

and by holding <C-p> for quite a while. Not the most elegant of solutions but worked.

Upvotes: 0

twalberg
twalberg

Reputation: 62519

Assuming this is on Linux, there are a number of utilities to do what you're wanting - fmt, roff/nroff/troff and variants, etc. fmt is one I use often, but it would require that you have a blank line between each paragraph - that's easy to accomplish in vim, though. So you could add blank lines, save the file, then run it by fmt -76 for example to limit each line to 76 characters.

Upvotes: 0

Related Questions