Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In VIM you can create a macro that will execute any edits you would typically do in <code>normal</code> mode without disturbing the redo <code>[.]</code> functionality by wrapping those edits in a user defined <code>:function</code> and then executing that function with a <code>:map</code>ped key. </p> <h1>Example</h1> <p>The best way to see it is with an example. Suppose you want to add the text <code>yyy</code> to the end of the current line every time you hit the F2 key, but you don't want this to interfere with the redo command <code>[.]</code>.</p> <p>Here's how to do it:</p> <ol> <li><p>Open a new vim window and execute the following commands:</p> <pre><code>:fu JL() normal Ayyy endfu :map &lt;F2&gt; :call JL()&lt;Enter&gt; </code></pre></li> <li><p>Now add some text, let's say <code>xxx</code>, by typing <code>Axxx&lt;Esc&gt;</code></p></li> <li><p>Now press the <code>[F2]</code> key and you should see <code>xxxyyy</code></p></li> <li><p>Finally, press the <code>[.]</code> key and you should see <code>xxxyyyxxx</code></p></li> </ol> <p>Just what you wanted!</p> <h1>Why this works</h1> <p>This works because of the nature of the way VIM executes the redo command. VIM keeps track of the characters of a command as you type it. When you press the <code>[.]</code> key, it stuffs those characters back into the keyboard buffer to re-execute them. Unfortunately, a simple <code>q</code> macro works the same way -- it stuffs characters into the buffer and by doing so overwrites the redo buffer. The <code>:normal</code> command does this as well, but when placed inside a user defined function we get around this limitation because the code that executes the user defined function saves and restores the redo buffer during the user defined function.</p> <p>This all happens in <a href="http://code.google.com/p/vim/source/browse/src/eval.c" rel="nofollow noreferrer"><code>eval.c</code></a> and <a href="http://code.google.com/p/vim/source/browse/src/getchar.c" rel="nofollow noreferrer"><code>getchar.c</code></a> in the VIM source code. Search for <code>saveRedobuff</code> to see what's going on.</p>
 

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