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

How do I join wrapped paragraph lines while keeping blank-line paragraph separators?

Answer

:g/^\s*$/,/./-1join

Explanation

When text is hard-wrapped (for example from email, logs, or copied docs), joining entire paragraphs manually is slow and error-prone. :g/^\s*$/,/./-1join gives you a fast batch cleanup: it joins contiguous nonblank lines into single lines while keeping blank lines as paragraph boundaries. This is especially useful before running structural edits, parsers, or reflow tools that expect one logical paragraph per line.

How it works

  • :g/^\s*$/ iterates over blank lines (paragraph separators)
  • ,/./-1 creates a range from that blank line to the line before the next nonblank match offset
  • join merges lines in the range into one line, inserting spaces between joined lines

In practice, each block of wrapped lines gets collapsed, but blank-line separators remain, so paragraph structure is preserved.

Example

Before:

This is a long paragraph
that was wrapped at column 72
by another tool.

Second paragraph
also wraps
across lines.

After running the command:

This is a long paragraph that was wrapped at column 72 by another tool.

Second paragraph also wraps across lines.

Tips

  • Use :join! instead of join if you need to avoid inserting spaces
  • Run on a visual range by prefixing with :'<,'> when you only want part of the buffer
  • Combine with :set textwidth=0 temporarily if you want to prevent immediate re-wrapping

Next

How do I make buffer jumps prefer the last-used window that already shows the target buffer?