Replacing Multiple blank lines with a single blank line in vim / sed

21,709

Solution 1

If you aren't firing vim or sed for some other use, cat actually has an easy builtin way to collapse multiple blank lines, just use cat -s.

If you were already in vim and wanted to stay there, you could do this with the internal search and replace by issuing: :%s!\n\n\n\+!^M^M!g (The ^M is the visual representation of a newline, you can enter it by hitting Ctrl+vEnter), or save yourself the typing by just shelling out to cat: :%!cat -s.

Solution 2

Use \n to indicate a newline in the search pattern. Use Ctrl+M in the replacement text, or a backreference. See :help pattern and :help sub-replace-special (linked from :help :s).

%s/\(\n\n\)\n\+/\1/

Solution 3

If in Vim, just do this:

:%!cat -s

The -s flag for cat squeezes multiple blank lines into one.

Solution 4

Using Perl:

perl -00 -pe ''

-00 command line option turns paragraph slurp mode on, meaning Perl reads text paragraph by paragraph rather than line by line.

Share:
21,709

Related videos on Youtube

Andrew Bolster
Author by

Andrew Bolster

I'm a Masters student of Electronics & Software Engineering at Queen's University Belfast. I want to get more involved in FOSS (as I've been using FOSS for years now). I try to document my experiments and experiences on my blog so check it out

Updated on September 18, 2022

Comments

  • Andrew Bolster
    Andrew Bolster over 1 year

    Question more or less says it all. I'm aware that /^$/d will remove all blank lines, but I can't see how to say 'replace two or more blank lines with a single blank line'

    Any ideas?

  • maxschlepzig
    maxschlepzig about 13 years
    wow, did not know the -s option of cat - just a historic note, it is not in POSIX, but seems to be available in BSD and GNU cat.
  • Andrew Bolster
    Andrew Bolster about 13 years
    Marking as 'winning' for the :%!cat -s. Learn something GNnew everyday!
  • jasonwryan
    jasonwryan over 11 years
    You might want to add a little detail around how this works, even if it is just a quote from the man page.
  • Sagar Jain
    Sagar Jain over 8 years
    Can you please explain how does the search pattern?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 8 years
    @sjmp Requirement: replace two or more blank lines with a single blank line. Implementation: replace three or more consecutive newlines with just two newlines. See the manual for an explanation of the constructs used in the command.
  • pacholik
    pacholik about 7 years
    uniq removes adjacent equal lines. Which is not what OP wants.
  • pacholik
    pacholik about 7 years
    Yes, exactly...
  • Niko Bellic
    Niko Bellic almost 7 years
    I think you can also use \r instead of ^M like this: %s!\n\n\n\+!\r\r!g
  • user6380706
    user6380706 over 6 years
    But why do you need to use a group when its contents are fixed?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 6 years
    @Sabuncu You don't need to use a group. But why not use a group? Here, I used a group because you can't copy-paste a command with a newline in the replacement text: you need to insert a literal Ctrl+M character, \n doesn't work in the replacement text.
  • user6380706
    user6380706 over 6 years
    Now I understand, thank you for taking the time to answer.
  • user674669
    user674669 over 6 years
    Didn't work on windows 7
  • Luke Davis
    Luke Davis over 6 years
    I added the map nnoremap <Leader>x :%s/\(\n\n\)\n\+/\1/gc<CR> to my .vimrc; thanks coming up with the regex. Yours is much cleaner than the accepted answer.
  • Dɑvïd
    Dɑvïd almost 5 years
    I needed this today. Thank you, Caleb! :)
  • silicontrip
    silicontrip over 3 years
    I use a technique like this using the fmt command to do hard word wrapping on text and unexpand to convert spaces to tabs.
  • shalomb
    shalomb about 3 years
    This does more than remove multiple blank lines - it removes all repeating lines.
  • Admin
    Admin almost 2 years
    @user674669, ! will execute a shell command. cat is a Unix shell command, the equivalent command in Windows is type, but it does not condenses empty lines. Look for the other answers that use the substitute command (s)