How to reformat “gq”ed text in one line per paragraph format in Vim

后端 未结 3 1859
感情败类
感情败类 2021-02-06 04:13

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,

3条回答
  •  说谎
    说谎 (楼主)
    2021-02-06 05:18

    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.
    

提交回复
热议问题