created June 5, 2001 · complexity basic · author Volker Duetsch · version 5.7
Use either of the following commands to delete all empty lines:
If you want to delete all lines that are empty or that contain only whitespace characters (spaces, tabs), use either of:
In the second command, v operates on lines that do not match, and \S matches anything that is not a whitespace, and d deletes the flagged lines (all lines that have no characters, or that have only whitespace characters).
You may want to condense multiple blank lines into a single blank line. The following will delete all trailing whitespace from each line, then replace three or more consecutive line endings with two line endings (a single blank line):
The e substitute flag means that no error is displayed if the pattern is not found. In the second command, \n in the search pattern finds newline, while \r in the replacement inserts a newline.
Handling IDEOGRAPHIC SPACE
In CJK languages (Chinese, Japanese, Korean), the Unicode character IDEOGRAPHIC SPACE may be used. If you have 'encoding' utf-8 (and do not have the 'l' flag in 'cpoptions'), you can use \u3000 in a search pattern to specify the UTF-16 hex code for IDEOGRAPHIC SPACE. :help /\]
In that case, the following will delete all empty lines, or lines that contain only combinations of space, tab or IDEOGRAPHIC SPACE.
An alternative procedure, which should work in other encodings, would be to enter the CJK space directly into the pattern. That is, you would type the following, but instead of '#' you would enter a CJK space.
If you're not sure how to type the character, position your cursor on an example of the character that you already have in the buffer. Then type yl to yank the character. Then, enter the above :g command, but instead of '#' type Ctrl-R " (Control-R then double-quote, which will insert the contents of the unnamed register containing the yanked character).
- Power of g Some advanced techniques to condense multiple blank lines
- Search across multiple lines Searching across multiple lines
- Quickly adding and deleting empty lines A method to easily add/delete adjacent blank lines
- Delete all lines containing a pattern Examples using g//d
- Fold away empty lines To temporarily hide multiple blank lines