Vim Find Blank Lines
From Regex Regular Expression Encyclopedia
You can use this recipe for identifying blank lines in a file. Blank lines can contain spaces or tabs, or they can contain a combination of spaces and tabs. Variations on these expressions can be useful for stripping blank lines from a file.
[edit] Code
/^\s*$
[edit] How It Works
The previous \s sequence represents a character class. A character class can match an entire category of characters, which \s does here even though it's a category that contains only tabs and spaces. Other character classes match far more examples, such as \d, which matches digits in most regular expression flavors. Character classes are shorthand ways of describing sets of characters.
| Regular Expression | Description |
|---|---|
| ^ | starts the beginning of the line, followed by . . . |
| \s | any whitespace character (a tab or space) . . . |
| * | zero or more times, followed by . . . |
| $ | the end of the line. |
[edit] Variations
You can alter the grep recipe by adding the -v parameter to grep, which tells grep to print the lines that don't match the expression. The Perl and vi recipes have the advantage of containing a different character class that means "nonwhitespace." That character class is \S. Changing the expression in the Perl recipe to ^\S*$ will have the opposite effect as ^\s*$.
