Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a vim idiom for removing all lines from a file that have only whitespace (including newlines)?

share|improve this question
up vote 2 down vote accepted

The d command takes a range, and the range can be a regex.

:g/^\s*$/d
share|improve this answer
    
so, normally I use something like :%s/search/replace/g to do global search and replace. is :g equivalent to :%s with g option? – AJ. May 27 '11 at 19:12
    
: puts vim in command mode. g is a modifier for the range. % is the range that means "all lines". s is the substitute command. d is the delete command, which takes no options. – Ignacio Vazquez-Abrams May 27 '11 at 19:15
    
@Ignacio: On first read I saw that d takes no hostages ... – Benjamin Bannier May 27 '11 at 19:16
    
@honk: Unfortunately it does. Fortunately they're only a p away. – Ignacio Vazquez-Abrams May 27 '11 at 19:17
2  
:g[lobal] applies an ex command to the lines that match a given pattern. So, in the given solution :global will search for lines with 0 or more white space (/^\s*$/) and apply the :d[elete] command on them. See :h :global for more details and alternatives. – Raimondi May 27 '11 at 19:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .