Skip to content
Dec 27 / Derek Reeve

Intelligent word boundaries in zsh

A couple years ago, after several years of constant shell usage, I found myself wanting a more efficient way to move backward through paths.  As I work from the shell, I undoubtedly end up working with a long path, a portion of which I need to use more than once. Take, for example, a Ruby on Rails project:

dreeve@puggle ~/projects/ $ ls cdreaper2/app/views/layouts

To run ls on another directory (cdreaper2/app/controllers, for example) you would normally have to backspace all the way to the shared parent directory. This is cumbersome for me: I hate holding down the backspace key. bash helps alleviate the overuse of the backspace key by allowing the user to use ctrl+w to clear entire words. However, it isn’t easy to configure the bash shell to clear words up to a forward slash.

So, I started using zsh instead. Since it’s already installed in OS X, you can set your user’s shell to zsh quite easily via the Accounts interface.

Adding

local WORDCHARS=${WORDCHARS//\//}

to your .zshrc file (found or created in your home directory) will tell zsh to use forward slash as a boundary character.

Then, when you have to move backward through a path, ctrl+w will remove the rightmost part of the path. Given the path we started with:

dreeve@puggle ~/projects $ ls cdreaper2/app/views/layouts |

Hitting ctrl+w twice will give you:

dreeve@puggle ~/projects $ ls cdreaper2/app/ |

making moving between directories in a heavily-nested projected easy.