2013-11-21

Reverse Simple Deletion

It's hard to say if a VimGolf challenge is "hard" or "easy". Every challenge is easy if you waste enough strokes. In Reverse Simple Deletion, just one stroke separates an easy 12 from an unreasonably difficult 11.

Reverse Simple Deletion

a.^H^M^[24@.ZZ for 11.

This is an optimized variation on the far more obvious qqa^M^[q24@qZZ for 12.

The trick is the inserted text, .^H^M. It's simultaneously the text to be inserted after the current letter, and the macro that will repeat the insert.

For the insert itself, you could just use ^M. The .^H is useless filler that cancels itself out by inserting a character and backspacing it.

For the macro, you could just use .. The ^H^M is useless filler that cancels itself out by moving the cursor back a character (and up a line, since ^H runs from the beginning of the line), and down a line, to where it started.

The ^H is superfluous in both contexts, but it's essential to bind the two together. It cancels the . in the insert, so you don't leave garbage in the file, and it cancels the ^M in the macro, so it doesn't run from the last line and fail the macro.

Note that you can't use 25a^M^[, because that just puts 25 line breaks between a and b. Similarly, you can't use a^M^[24., because that puts 24 line breaks between b and c. Using . with a number argument doesn't mean to repeat . that many times; it means run the last command with a changed numeric argument. 2. and .. are not the same thing. You could use . repeats to finish this challenge, but it would look like ........................; that's why you need a macro.

Read the manual

  • :help 'whichwrap': The 'nocompatible' default of this setting lets you use Backspace and Space to ignore line breaks while moving the cursor left or right. This is the reason ^H moved the cursor up a line.

Similar challenges

1 comment: