vimtricks.wiki Concise Vim tricks, one at a time.

How do I join all soft-wrapped lines in each paragraph into a single line?

Answer

:g/^./,/^$/join

Explanation

The :g/^./,/^$/join command collapses each block of consecutive non-empty lines (a paragraph) into a single line. This is especially useful when working with prose, markdown, or any soft-wrapped text that you need to reformat or process as whole paragraphs.

How it works

  • :g/^./ — The global command iterates over all lines matching ^. (a line beginning with any character — i.e., any non-empty line).
  • ,/^$/ — For each matched line, defines a range from that line to the next completely empty line (^$).
  • join — Joins all lines in that range into one, inserting a single space between each.

Empty lines between paragraphs remain untouched since they don't match ^., so the paragraph structure is preserved.

Example

Given soft-wrapped text:

The quick brown fox
jumps over the lazy
dog.

Pack my box with five
dozen liquor jugs.

After :g/^./,/^$/join:

The quick brown fox jumps over the lazy dog.

Pack my box with five dozen liquor jugs.

Tips

  • Restrict to a visual selection: :'<,'>g/^./,/^$/join.
  • To re-wrap at a fixed column width, use gq{motion} with :set textwidth=80.
  • If the file does not end with an empty line, the last paragraph may not be joined; add a trailing blank line with Go<Esc> first.

Next

How do I inspect a Vim register's full details including its type (charwise, linewise, or blockwise) in Vimscript?