Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It really is quite ingenious. Let's break it down. The <code>ex</code> command</p> <pre><code>g/^$/xyzzy </code></pre> <p>will search for all empty lines and execute the <code>xyzzy</code> command (an arbitrary <code>ex</code> command) on each of them.</p> <p>The tricky bit here is that the <code>xyzzy</code> command in your case is <em>yet another</em> substitute command:</p> <pre><code>,/./-j </code></pre> <p>The <code>,/./-</code> specifies a range. This is of the form <code>&lt;start&gt;,&lt;end&gt;</code> and, because there's nothing before the comma, it assumes the current line (the one where you found the blank line) is the start.</p> <p>After the comma is <code>/./-</code> which means search for the next character (<code>.</code> means any character) then back up one line (<code>/./-</code> is short for <code>/./-1</code> since the one is implied if no value is given). You'll find that pattern <code>.</code> on the first non-blank line following the one you're operating on.</p> <p>In other words, the end of the range is the last blank line after or at the one you're currently operating on.</p> <p>Then you execute a join over that range.</p> <p>If the start and the end of the range were equal (only one blank line was in the section), join does nothing. If they're not equal, join will join them all up.</p> <p>That's the way in which it combines multiple blank lines into one.</p> <p>Lets look at an example (line numbers are not in the file):</p> <pre><code>1 Line 1 2 3 Line 3 4 Line 4 5 6 7 8 9 Line 9 </code></pre> <p>The <code>:g</code> command will find all blank lines and perform its operation on them (lines 2, 5, 6, 7 and 8).</p> <p>For line 2, <code>,/./-j</code> will set up a range from 2 to 2 (next <code>.</code> found on line 3 then subtract 1). A join on the range 2,2 does nothing.</p> <p>For line 5, <code>,/./-j</code> will set up a range from 5 to 8 (next <code>.</code> found on line 9 then subtract 1). A join on the range 5,8 will join all those lines together.</p> <p>I'm not <em>entirely</em> certain about this but I think the operation may not be performed on lines that disappear as part of an earlier operation. That's because it would make no sense to process lines that have been deleted earlier in the cycle.</p> <p>In other words, because lines 6 through 8 are deleted (combined with line 5), the global command doesn't operate on them after that. I base that on nothing more than the fact that the vim documentation states a two-pass algorithm, one to mark the lines, one to perform the operation.</p> <p>I <em>may</em> be wrong on that point (it wouldn't be the first time) but it's an implementation detail which doesn't affect the functionality.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload