Type out Green Eggs and Ham in vim



The book Green Eggs and Ham by Dr. Seuss is famous for only having 50 different words in the entire story of the book.

Your task is to create a sequence of keystrokes that will result in the entire text of Green Eggs and Ham when applied to an empty file in the vim text editor, starting in normal mode with all default settings. Your keystrokes must not load any other files as part of their operation - all text must be generated within the editor itself.

Your final buffer must look exactly like this file, although it can remain in whichever mode it ends up being in (you don't have to have a final <Esc> to return it to normal mode).

Chorded keystrokes like <C-o> or using Shift to type a capital letter count as a single keystroke.

The sequence of the fewest keystrokes to achieve this goal wins.

Joe Z.

Posted 2013-12-09T03:43:21.800

Reputation: 30 589

If somebody wants to create the Emacs equivalent of this question, they can. I'm not familiar enough with Emacs to know if it would make for an interesting question at all. – Joe Z. – 2013-12-09T03:46:25.503

Also, "with all default settings" means that you aren't allowed to load settings from a .vimrc file. – Joe Z. – 2013-12-09T03:47:12.117

Based on your "canonical" insert-entire-file answer, I take it the mode you leave the editor in after the insertion doesn't matter. Maybe this should be explicitly specified to clarify (i.e. you don't have to <Esc> out from insert-mode). – FireFly – 2013-12-09T15:26:06.470

Yeah, you don't have to <Esc> out from insert mode, but you do have to start in normal. – Joe Z. – 2013-12-09T19:48:19.800

As I see it, this question is on the edge of being off-topic. It's not a codegolf, it's a vimgolf (there is a page for that, a good one. Anyone interested should take a look). I also find the text to be too long, making it very time consuming to golf. – shiona – 2013-12-09T20:41:07.750

You lied: it is 51 words. Now I have to start over. (kidding) – hildred – 2013-12-09T22:20:48.250

It's more than 51 if you count capitalizations as different words. – Joe Z. – 2013-12-09T22:45:10.720

Also, there is a small typo in there. I'm not sure if its meant as a test to see who used your reference text or not, so I won't point it out. – None – 2013-12-09T23:01:39.087

fixing the typo brings the unique word count to 50 – hildred – 2013-12-09T23:42:24.793

I didn't even notice the typo myself. Thanks for pointing it out, though - I found it and fixed it. – Joe Z. – 2013-12-10T00:12:30.287

Is invoking external commands using Vim keystrokes allowed, if the "Your keystrokes must not load any other files" rule is still followed? – Jordan – 2013-12-23T23:04:58.700

If those external commands are not available on a default Vim installation, then yes, it would violate the "loading any other files" rule. – Joe Z. – 2013-12-23T23:41:23.833



1864 keystrokes

Somebody could probably write some code that produces a more optimal solution, but this beats the reference case for now. This is a damn mess, and I am ashamed I did it by hand.

First, we enter insert mode.


Then all this

That ]! That ]!
IDNlike that ]!

Do Y like$?


WPY LTB or tB?

IZ LTB or tB.
IZ LTanywB.

WPY LTin a h@?
WPY LT=a m@?

|in a h@.
|=a m@.
|B or tB.

WPY ETin a box?
WPY ET=a fox?

Not_a box. Not =a fox.
Not_a h@. Not =a m@.
IZ ETB or tB.
IZ ETanywB.
IZ eat$.

WPY? CPY? In a car?
Eat them! Eat them! B they are.

IZ,X,_a car.

You may LT. You will see.
You may LTin a tree!

IZ,X_a tree.
Not_a car! You let me be.

|in a box.
|=a fox.
|in a h@.
|=a m@.
|B or tB.

CPY;wPY;on aQ?

Not_aQ! Not_a tree!
Not_a car! Sam! Let me be!

IZ,X,_a box.
IX,Z;=a fox.
:=a m@.
:in a h@.
:B or tB.

Say! In +? B_+!


WPY;cPY,_the rain?

IZ,X,_the rain.
Not_+. Not on aQ.
Not_a car. Not_a tree.
|;Sam;Y see.
Not_a h@. Not_a box.
Not =a m@. Not =a fox.
:B or tB.

You do not like$?

CPY;wPY;=a g^?

IZ,X;=a g^!

WPY;cPY;on a b^?

IX,Z;on a b^.
IWN,WN;=a g^.

:in the rain.
:on aQ.
Not_+! Not_a tree!
Not_a car! You let me be!
|in a box.
|=a fox.
:in a h@.
|=a m@.
|B or tB.


You do not LT. So Y say.
Try them! Try them! & Y may.
Try them and Y may;I say.

Sam! If Y will let me be,
I will try them. You will see.

Say! I like$!
I do! I LT;]!
& I wPETin a b^.
& I wPET=a g^.

!ETin the rain.
&_+. & on aQ.
&_a car. &_a tree.
They are so good;so good;Y see!

So I will ETin a box.
!ET=a fox.
!ETin a h@.
!ET=a m@.
!ETB and tB.
Say! I will ETanywB!

I do so like$!

Thank Y! Thank Y;]!

Hit escape to enter command mode. And unravel the compressed stuff. Order matters, as I've compressed some compressed text.

%s/Z/ wPnot/g
%s/X/ cPnot/g
%s/+/the dark/g
%s/IDN/I do not /g
%s/LT/like them /g
%s/ET/eat them /g
%s/WN/ will not/g
%s/$/ green eggs and ham/g
%s/P/ould /g
%s/!/And I will /g
%s/Q/ train/g
%s/_/ in /g
%s/=/with /g
%s/;/, /g


Posted 2013-12-09T03:43:21.800


you got the wrong substitution for eat them, it's backwards. and you can cut the leading colons by using ed mode which stays in command mode after commands. – hildred – 2013-12-09T23:31:42.570

@hildred I actually started with the full text, did a global replace and added the full text to compressed text to another file. Must have forgotten to reverse that one :( – None – 2013-12-09T23:42:09.103

If you replace so good;so good; with <Esc>2aso good;<Esc>a, you can save three keystrokes. Also, replace iThat ]! That]!<Enter> at the beginning with 2iThat ]! <Esc>xo for another three. – Joe Z. – 2013-12-10T01:16:54.613

Also, since DN doesn't appear elsewhere, you can remove the I from IDN and I do not in the replace statement with those two expressions in it. – Joe Z. – 2013-12-11T00:04:33.913

And, replace all instances of IDNlike$!<Enter>|;]. with 1 and input %s/1/IDNlike$!\n|;]./g to save about 20 keystrokes or so. – Joe Z. – 2013-12-11T00:07:00.987


3,470 keystrokes

i<entire text of file>

The text file is 3,469 characters long, so typing "i" to put it in insert mode and typing out each character individually will take 3,470 keystrokes.

Joe Z.

Posted 2013-12-09T03:43:21.800

Reputation: 30 589

Lol @ pure brute force – Cruncher – 2013-12-09T16:21:06.883

This is intended mostly as a last-place reference solution more than anything. – Joe Z. – 2013-12-09T20:07:59.913

yet it's currently the best contestant as well :-) (no, I'm not going to participate; sorry) – John Dvorak – 2013-12-09T20:14:51.123

You more of an Emacs person? :-) – Joe Z. – 2013-12-09T20:15:40.593