Skip to content

Instantly share code, notes, and snippets.

@harith
Last active September 18, 2022 07:50
Show Gist options
  • Star 55 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save harith/8340924 to your computer and use it in GitHub Desktop.
Save harith/8340924 to your computer and use it in GitHub Desktop.
Utilities to let you easily reach frequently visited but deeply nested directories.
# Utilities for quickly accessing frequently used directories in bash.
# Usage:
# $ cd /path/to/project/src/
# $ mark code # Will create a new shortcut.
# # Becomes interactive if a shortcut already exists
# # m is an alias for mark. You can also `m code`
#
# $ code # From now on, running this anywhere in the shell
# # will put you in /path/to/project/src/code
#
# $ unmark code # Will remove the symlink and is interactive
# # u is an alias for unmark. You can also `u code`
SHELLMARKSDIR="$HOME/.shellmarks"
mkdir -p $SHELLMARKSDIR
function mark_alias { alias $(basename $1)="cd -P $1"; }
function mark { # Mark a directory
symlink=$SHELLMARKSDIR/$1
ln -ivns "$(pwd)" $symlink && mark_alias $symlink
}
alias m=mark
function unmark { # Remove a mark
symlink=$SHELLMARKSDIR/$1
rm -iv $symlink
if [ ! -f $symlink ]; then
unalias $1
fi
}
alias u=unmark
function shellmarks { # List all existing marks
LINK_COLOR=$'\e[1;35m'
RESET_COLOR=$'\e[0m'
for symlink in $SHELLMARKSDIR/*; do
test -L $symlink && echo "${LINK_COLOR} $(basename $symlink) ${RESET_COLOR} -> $(readlink $symlink)"
done
}
for symlink in $SHELLMARKSDIR/*; do # load all existing symlinks as aliases
mark_alias $symlink
test -L $symlink && ( test -e $(readlink $symlink) || rm $symlink ) # remove symlinks if source does not exist
done
@Wilfred
Copy link

Wilfred commented Jul 11, 2014

There's a utility called fasd that provides a superset of both z and autojump functionality:

https://github.com/clvv/fasd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment