Vim Tips Wiki
No edit summary
AntOfThy (talk | contribs)
No edit summary
Tags: Visual edit apiedit
 
(12 intermediate revisions by 9 users not shown)
Line 3: Line 3:
 
|previous=71
 
|previous=71
 
|next=73
 
|next=73
|created=June 5, 2001
+
|created=2001
 
|complexity=basic
 
|complexity=basic
 
|author=Volker Duetsch
 
|author=Volker Duetsch
Line 23: Line 23:
 
</pre>
 
</pre>
   
In the second command, <tt>v</tt> operates on lines that do ''not'' match, and <tt>\S</tt> matches anything that is ''not'' a whitespace, and <tt>d</tt> deletes the flagged lines (all lines that have no characters, or that have only whitespace characters).
+
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):
 
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):
Line 31: Line 31:
 
</pre>
 
</pre>
   
The <tt>e</tt> substitute flag means that no error is displayed if the pattern is not found. In the second command, <tt>\n</tt> in the search pattern finds newline, while <tt>\r</tt> in the replacement inserts a newline.
+
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==
 
==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 <tt>\u3000</tt> in a search pattern to specify the UTF-16 hex code for IDEOGRAPHIC SPACE. {{help|tag=/\%5D|label=/\&#93;}}
+
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=/\&#93;}}
   
 
In that case, the following will delete all empty lines, or lines that contain only combinations of space, tab or IDEOGRAPHIC SPACE.
 
In that case, the following will delete all empty lines, or lines that contain only combinations of space, tab or IDEOGRAPHIC SPACE.
Line 46: Line 51:
 
</pre>
 
</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 <tt>yl</tt> to yank the character. Then, enter the above <tt>:g</tt> 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).
+
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==
 
==See also==
Line 52: Line 57:
 
*[[VimTip242|Search across multiple lines]] Searching across multiple 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
 
*[[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 <tt>g//d</tt>
+
*[[VimTip213|Delete all lines containing a pattern]] Examples using <code>g//d</code>
 
*[[VimTip399|Fold away empty lines]] To temporarily hide multiple blank lines
 
*[[VimTip399|Fold away empty lines]] To temporarily hide multiple blank lines
   
 
==Comments==
 
==Comments==
 
<div style="margin-left:-9999px;">[http://www.vipsexshop.com.br Sex Shop][http://www.requintesexshop.com.br Sex Shop][http://www.sexshop.com.vc Sex Shop][http://www.belasexshop.com.br Sex Shop][http://www.belasexshop.com Sex Shop][http://blog.vipsexshop.com.br Dicas de Sexo][http://www.viplingerie.com.br Lingerie][http://www.vipcalcinhas.com.br Calcinhas][http://www.uniformesbr.com Uniformes Profissionais][http://www.uniformesbr.com Uniformes]</div>
 

Latest revision as of 01:21, 2 August 2017

Tip 72 Printable Monobook Previous Next

created 2001 · complexity basic · author Volker Duetsch · version 5.7


Use either of the following commands to delete all empty lines:

:g/^$/d
:v/./d

If you want to delete all lines that are empty or that contain only whitespace characters (spaces, tabs), use either of:

:g/^\s*$/d
:v/\S/d

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):

:%s/\s\+$//e
:%s/\n\{3,}/\r\r/e

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.

Another solution is to join consecutive lines found between non-blank lines.

:v/\S/,/\S/-j

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.

:g/^[ \t\u3000]*$/d

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.

:g/^[ \t#]*$/d

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).

See also[]

Comments[]