Linux toolchest: xd

If you’ve ever seen my dotfiles, you know I love short syntax. It allows me to type in commands faster and as a result get my robot-do-things-mode tasks done a tad faster. One of the tools that has arguably saved me thousands of keypresses is xd.

Extra fast directory changer

That’s what xd means. And once you get used to it, you never want to use anything else! xd allows you to type in the initial characters of a directory and it’s subdirectories, and it’ll either give you the right directory at once, or allow you to choose between several matching directories.

So, first things first. Paste this in your ~/.bashrc or ~/.bash_functions or whatever you use to store your bash functions.

1
2
3
c() {
cd "$(command xd -g "$@")";
}

I use c as the command. I like to think of it as an even shorter (and smarter) version of cd. You might prefer to call it something else, but as you’ll be cding through directories your entire linux life, you’ll probably want to use a single character as the function name.

Now imagine you’re working on something but you quickly need to fix something in another directory:

1
2
3
4
5
kapott@demeter:~/work/dotfiles$ c ulb
kapott@demeter:~/usr/local/bin$
(...do stuff..)
kapott@demeter:~/usr/local/bin$ cd -
kapott@demeter:~/work/dotfiles$

That was fun, right? There are a few gotcha’s you’ll need to remember though.

The gotcha’s

By default, xd will look in your home directory and the root directory for the directories matching your query. So xd ulb will look at /usr/local/bin but it will also look at ~/unbilled/lollipopcompany/bratwurst_project for example.

If you want to specify where xd should look, prefix your search.

If you prefix with /, the search will only start from the root directory.
If you prefix with ., the search will only start from the home directory.
And if you prefix with 0, the search will start from the directory you’re currently in, $PWD.

Example:

1
2
3
4
5
kapott@demeter:~/work/dotfiles$ c /ulb
kapott@demeter:~/usr/local/bin$ c .ulb
kapott@demeter:~/unbilled/lollipopcompany/bratwurst_project$ cd -
kapott@demeter:~/usr/local/bin$ c 0ulb
kapott@demeter:~/usr/local/bin/unlikely/located/binaries/$

Now, go forth and have fun navigating through your filesystem with blazing speed.