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.
In VIM you can create a macro that will execute any edits you would typically do in
normalmode without disturbing the redo[.]functionality by wrapping those edits in a user defined:functionand 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
yyyto 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:
Open a new vim window and execute the following commands:
Now add some text, let’s say
xxx, by typingAxxx<Esc>Now press the
[F2]key and you should seexxxyyyFinally, press the
[.]key and you should seexxxyyyxxxJust 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 simpleqmacro works the same way — it stuffs characters into the buffer and by doing so overwrites the redo buffer. The:normalcommand 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.candgetchar.cin the VIM source code. Search forsaveRedobuffto see what’s going on.