Join lines inside paragraphs in vim
I think this does what you want: make sure there is an empty line at the end of the file, then join every paragraph (terminated by an empty line).
G:a
.
:g/^./ .,/^$/-1 join
Explanation: first go to the end of the file and append an extra empty line with :a
(maybe there's a more elegant way to do that; interactively, you can replace the first three lines with o<ESC>
). Then, for every non-blank line that hasn't been considered yet (:g/^./
), apply the join
command to the range starting at the selected line (.
) and ending one line before the next empty line (/^$/-1
).
Optionally, :g/^$/d
if you don't want any blank line to remain (then you can take off the -1
).
This should do it:
:set tw=99999
gggqG
tw
is set to some value at least as large as the number of characters in the longest paragraph. gg
moves the cursor to the first line; gq
is the command to reformat; G
moves the cursor to the last line, telling gq
to reformat from the current cursor location to the last line.
Set the cursor inside the desired paragraph
Type:
vipJ
(vip
highlights the current paragraph, J
joins all lines)