tags:

views:

98

answers:

3

I have some text files previously formatted in vim using "gggqG". Now I need to convert them to one line per paragraph format. I saw a way(using :g command) to do that before, but I have forgot it. Anyone knows?

A: 

Maybe you should set textwidth to very large value (like 99999999) (0 does not work for some reason) and use gggqG?

// I cannot tell you a way to reformat your paragraph with :g without knowing exactly what the paragraph is. Maybe somebody else can.

ZyX
+1  A: 

There are two approaches I know of:

  • Set textwidth to something big and reformat:

    :set tw=1000000
    gggqG
    
  • Use substitute (this is more appropriate if you want to do it in a mapping):

    :%s/.\zs\n\ze./ /
    

Explanation of the latter:

:%s        " Search and replace across the whole file
/          " Delimiter
.\zs\n\ze. " Look for a character either side of a new-line (so ignore blank lines).
           " The \zs and \ze make the replacement only replace the new-line character.
/ /        " Delimiters and replace the new-line with a space.
Al
A: 

If your text paragraphs are separated by a blank line, this seems to work:

:g!/^\s*$/normal vipJ

:g global (multi-repeat)

!/^\s*$/ match all lines except blank lines and those containing only whitespace.

normal enters 'normal' mode

vip visually select inner paragraph

J join lines

Curt Nelson
+1: This is very nearly perfect.Lines with zero or more than one whitespace character may be considered empty, too. I suggest `/^\s*$/`. Also note that `:v` is equivalent to `:g!` So you can get your "vim par" down to just 17: `:v/^\s*$/norm vipJ`.
Johnsyweb
@Johnsyweb: Good catch. There was supposed to be a `*` in there... I'll update that. Thanks for the `:v` tip as well.
Curt Nelson