Friday, May 2, 2008

Remove Empty Lines

Here's a quick sub to remove all the empty lines in a file. I use this to cleanup after macros that leave a bunch of blanks.

:%s/^\n//

3 comments:

Unknown said...

Hi !
Your blog is very cool, it made me use H J K and L keys rater than the arrow keys in vim ^^ !

To remove blank lines, you can also do
:%g/^$/d

Since sometimes, \n does not work (I saw that once on AIX with vi)...

Travis Whitton said...

Wow, that's great! Glad you're enjoying the blog and thanks for the tip.

Anonymous said...

Really a nice blog, so much tips about vim, great, thanks.

Outside vim:

$ grep -v '^$' file
$ grep '.' file
$ sed '/^$/d' file
$ sed -n '/^$/!p' file
$ awk NF file
$ awk '/./' file

http://unstableme.blogspot.com