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

link|improve this question

feedback

1 Answer

up vote 2 down vote accepted

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

:g/^\s*$/d
link|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 ... – honk 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. – El Isra May 27 '11 at 19:38
show 2 more comments
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.