What command can I run to remove blank lines in Vim?
14 Answers
:g/^$/d
:g
will execute a command on lines which match a regex. The regex is 'blank line' and the command is :d
(delete)
-
55Thanks soulmerge. This is my favorite answer, since it actually explains what the :g command does. Commented Aug 15, 2011 at 19:03
-
5This 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..– coanorCommented Nov 21, 2012 at 5:04 -
4:g/^\s*$/d with a regex of 'any possible space char' in the line– oyd11Commented Aug 16, 2018 at 12:32
-
@soulmerge what about adding range
1,$/^\s$/d
or using tags'a,'b/^\s$/d
? This does not work for me Commented Jul 6, 2020 at 20:21 -
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.
-
9
-
15Ah, yes, this will match lines containing only whitespace characters. I'll accept soulmerge's answer. Commented Apr 2, 2009 at 9:18
-
17that should match blank lines AND lines composed only of whitespaces... (* matches zero or multiple instances of \s...)? Commented Apr 15, 2011 at 11:59
-
15I would argue pure whitespace IS EXACTLY a blank line, whereas no whitespace is really more of an empty line, don't ya think? ;)– KasapoCommented Dec 5, 2012 at 20:02
-
5Oxford 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! Commented Apr 8, 2014 at 15:13
:v/./d
or
:g/^$/d
or
:%!cat -s
-
10Neat trick with the :v/./d (easier to type) but then you need to do a :nohl after. Commented May 11, 2011 at 8:05
-
1
-
6@zladuric according to vim.wikia.com/wiki/Power_of_g it's reVerse global (so it does the command on everything that doesn't match)– adam_0Commented May 1, 2013 at 18:29
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
-
3Could someone explain what the tokens mean? (So, how does it work?) Thanks. Commented Apr 19, 2014 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. Commented Mar 30, 2016 at 7:25 -
1
how to remove all the blanks lines
:%s,\n\n,^M,g
(do this multiple times util all the empty lines went gone)
how to remove all the blanks lines leaving SINGLE empty line
:%s,\n\n\n,^M^M,g
(do this multiple times)
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
-
2
This works for me
:%s/^\s*$\n//gc
-
2
-
-
work with perl in vim:
:%!perl -pi -e s/^\s*$//g
-
My perl may be a bit rust, but wouldn't
chomp
also do the job? Commented Jun 15, 2014 at 21:21
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
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>
: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!
-
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.– TimCommented Apr 30, 2014 at 7:15
-
That doesn't really matter here. This question accepted an answer 5 years ago– TimCommented Apr 30, 2014 at 7: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"?– cn8341Commented Apr 30, 2014 at 7:41
-
My answer is better than his answer, and who rejected my answer and why my answer was rejected?– cn8341Commented Apr 30, 2014 at 7:45
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/^^^^.*
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.