A blog on Computer Science, Security, Programming, and more...

HeapSpray Blog » Programming » View Post

13
Apr
2015

FizzBuzz in Vim

Written by Matt

Can you write FizzBuzz using only your editor's macro language? In vim you can. I found out about vim's '-s' option recently, something that will read a file and interpret the bytes as raw key commands. You can create 'strings' that feed into vim and execute macros or any vim command.

As an example, here's a string that generates a FizzBuzz solution inside of vim's buffer - the actual 'program' produced by printf is only 69 bytes! Run the below in a terminal and vim will open with FizzBuzz's output

vim -s <(printf "i1 \x1bqqYp\x01q98@qkqqAFizz\x1b3kq32@qGqqABuzz\x1b5kq19@q:%%s/ $\x0d:%%s/^[0-9]* //g\x0d")

It defines macros to create and increment the integers, then creates a macro to skip lines and add Fizz, does the same for Buzz on line multiples of 3 and 5 respectively, and then uses sed to clean up the output.

Here's the snipped output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
[...]
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz
Topic: Programming tags: linux, fun, vim, macros
  • Name and Email fields are optional
  • Your email will not be public, only the administrator can see it
  • You are rate limited to one comment for every 10 minutes