Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Remove unwanted empty lines
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Remove unwanted empty lines
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{TipImported |id=72 |previous=71 |next=73 |created=2001 |complexity=basic |author=Volker Duetsch |version=5.7 |rating=257/125 |category1=Searching |category2= }} Use either of the following commands to delete all empty lines: <pre> :g/^$/d :v/./d </pre> If you want to delete all lines that are empty or that contain only whitespace characters (spaces, tabs), use either of: <pre> :g/^\s*$/d :v/\S/d </pre> In the second command, <code>v</code> operates on lines that do ''not'' match, and <code>\S</code> matches anything that is ''not'' a whitespace, and <code>d</code> 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 [[VimTip878|delete all trailing whitespace]] from each line, then replace three or more consecutive line endings with two line endings (a single blank line): <pre> :%s/\s\+$//e :%s/\n\{3,}/\r\r/e </pre> The <code>e</code> substitute flag means that no error is displayed if the pattern is not found. In the second command, <code>\n</code> in the search pattern finds newline, while <code>\r</code> in the replacement inserts a newline. Another solution is to join consecutive lines found between non-blank lines. <pre> :v/\S/,/\S/-j </pre> ==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 <code>\u3000</code> in a search pattern to specify the UTF-16 hex code for IDEOGRAPHIC SPACE. {{help|tag=/\%5D|label=/\]}} In that case, the following will delete all empty lines, or lines that contain only combinations of space, tab or IDEOGRAPHIC SPACE. <pre> :g/^[ \t\u3000]*$/d </pre> 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. <pre> :g/^[ \t#]*$/d </pre> 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 <code>yl</code> to yank the character. Then, enter the above <code>:g</code> 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). ==See also== *[[VimTip227|Power of g]] Some advanced techniques to condense multiple blank lines *[[VimTip242|Search across multiple lines]] Searching across multiple lines *[[VimTip1066|Quickly adding and deleting empty lines]] A method to easily add/delete adjacent blank lines *[[VimTip213|Delete all lines containing a pattern]] Examples using <code>g//d</code> *[[VimTip399|Fold away empty lines]] To temporarily hide multiple blank lines ==Comments==
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Help
(
view source
)
Template:Navigation
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab