Skip to content

Instantly share code, notes, and snippets.

@vannell
Last active December 21, 2015 06:08
Show Gist options
  • Save vannell/6261832 to your computer and use it in GitHub Desktop.
Save vannell/6261832 to your computer and use it in GitHub Desktop.
Jump around your folders
#!/bin/bash
#This is free adaptation of an idea exposed at http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html
#Include this directly in your .bashrc or source it
#Examples:
# [very/deep/path] $ mark deep
# [other/location] $ jmp deep
# $ mark list #will list all your marks
# $ unmark deep
# Quick way to mark a temporary location without naming it
# [deep/deep/folder] $ mark
# Go to the last location you save with 'mark'
# [other/folder] $ jump
# Use your marks as location argument
# $ cat $(path_to deep)/test.txt
MARKPATH=$HOME/.marks
mark(){
mkdir -p $MARKPATH
if [[ $1 = "list" || $1 = "ls" ]]; then
ls -l $MARKPATH
elif [ -z $1 ]; then
[ -e $MARKPATH/last ] && rm -f $MARKPATH/last
ln -s -T "$(pwd)" $MARKPATH/last
else
ln -s -T "$(pwd)" $MARKPATH/$1
fi
}
unmark() {
if [ -e $MARKPATH/$1 ]
then
echo "[+] Drop $1"
rm $MARKPATH/$1
fi
}
jmp() {
if [[ -z $1 && -e $MARKPATH/last ]]
then
cd -P "$MARKPATH/last"
else
if [ -e $MARKPATH/$1 ]
then
cd -P $MARKPATH/$1
else
echo "[x] Unknown mark, I am not going to jump into the void"
fi
fi
}
#expand mark absolute path
path_to() {
if [ $1 ]
then
readlink -f "$MARKPATH/$1"
fi
}
alias j='jmp'
alias m='mark'
alias marks='mark list'
#copy this completion rule in your /etc/bash_completion.d/marks_completion
_complete_marks() {
local cur opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
opts=$(find "$MARKPATH" -type l -printf '%f ')
if [[ $COMP_CWORD == 1 ]]
then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
}
complete -F _complete_marks jmp j unmark
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment