paint-brush
HyperJump — A Quicker Way to CD in Bashby@x0054
527 reads
527 reads

HyperJump — A Quicker Way to CD in Bash

by BogdanSeptember 23rd, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Sat, Aug 1, 2015

Company Mentioned

Mention Thumbnail
featured image - HyperJump — A Quicker Way to CD in Bash
Bogdan HackerNoon profile picture

Sat, Aug 1, 2015

This spring I was working with a startup that wanted to use CentOS as their main development platform. Though this wasn’t my role exactly, as it is with startups, you pitch in where you can. So I also became the defacto server admin. Problem was, I am used to the Debian derivatives of Linux, so CentOS was a bit of a switch for me, especially in their default directory structure. Not a big deal, but working over a high latency connection, autocompletion wasn’t working so well. To avoid the headache of remembering new directory structure, and in general to speed up directory navigation, I decided to make a little BASH script to bookmark all the directories I commonly use on a system. Thus, HyperJump was borne.

I have used pushd and popd as well as the autojump utility, but for my needs I wanted something else. My goals were:

  • quickly jump to random directories
  • persistent storage from session to session
  • ability to nickname directories so the nickname does not necessarily have to be the name of the directory
  • autocomplete for the nicknames
  • quick and easy to use
  • little to no dependencies, so it’s easy to install on any *NIX system

With that in mind I made HyperJump, a simple and quick bookmark tool for bash and zsh shells. Actually, it might work on other shells, but I only tested it on bash. A zsh patch was provided by daveFNbuck on GitHub. I tested it on OSX, Debian, CentOS, Ubuntu, and Slackware. I am sure it will run on basically anything with bash or zsh support.

In short, HyperJump is a bookmark tool and a kind of memory aid. You bookmark directories by using the jr command (for Jump Remember), delete bookmarks with the jf command (for Jump Forget), and jump to directories with jj command (for Jump Jump, I guess). If you have Dialog utility, a UNIX CURSES utility for presenting Text Based GUI, it will also show you a list of all bookmarks, and you can select the one you would like to jump to.

HyperJump is almost pure BASH. I originally intended it to be 3 separate bash scripts, but that proved to be problematic. There is no simple mechanism by which you can change the working directory of one bash session from another one. So, when you run jj, if it starts in a separate bash instance, it will not be able to change the working directory of the shell it was lunched from. The solution was to write the script as a function, add it to bashrc, and have it execute inside the active bash instance directly. In any case, having all 3 scripts and the supporting logic collected in one file actually turned out to be more elegant.

Download hyperjump script and place it somewhere on your system, such as ~/bin/hyperjump. Add the following line to your .profile.bashrc or .zshrc file:

source /location/of/hyperjump

Optional: To get the list of all the Bookmarks in a nice looking menu window, you need a unix utility called dialog. You can install it via yum, apt-get, homebrew, ports, and others like so:

# On CentOS or Another RedHat Derivativesudo yum install dialog# On Ubuntu or Another Debian Derivativesudo apt-get install dialog# On OS Xbrew install dialogsudo port install dialog

HyperJump consists of 3 command line commands (functions).

  • jr — Remember Jump. Bookmarks current directory. Run jr nickname to add current directory, or just run jr and use the interactive mode.
  • jf — Forget Jump. Deletes the current directory from the bookmarks. Run jf while in a directory you want forgotten or jf nickname to forget a specific nickname.
  • jj — Jump to a bookmark location. Run jj nickname to jump to a location or just jj to get a list of all bookmarks. You can also run jj nickname command to jump to a location and than run the command specified with “./” as the first argument. So, for instance, you can run jj myProject open subl on OSX to jump to the myProject directory and open the myProject directory in Finder and Sublime Text.

All of the commands have autocomplete. Both jj and jf will autocomplete with nicknames of bookmarked locations. The jr command will autocomplete with the basename of the current directory. After the first argument, jj will autocomplete with list of available system commands (programs).

Examples:

# Remember current directoryjrjr MyDir# Forget current directoryjf# Forget another directoryjf AnotherDir# Jump to a Directoryjjjj MyDir # Jump to a directory and open the directory in another program(s)jj MyDir openjj MyDir open subl tm

Final Thoughts

I hope HyperJump can be as useful to others as it is to me. HyperJump is released under the MIT License. Use it, love it, fork it, make changes, send pull requests. Enjoy!

Originally published at sdbr.net.