Cleanup of zsh-dwim

| Comments

I went ahead and cleaned up zsh-dwim (Do What I Mean) quite a bit. If you haven’t been following along, zsh-dwim attempts to predict your intended next command when you hit control-u. I also recorded a short screen cast demonstrating some of what happens when you hit control-u.

Removing the Perl dependency

I decided that I may as well bite the bullet and move all the transformation logic to zsh from the original Perl “helper” script. Early on, I was worried that zsh code would be too ugly, but I have too many ideas that would be hard to implement with the code base split up like that.

All the transformation logic now uses zsh and sed. I was hoping to use zsh’s built-in substitution abilities, but they were lacking some important features. The “scp to mv” transformation looked like it would have been really ugly without sed.

Cleaning up the if/then spaghetti

When I was testing this idea I only had two transformations, so a pair of if/then statements didn’t seem like a problem. Now that there are ten, it is starting to get ugly.

I ended up moving all the transformation definitions into a zsh hash and I created a little helper function to add new definitions to that hash. When you call zsh-dwim, it loops over that hash testing for relevant transformations. This means we can now add transformations anywhere in our shell configuration.

Adding a transformation looks like this now:

  _dwim_add_transform '^mkdir ' \
    'BUFFER=$(echo $BUFFER | sed -re "s/^mkdir /cd /")'

What’s coming down the road?

There’s still a lot of repetitive nonsense to eliminate. I need to factor out all those “BUFFER=$(echo $BUFFER | … )” bits. They’re repeated in every single transformation.

I’d also like to make use of more data in deciding which transformation to run. For instance, it might only make sense to call ssh-keygen if the ssh command fails.

The zsh-dwim repository is ready to be pulled into Prezto as a git submodule. It can probably be used as a plugin with oh-my-zsh pretty easily, and the init.zsh file can probably be sourced right into plain old zsh without, hopefully, any problem at all.

Comments