Vim delete blank lines

VimVi

Vim Problem Overview


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

Vim Solutions


Solution 1 - Vim

:g/^$/d

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

Solution 2 - Vim

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.

Solution 3 - Vim

:v/./d

or

:g/^$/d

or

:%!cat -s

Solution 4 - Vim

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

Solution 5 - Vim

  1. how to remove all the blanks lines

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

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

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

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

(do this multiple times)

  1. 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

Solution 6 - Vim

How about:

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

Solution 7 - Vim

This works for me

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

Solution 8 - Vim

work with perl in vim:

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

Solution 9 - Vim

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>

Solution 10 - Vim

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

Solution 11 - Vim

: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!

Solution 12 - Vim

Press delete key in insert mode to remove blank lines.

Solution 13 - Vim

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/^^^^.*

Solution 14 - Vim

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnearly_lunchtimeView Question on Stackoverflow
Solution 1 - VimsoulmergeView Answer on Stackoverflow
Solution 2 - Vimnearly_lunchtimeView Answer on Stackoverflow
Solution 3 - VimmandaleekaView Answer on Stackoverflow
Solution 4 - VimDraemonView Answer on Stackoverflow
Solution 5 - Vimgauge00View Answer on Stackoverflow
Solution 6 - VimanonView Answer on Stackoverflow
Solution 7 - VimallenhwkimView Answer on Stackoverflow
Solution 8 - VimniejieqiangView Answer on Stackoverflow
Solution 9 - VimSergioAraujoView Answer on Stackoverflow
Solution 10 - VimPokeyView Answer on Stackoverflow
Solution 11 - Vimcn8341View Answer on Stackoverflow
Solution 12 - VimakpView Answer on Stackoverflow
Solution 13 - VimMisterWView Answer on Stackoverflow
Solution 14 - Vimuser1481441View Answer on Stackoverflow