Replacing Multiple blank lines with a single blank line in vim / sed
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.
Related videos on Youtube
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, 2022Comments
-
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 about 13 yearswow, 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 about 13 yearsMarking as 'winning' for the
:%!cat -s
. Learn something GNnew everyday! -
jasonwryan over 11 yearsYou might want to add a little detail around how this works, even if it is just a quote from the
man
page. -
Sagar Jain over 8 yearsCan you please explain how does the search pattern?
-
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 about 7 years
uniq
removes adjacent equal lines. Which is not what OP wants. -
pacholik about 7 yearsYes, exactly...
-
Niko Bellic almost 7 yearsI think you can also use \r instead of ^M like this:
%s!\n\n\n\+!\r\r!g
-
user6380706 over 6 yearsBut why do you need to use a group when its contents are fixed?
-
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 over 6 yearsNow I understand, thank you for taking the time to answer.
-
user674669 over 6 yearsDidn't work on windows 7
-
Luke Davis over 6 yearsI 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 almost 5 yearsI needed this today. Thank you, Caleb! :)
-
silicontrip over 3 yearsI use a technique like this using the
fmt
command to do hard word wrapping on text andunexpand
to convert spaces to tabs. -
shalomb about 3 yearsThis does more than remove multiple blank lines - it removes all repeating lines.
-
Admin almost 2 years@user674669,
!
will execute a shell command.cat
is a Unix shell command, the equivalent command in Windows istype
, but it does not condenses empty lines. Look for the other answers that use the substitute command (s
)