Do it Faster With VIM!

This is just a quick-n-short entry.

Every time I do something fast, I get a bit of an adrenaline rush. This time I was able to save a ton of time by using vim. I am currently working on a little project for a student back in Mexico. She wanted to build a didactic game so learning would be more fun, so she sent me a few specifications for the game, and a list of questions and answers. Every question is formulated like this:

N.-) Some Question?

a) Ans. 1

b) Ans. 2

c) Ans. 3

There are three types of questions for 3 different types of subjects. The game is a simple racing game where a car encounters little markers along the road and for every marker a question pops up. Previously the player has selected which of the three types of questions they want. Based on that I decided to create an array to manage the questions and answers. The array would be like this:


array(
   type_1 =array(
      ...
   ),
   type_2 = array(
      ...
   ),
   type_3 = array(
      ...
   )
)

As you can see I created an array for each type of questions. This array contains another two arrays. One for the answers, and one for the questions, so the type arrays look like this:


type_N = array(
    questions = array(
      ...
   ),
   answers = array(
      ...
   )
)

This way it is easy to relate the questions to their answers, since the question in index 0, has answers in index 0 as well.

Since there are over 60 questions, creating the arrays would take too much time, and in this project time is really, really scarce. I decided to copy and paste all the question over to VIM. Luckily, the student is very organized and follow the same pattern for all the questions. So it was a matter of recording one macro that converts this:

N.-) Some Question?

a) Ans. 1

b) Ans. 2

c) Ans. 3

Into this:

questions[‘type_1’][‘questions’].push(‘Some Question?’);
questions[‘tyoe_1’][‘answers’].push(‘Ans 1’);
questions[‘type_1’][‘answers’].push(‘Ans 2’);
questions[‘type_1’][‘answers’].push(‘Ans 3’);

Then run this with a simple NN@a (NN was the number of times I had to run the macro). For questions type_2, and type_3, I just needed to change the index value from type_1 to type_2, or type_3.

I was done in less than 5 minutes. And some people thing VIM is stupid.