Last active
June 12, 2022 00:59
-
-
Save albrow/10684515 to your computer and use it in GitHub Desktop.
Bash Bookmarks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
# A bookmarking implementation based on | |
# http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html | |
# and https://news.ycombinator.com/item?id=6229001 | |
# and https://gist.github.com/albrow/10684515 | |
# | |
# put this in ~/.bash_profile or ~/.bashrc | |
# | |
# USAGE: | |
# mark <name> | |
# bookmarks the current directory and identify it by <name> for future use | |
# j <name> | |
# jumps to the bookmarked directory identified by <name> | |
# marks | |
# show all bookmarks | |
# unmark <name> | |
# remove the bookmark identified by <name> | |
# | |
export MARKPATH=$HOME/.marks | |
# I renamed this to j because I'm lazy and it's 3 less letters. | |
function j { | |
cd -P $MARKPATH/$1 2> /dev/null || echo "No such mark: $1" | |
} | |
function mark { | |
mkdir -p $MARKPATH; ln -s "$(pwd)" $MARKPATH/$1 | |
} | |
function unmark { | |
rm -i $MARKPATH/$1 | |
} | |
function marks { | |
\ls -l $MARKPATH | tail -n +2 | sed 's/ / /g' | cut -d' ' -f9- | awk -F ' -> ' '{printf "%-10s -> %s\n", $1, $2}' | |
} | |
# include auto completions (press tab) | |
function _jump { | |
local cur=${COMP_WORDS[COMP_CWORD]} | |
local marks=$(find $MARKPATH -type l | awk -F '/' '{print $NF}') | |
COMPREPLY=($(compgen -W '${marks[@]}' -- "$cur")) | |
return 0 | |
} | |
complete -o default -o nospace -F _jump j |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment