sponsor Vim development Vim logo Vim Book Ad

basic Tip #72: Remove unwanted empty lines

 tip karma   Rating 308/152, Viewed by 5934 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   June 5, 2001 11:11      complexity:   basic
author:   Volker Duetsch      as of Vim:   5.7

Sometimes to improve the readability of the document I insert empty lines, which will be later removed.
To get rid off them try:
:%g/^$/d
This will remove a l l  empty line in the document.
Some other tipps you can find under www.linuxclass.de/vim.phtml

 rate this tip  Life Changing Helpful Unfulfilling 

<< Transfer text between two gvim sessions using clipboard | Using vim as calculator >>

Additional Notes

Anonymous, July 13, 2001 20:42
I know this may sound like trolling, but this makes me wonder why there's not an option to rate suggestions "Detremental".
Anonymous, January 13, 2004 1:00
Detrimental? In my opinion tips like these are useful since they show up nicely when you for example
google for this little hint you just needed.
Anonymous, January 9, 2005 15:01
This is exactly what I was looking for !
[email protected], March 23, 2005 12:02
Another way to do it especially if you have spaces or tab characters in your empty lines:
:%s/^[\ \t]*\n//g
This pretty much says: replace whole lines that "may" contain spaces or tabs with nothing globally (g).
You can add a "c" (for confirm) after the "g" to confirm on every erased line if you don't trust the regexp.
Greets,
Roumen Semov.
[email protected], July 5, 2005 10:34
Thank you much. This was exactly what I needed to know.
sator at npr dot org, October 12, 2005 8:40
this was exactly what I was needing. I knew it was something simple; just couldn't remember the exact syntax.
Anonymous, September 3, 2006 23:38
Thanks!
Anonymous, November 8, 2006 6:28
on some machines,
:%s/\n\+/\n/g
[email protected], November 8, 2006 6:32
on some machines,
:%s/\n\+/\n/g
replaces all line breaks with  a ^@

i can't find the reason (already looked for line break type and encoding)
Anonymous, November 8, 2006 6:34
:%s/\(\n\)\n\+/\1/g
that works on those machines
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo