2

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

2

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

:g/^\s*$/d
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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