The problem with turing complete editors

I was just editing a file with vim and was about to do the same few strokes repeatedly. I vaguely remembered something about recording macros, so I tried it: I pressed q-1 to start recording, did my steps (a yank, a movement, an insert, a paste, a movement), and finished the recording with q. Now I could repeat these steps with @-1.

I then noticed that I forgot something at the end (moving the curser to the beginning of the line). I re-started recording (q-1), thought “hey, maybe I can insert the old recording here” and pressed @-1. Surprisingly, that worked. I added the missing movement and finshed the recording with q.

When I then tried to recall these commands with @-1, the editor froze and I was wondering whether the network connection went down (I was working over SSH). But it wasn’t, and it dawned me that vim did not insert my old commands when re-recording the macro, but just inserted the call to the macro itself. My text editor was caught in a infinite loop...

Not too bad, I just killed vim, did not lose anything and everything was fine. But I think an editor, even vim, should somehow prevent such behaviour.


Maybe you should just open a new buffer, insert the macro with "1p, edit it and than yank it again with "1y :-).
#1 Anonymous am 2008-03-16T22:52:46+00:00
Yeah, I was bitten by this behavior many times. I just redo the whole thing. But it sucks, vim should be more clever.
#2 Ondrej (Homepage) am 2008-03-16T23:15:26+00:00
Hmm. Yes, nasty bug I'd say. VIM macros require too many keystrokes, too. On Cygnus Ed on my old Amiga, you could bind a macro to any single key, and just hold that key, watching the macro work it's magic like lightning. It was still prone to the same issue of getting the cursor position wrong etc., of course, but you do get used to that quickly enough, given an editor that encourages you to actually take advantage of macros in the first place.
#3 Lee am 2008-03-16T23:23:59+00:00
The macro name '@' refers to the last-executed macro. So after recording and initial invocation, if your vim hasn't gone down a recursive bottomless pit, you can just hold down '@' to keep on repeating the macro. Alas, you will repeat it only once for every two key events sent, so I guess it's still slower than Cygnus Ed...
#4 Jon (Homepage) am 2008-03-16T23:41:08+00:00
Too bad, emacs stops macro recording mode (C-x () when calling a macro (C-x e).
#5 Astro (Homepage) am 2008-03-16T23:34:04+00:00
Fun bug. FWIW Ctrl+C aborts the runaway macro successfully.

Sometimes it is more convenient to define a mapping instead of a macro -- then when it goes wrong, you can use the command line history to fix the definition without having to redo everything. Although hardcore vimmers do edit macros by pasting them into a buffer, editing, and yanking them back to the same register.
#6 Marius Gedminas (Homepage) am 2008-03-16T23:34:46+00:00
Obviously this needs to be fixed. The next generation of scriptable editor will be as powerful as possible but not turing complete; that's right, it will be dependently typed! :-P
#7 Luke Palmer (Homepage) am 2008-03-17T00:05:48+00:00
I'd suggest not using numeric registers for recording macros (or at all, really). Register 0 is used to store the latest yank/delete and registers 1-9 are used to store the previous 9 deletions. The only safe registers (as in not touched by Vim itself) are a-zA-Z. There are some plugins that may, annoyingly, clobber those registers though.
#8 jamessan am 2008-03-17T02:59:40+00:00
jamessan already told that numeric registers have a use.

What he neglected to say, though, is that you can *append* to registers a-z by using capital letters.

So after doing the first recording with "qa", you could add the movement to next line with "qA".
#9 Kari Oikarinen am 2008-03-17T07:05:29+00:00
Now that’s neat, I’ll keep that in mind. Thx!
#10 Joachim Breitner (Homepage) am 2008-03-17T10:33:37+00:00
Ah. You think vim should maybe solve the halting problem. What a good idea.

#11 John Hughes (Homepage) am 2008-03-17T11:22:31+00:00
This does actually have useful benefits. I've used this feature several times to do something to a line, move down a line, call current macro. When you run it, it will run down the file doing everything you wanted and then error out at the bottom when it can't move down any more.

I'm sure I could probably do the same thing in a better way, given that I wasn't as experienced with vim as I am now, but still occasionally useful.
#12 David Pashley (Homepage) am 2008-03-17T14:47:29+00:00

Have something to say? You can post a comment by sending an e-Mail to me at <mail@joachim-breitner.de>, and I will include it here.