natechoe.dev The blog Contact info Other links The github repo

The absolute power of Vim

Here's a simple piece of Java code:

enum Direction {
    UP,
    RIGHT,
    DOWN,
    LEFT
}

static Direction rotateClockwise(Direction d) {
    switch (d) {
    case UP: return Direction.RIGHT;
    case RIGHT: return Direction.DOWN;
    case DOWN: return Direction.LEFT;
    case LEFT: return Direction.UP;
    default: return d;
    }
}

There are cleaner ways of representing the cardinal directions - in fact I never ended up using this code and instead used a vector representation - but let's just roll with this.

We might also want a rotateCounterClockwise function which looks like this:

static Direction rotateCounterClockwise(Direction d) {
    switch (d) {
    case RIGHT: return Direction.UP;
    case DOWN: return Direction.RIGHT;
    case LEFT: return Direction.DOWN;
    case UP: return Direction.LEFT;
    default: return d;
    }
}

We can write this function by copying over the rotateClockwise function, selecting our switch cases, and running this Vim command:

:'<,'>norm $F.ldt;0elli.^[0f.P0f.xdt:$F.p

Note that the ^[ sequence is actually typed by pressing ctrl-v and then pressing escape.

I managed to get this to work on the first try. I'm not going to explain how it works, I just felt like this magic had to be archived.