Vim: 231 Key Commands
Note that any ^ preceding a character means you should hold control while typing that character
mbomayiwo^V^R"^V^V^V^X ^V^["sy0dd`a@f ^["bc0yiwo^V^V^V^X^V^R"^V^[0l@sa^V^V^V^A-^V^[0f-"ayhdd`a@i ^["dc0mbyiwo^V^R"Exe@b^V^[0fel"ty2ldd`b@t ^["ec0wmbyiwo@f @d^V^[@z ^["fc0"xyiwwmbyiwocw^V^V^V^Rx^V^V^V^[@a@i @e^V^[@z ^["ic0IB0 B^V^R" ^V^OWB0 ^V^OA B0^V^[0*w"tyiWdd`b@t ^["zd0dd`bAe^[0@e
Steps so you can run this too!
- Copy the line into Vim
- Type
:s/\^V/<Ctrl-V><Ctrl-V>/g and press enter (the two s should give you a blue ^V)
- Type
:s/\^R/<Ctrl-V><Ctrl-R>/g and press enter (you should see blue ^Rs now)
- Type
:s/\^X/<Ctrl-V><Ctrl-X>/g and press enter (you should see blue ^Xs now)
- Type
:s/\^O/<Ctrl-V><Ctrl-O>/g and press enter
- Type
:s/\^A/<Ctrl-V><Ctrl-A>/g and press enter
- Type
:s/\^\[/<Ctrl-V><Ctrl-[>/g and press enter (this command is slightly different because I needed to escape the [)
- Type
0"yy$. The command is now stored in the y register
- Set up input on a line, and run with
@y
If someone knows a better way to share the command, please let me know. I know this is lengthy, but it's the best I could come up with.
Input/Output
The input string should be alone on any line in the file.
1 0 0 4 3 0 0 0 7
The output will simply overwrite the input string
1 2 3 4 3 4 5 6 7
Explanation
Algorithm
- Start at a non-zero number, make sure it's not the last number
- Find the next non-zero number
- Take their difference. If the answer is negative, you should decrement to repair the range, otherwise, increment to repair the range.
- Go back to the first character and replace each zero by incrementing/decrementing the previous number.
- Repeat until you get to the last character
Macros Used
@e - Check for end. The last number will have an e appended to it. If the number under the cursor has an e at the end, delete the e and stop execution. Otherwise, start an interpolation cycle with @b.
mbyiwo^R"Exe@b^[0fel"ty2ldd`b@t
@b - Begin interpolation cycle. Save the number under the cursor for a subtraction operation (@s) and then find the next non-zero term (@f)
mayiwo^R"^V^X ^["sy0dd`a@f
@s - Stores the subtraction command to use in @d. It is simply (val)^X where (val) is the number at the start of the interpolation step. This is set by the @b command.
@f - Find the next non-zero term. Write the current value to the unnamed register, then write @f @d on the next line, and then run @z. This will repeat this command if the number is a zero, and execute @d if it isn't.
wmbyiwo@f @d^[@z
@z - Conditional execute if unnamed register is 0. This command expects two commands on a new line in the format command1 command2. If the unnamed register is 0, command1 is executed, otherwise command2 is executed. Note that neither command can have any spaces in it.
IB0 B^R" ^OWB0 ^OA B0^[0*w"tyiWdd`b@t`
@t - Temporary command register. Stores various commands for a short time before executing them. Used primarily in if statements.
@d - Determine interpolation direction. Subtracts the first number in the sequence from the number under the cursor (using @s). If the result is negative, the interpolation must decrement so ^X is saved to @a. Otherwise, we should increment so ^A is saved to @a. Once this is saved, move back to the beginning of this interpolate cycle and run @i to actually interpolate
yiwo^V^X^R"^[0l@sa^V^A-^[0f-"ayhdd`a@i
@a - Stores either ^A or ^X to increment or decrement during the interpolation step. This is set by the @d command.
@i - Interpolate. Copy the number at the current location to @x and move to the next number. If that number is zero, replace it with @x and run @a to properly modify it up or down, then repeat this command. If the number isn't a zero, we have reached the end of this interpolation cycle. A new one should be started with this number as the beginning, so run @e to check for the end and run again.
"xyiwwmbyiwocw^V^Rx^V^[@a@i @e^[@z
@x - Temporary storage register. Used in the interpolate command (@i)
Breaking Down the keystrokes
mbo :Set b mark to current position and open a new line below to write macros
mayiwo^V^R"^V^V^V^X ^V^["sy0dd`a@f ^["bc0 :Write to @b and reset line
yiwo^V^V^V^X^V^R"^V^[0l@sa^V^V^V^A-^V^[0f-"ayhdd`a@i ^["dc0 :Write to @d and reset line
mbyiwo^V^R"Exe@b^V^[0fel"ty2ldd`b@t ^["ec0 :Write to @e and reset line
wmbyiwo@f @d^V^[@z ^["fc0 :Write to @f and reset line
"xyiwwmbyiwocw^V^V^V^Rx^V^V^V^[@a@i @e^V^[@z ^["ic0 :Write to @i and reset line
IB0 B^V^R" ^V^OWB0 ^V^OA B0^V^[0*w"tyiWdd`b@t ^["zd0 :Write to @z and reset line
dd`b :Delete this line and move cursor back to original line
Ae^[ :Append an e to the last number
0@e :Move to the beginning of the line and run
Instead of
0can our program take another value such asnull? – Downgoat – 2016-02-20T00:43:34.403@Downgoat No, missing numbers must be given as
0. – Doorknob – 2016-02-20T00:44:09.920