Have you ever hit alt + backspace while in a zsh session, intending to delete just the last part of a path, or a word, or an identifier, but ended up deleting far more than that?

This is probably because you are using the default value for $WORDCHARS.

The default value for $WORDCHARS is

*?_-.[]~=/&;!#$%^(){}<>

i.e. pretty much everything and the kitchen sink. Usually, therefore, you will want to remove characters which you don’t want to be considered parts of words

Each one of those characters is considered part of a “word”, and when you hit alt + backspace, you are signalling your intention to the shell to delete a “word”.

This is why if you have a path like /home/user1/projects/some-project/src/module1 on your terminal input line, you’ll end up deleting the whole path with alt + backspace instead of just module1.

I think that most developers will probably benefit from removing the following characters from $WORDCHARS

  • / (we usually want to delete elements of a path, rarely the entire path)
  • - (cli subcommands that are separated-like-this usually require replacing the last N elements to do something similar-but-different)
  • _ (same as above, and also useful when grepping variables_like_this or LIKE_THIS)
  • . (often used as a module delimiter, also useful when you just want to change the filetype)

This leaves us with our sensible $WORDCHARS value for most developers:

*?[]~=&;!#$%^(){}<>

You can export this new $WORDCHARS value in your .zshrc files on the various machines you use.

Alternatively, if you manage your machines with NixOS, you can set this as the new default for all of them using a common piece of home-manager configuration:

programs.zsh.initExtra = ''
    WORDCHARS='*?[]~=&;!#$%^(){}<>'
'';

If you have any questions you can reach out to me on Mastodon and Twitter.

If you’re interested in what I read to come up with solutions like this one, you can subscribe to my Software Development RSS feed.

If you’d like to watch me writing code while explaining what I’m doing, you can also subscribe to my YouTube channel.