Autojump

In line with my recent posts on Bash, Bash options, and other command line tools, I thought I might do a post on another CLI plugin I use regularly: autojump.

Autojump is a way to navigate the filesystem faster. It works by maintaining a database of directories you commonly cd into. Once you’ve visited a directory, you can return to it via the autojump shortcut, saving having to type or complete the full path.

1
2
3
4
5
$ cd /foo
$ cd ~
$ j foo
$ pwd
/foo

Here we change into the /foo directory and then into our home directory. We then run the autojump convenience shortcut, j, and return to the foo directory.

Installing autojump

Autojump require Python 2.7+ and ships with most of the major distributions, for example on Ubuntu.

1
$ sudo apt install autojump

Or on OS X via Homebrew.

1
$ brew install autojump

It supports most of the major shells, Bash, Zsh, Fish, etc and needs to be sourced by your shell before it’ll function. For Fish I would add this to my configuration.

1
2
3
4
5
6
begin
    set --local AUTOJUMP_PATH /usr/share/autojump/autojump.fish
    if test -e $AUTOJUMP_PATH
        source $AUTOJUMP_PATH
    end
end

And then source it manually or launch a new shell to automatically source it.

Using autojump

We’ve already seen autojump’s basic mode, returning to a previously visited directory. In addition to changing to a directory we can also change to a child directory of the current directory, using jc:

1
2
$ j foo
$ jc bar

We can open our default file manager in the path requested too, using the jo shortcut.

1
$ jo foo

Or to a child directory.

1
$ jco bar

Autojump maintains a weighted database of directory entries, so if you have a name collision, then you can narrow it down by specifying further directory information.

Tip
You can see the database via the j --stat command. You can also manually add directories or change weights if needed, see j --help for options.

Let’s say you have /home/james/src/ and /home/james_work/src/ in your database like so:

1
2
30   /home/james/src
10   /home/james_work/src

Using j autojump would leap into the higher weighted directory, /home/james/src/, but if we want the lower weighted directory we can type:

1
$ j w src

And you’ll jet off to the lower weighted: /homes/james_work/src.

As always, hope someone finds this useful!