754

What command can I run to remove blank lines in Vim?

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
nearly_lunchtime
  • 12,203
  • 15
  • 37
  • 42

14 Answers14

1374
:g/^$/d

:g will execute a command on lines which match a regex. The regex is 'blank line' and the command is :d (delete)

soulmerge
  • 73,842
  • 19
  • 118
  • 155
  • 48
    Thanks soulmerge. This is my favorite answer, since it actually explains what the :g command does. – Tim Swast Aug 15 '11 at 19:03
  • 5
    This will delete all the empty lines(do not contain any white space characters..), but that may not be the unique requirement. Someone may still keep one of the empty line. `:%!cat -s` may be the choice.. – coanor Nov 21 '12 at 05:04
  • 3
    :g/^\s*$/d with a regex of 'any possible space char' in the line – oyd11 Aug 16 '18 at 12:32
  • @soulmerge what about adding range `1,$/^\s$/d` or using tags `'a,'b/^\s$/d`? This does not work for me – Alexander Cska Jul 06 '20 at 20:21
  • @AlexanderCska should be `1,$g/^$/d` – brownian Nov 11 '21 at 12:46
212

Found it, it's:

g/^\s*$/d

Source: Power of g at vim wikia

Brief explanation of :g

:[range]g/pattern/cmd

This acts on the specified [range] (default whole file), by executing the Ex command cmd for each line matching pattern (an Ex command is one starting with a colon such as :d for delete). Before executing cmd, "." is set to the current line.

Randall
  • 2,859
  • 1
  • 21
  • 24
nearly_lunchtime
  • 12,203
  • 15
  • 37
  • 42
  • 9
    Nice. But not exactly a blank line. – innaM Apr 01 '09 at 16:00
  • 13
    Ah, yes, this will match lines containing only whitespace characters. I'll accept soulmerge's answer. – nearly_lunchtime Apr 02 '09 at 09:18
  • 17
    that should match blank lines AND lines composed only of whitespaces... (* matches zero or multiple instances of \s...)? – monojohnny Apr 15 '11 at 11:59
  • 14
    I would argue pure whitespace IS EXACTLY a blank line, whereas no whitespace is really more of an empty line, don't ya think? ;) – Kasapo Dec 05 '12 at 20:02
  • 5
    Oxford says a blank is "a space left to be filled in a document". Interpret that as you may, but this is exactly what I was looking for. Thanks! – Joe Tricarico Apr 08 '14 at 15:13
  • Is there some man page where I can read about the used flags and the available ones for this command. – Prabhat Kumar Singh Nov 27 '14 at 13:17
  • @PrabhatKumarSingh I know it's late but..yes. Try in vim: `:help :g` (at least if I understand you correctly). I'm not sure what you mean by flags as such since there aren't really any here I think but the command the answer here uses is `:g`. – Pryftan Feb 06 '20 at 12:22
56
:v/./d

or

:g/^$/d

or

:%!cat -s
mandaleeka
  • 6,577
  • 1
  • 27
  • 34
46

The following can be used to remove only multi blank lines (reduce them to a single blank line) and leaving single blank lines intact:

:g/^\_$\n\_^$/d
Draemon
  • 33,955
  • 16
  • 77
  • 104
  • 3
    Could someone explain what the tokens mean? (So, how does it work?) Thanks. – PAStheLoD Apr 19 '14 at 14:18
  • 5
    `\_$` means the *end-of-line* that can be used inside a pattern (`$` can only be used at the end of the pattern.) Same is with `\_^` that means *the start of a line* and can be used anywhere inside the pattern. So, the pattern matches an empty line, a newline, and again an empty line. See [Vim regex help page](http://vimdoc.sourceforge.net/htmldoc/pattern.html). – Wiktor Stribiżew Mar 30 '16 at 07:25
  • 1
    Another way to do the same: `:g/^$/,/./-j` – SergioAraujo Dec 21 '16 at 10:54
15
  1. how to remove all the blanks lines

    :%s,\n\n,^M,g
    

    (do this multiple times util all the empty lines went gone)

  2. how to remove all the blanks lines leaving SINGLE empty line

    :%s,\n\n\n,^M^M,g
    

    (do this multiple times)

  3. how to remove all the blanks lines leaving TWO empty lines AT MAXIMUM,

    :%s,\n\n\n\n,^M^M^M,g
    

    (do this multiple times)

in order to input ^M, I have to control-Q and control-M in windows

j0k
  • 22,600
  • 28
  • 79
  • 90
gauge00
  • 151
  • 1
  • 2
9

This works for me

:%s/^\s*$\n//gc

allenhwkim
  • 27,270
  • 18
  • 89
  • 122
8

How about:

:g/^[ \t]*$/d
5

work with perl in vim:

:%!perl -pi -e s/^\s*$//g

niejieqiang
  • 177
  • 1
  • 6
5

This function only remove two or more blank lines, put the lines below in your vimrc, then use \d to call function

fun! DelBlank()
   let _s=@/
   let l = line(".")
   let c = col(".")
   :g/^\n\{2,}/d
   let @/=_s
   call cursor(l, c)
endfun
map <special> <leader>d :keepjumps call DelBlank()<cr>
SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
5

I tried a few of the answers on this page, but a lot of them didn't work for me. Maybe because I'm using Vim on Windows 7 (don't mock, just have pity on me :p)?

Here's the easiest one that I found that works on Vim in Windows 7:

:v/\S/d

Here's a longer answer on the Vim Wikia: http://vim.wikia.com/wiki/Remove_unwanted_empty_lines

Pokey
  • 51
  • 1
  • 1
5

Press delete key in insert mode to remove blank lines.

Frits
  • 7,341
  • 10
  • 42
  • 60
akp
  • 619
  • 5
  • 12
4
:g/^\s*$/d
^ begin of a line
\s* at least 0 spaces and as many as possible (greedy)
$ end of a line

paste

:command -range=% DBL :<line1>,<line2>g/^\s*$/d

in your .vimrc,then restart your vim. if you use command :5,12DBL it will delete all blank lines between 5th row and 12th row. I think my answer is the best answer!

cn8341
  • 129
  • 7
  • When posting answers, please add and explanation describing what you changed or added or how it works or something. Even though your answer may be the solution, it's likely that some people who read this don't understand what it means or how it works. And please do it in english. – Tim Apr 30 '14 at 07:15
  • That doesn't really matter here. This question accepted an answer 5 years ago – Tim Apr 30 '14 at 07:32
  • I think soulmerge's answer :g/^$/d is not the best answer.and I edit to improve his answer with :g/^\s*$/d. but why I cannot watch my edit in "show all edits to this pose"? – cn8341 Apr 30 '14 at 07:41
  • My answer is better than his answer, and who rejected my answer and why my answer was rejected? – cn8341 Apr 30 '14 at 07:45
2

If something has double linespaced your text then this command will remove the double spacing and merge pre-existing repeating blank lines into a single blank line. It uses a temporary delimiter of ^^^ at the start of a line so if this clashes with your content choose something else. Lines containing only whitespace are treated as blank.

%s/^\s*\n\n\+/^^^\r/g | g/^\s*$/d | %s/^^^^.*
MisterW
  • 41
  • 4
1

This worked for me:

:%s/^[^a-zA-Z0-9]$\n//ig

It basically deletes all the lines that don't have a number or letter. Since all the items in my list had letters, it deleted all the blank lines.

Smern
  • 18,746
  • 21
  • 72
  • 90