Carefully choose how you take input.
V can take input two different ways: input and args. It can even take both at the same time! Choosing how you take input can sometimes save you many bytes. Here is how the two input formats work:
The input is the text that is loaded into the buffer. Generally, this is more convenient for string inputs.
The args are stuffed into the first n registers, depending on how many args are given. For example, if 5 args are given, these will be loaded into registers a-e. Since you can run these registers with @<reg>
, these are generally more convenient for numeric input.
Lets make a hypothetical example. Our hypothetical challenge is:
Given a string s, and an integer n, output s n times, separated by a newline.
In vim, you can only take input into the buffer. So you'd need to take the input on two lines, for example
n
s
And run
DJY@-pdd
The D
to delete n into register '-', the J
to remove the extra newline, and then @-p
to paste s n times, and dd to take care of the extra copy. In V, you can take s as input, and n as an arg. Then you could shorten this too:
@apdd
of course, there's still a few ways to shorten this even more. For example, Ä
makes n copies, rather than pasting n times. And À
, or <M-@>
will cycle through all of these args, so the first time it's called it is equivalent to @a
. So you can simply do:
ÀÄ
The À
will command will also loop back once you go through all of the args. So if you have 3 arguments, running À
repeatedly will run
@a
@b
@c
@a
@b
@c
@a
...
And what happens when you go over
@z
? – Erik the Outgolfer – 2017-06-15T09:28:58.520@erikthegolfer I don't know. I've never used that many args. – James – 2017-06-15T14:31:10.927
what unprintable put that in my inbox? – Erik the Outgolfer – 2017-06-15T14:33:29.137