You are here

Vim: Rejoin paragraph with forced line breaks / hard-wrapped multiple lines

Submitted by Druss on Sun, 2014-06-29 00:42

Some documents contain paragraphs which are wrapped often at the 80 character mark to help with formatting and readability. This is sometimes accomplished using forced line breaks which can be quite annoying especially when you want to reverse it as I did earlier today. Rather than messing with regex and weird edge cases, use Vim which provides a lovely solution! Here it be:

  1. Add the command :set tw=999999
    This sets the text width (number of characters in a line) to a really large value.
  2. Now use Vim's magic command: gggqG
  3. And voila, the problem is solved.

Explaining the magic:
gg tells Vim to go to the start (by default, of the buffer). Then gq tells Vim to automatically reformat from that line onwards based on current settings (which includes our new text width directive). Finally, the G tells Vim to keep reformatting until it reaches the last line.

Pure gold!