tags:

views:

461

answers:

3

I'd like to save the current single-repeat action (this is what happens when you press .), then run a macro, and then restore the . action (because macros usually change it).

Is this possible?

Alternatively, is it possible to write a macro without changing the current . action? I guess I could try to do everything in Command Mode, but I'm not sure if that's possible.

Background: I'm writing a file browser function (like command-T in TextMate, see http://github.com/mdemare/fuzzy_file_finder), and I want to make as few changes as possible to the user's environment, so the changes have to occur in the function. Currently, when you switch files using this function, your . action is overwritten, which is very unwelcome.

+4  A: 

You can record an action into a register to be used later. Press q followed by a register (a-z, A-Z, 0-9 or " are valid register identifiers), apply the desired command/actions and the press q to stop the recording. The command can be recalled by pressing @ followed by the register.

For more detailed instructions, see the complex repeat section of the Vim documentation.

NOTE: Unfortunately, the sequence qa.qu will not do exactly what you want since the . command will repeat the current last action and not the last action at the time the command was recorded.

Judge Maygarden
I know that, that's not what I meant at all. That's what I call a macro, and it changes the . action.
Michiel de Mare
Well, then I supposed you answer is no...
Judge Maygarden
+4  A: 

The only way I can think of to help you out: Remap '.' to save a history of actions, which you could then recall if needed. For ideas on these lines, see the repeat.vim plugin.

Caleb Huitt - cjhuitt
How does one retrieve the last action by remapping '.'? How is the last action stored/modified?
Judge Maygarden
@monjardin: I don't really know those answers, but I had come across the vim plugin that looked like it might be doing those things, so I mentioned it as a possible inspiration.
Caleb Huitt - cjhuitt
+7  A: 

In VIM you can create a macro that will execute any edits you would typically do in normal mode without disturbing the redo [.] functionality by wrapping those edits in a user defined :function and then executing that function with a :mapped key.

Example

The best way to see it is with an example. Suppose you want to add the text yyy 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 [.].

Here's how to do it:

  1. Open a new vim window and execute the following commands:

    :fu JL()
        normal Ayyy
        endfu
    :map <F2> :call JL()<Enter>
    
  2. Now add some text, let's say xxx, by typing Axxx<Esc>

  3. Now press the [F2] key and you should see xxxyyy

  4. Finally, press the [.] key and you should see xxxyyyxxx

Just what you wanted!

Why this works

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 [.] key, it stuffs those characters back into the keyboard buffer to re-execute them. Unfortunately, a simple q macro works the same way -- it stuffs characters into the buffer and by doing so overwrites the redo buffer. The :normal 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.

This all happens in eval.c and getchar.c in the VIM source code. Search for saveRedobuff to see what's going on.

JohnnyLambada
Excellent! Enjoy the karma!
Michiel de Mare