Jeff Roberts
RHCE #804006066322833
Vim-Fu is now iPhone and Android friendly

Check out the Vim-Fu Store!

Vim-Fu

delete all blank lines

:g/^\s*$/d

This uses the  search and replace function of VIM.

:g/pattern/command

“^\s*$” is the regex for “any line starting(^) with a space(\s), that has any number(*) of spaces after it and also ends($) with a space”

 

I recently used this to take a ridiculously long config file from squid (4518 lines) and knock it down to a more manageable 34 lines by doing:

 Open up the default config with:

vim /etc/squid/squid.conf.default

Then, load the same file again into a separate buffer (actually, it makes a copy of the current buffer):

:vnew /etc/squid/squid.conf.default

This gives a vertically split window with two separate buffers of the same file. Next remove all of the commented lines (the majority of them) out of one of the buffers.

:g/^#/d  <- remove all lines that are comments (the mojority in the case of squid)

:g/^\s*$/d <- remove all blank lines

 What is left is the default settings for squid.

 

The original buffer can be searched for the features you want to include and copy and pasted into a much more manageable config file. To switch back and forth between the two frames, use <CTRL>ww, then YY and [pP] to yank and paste.

Save the new file as squid.conf:

:w /etc/squid/squid.conf

 

I also regularly do this for Postfix config files.

1 comment to delete all blank lines

  • tr

    Nice tip. I am still learning the many many :functions within vim. I want to add that you can also use cat filename | grep -v ‘^$’ > filename.noBlankLines. ^$ is regexp for blank line.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>