Created
July 27, 2017 21:58
-
-
Save stephen-soltesz/b5a29582ef73aac434d452e292c28cdb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
cd_func () | |
{ | |
local x2 the_new_dir adir index | |
local -i cnt | |
if [[ $1 == "--" ]]; then | |
dirs -v | |
return 0 | |
fi | |
the_new_dir=$1 | |
[[ -z $1 ]] && the_new_dir=$HOME | |
if [[ ${the_new_dir:0:1} == '-' ]]; then | |
# | |
# Extract dir N from dirs | |
index=${the_new_dir:1} | |
[[ -z $index ]] && index=1 | |
adir=$(dirs +$index) | |
[[ -z $adir ]] && return 1 | |
the_new_dir=$adir | |
fi | |
# | |
# '~' has to be substituted by ${HOME} | |
[[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}" | |
# | |
# Now change to the new dir and add to the top of the stack | |
pushd "${the_new_dir}" > /dev/null | |
[[ $? -ne 0 ]] && return 1 | |
the_new_dir=$(pwd) | |
# | |
# Trim down everything beyond 11th entry | |
popd -n +11 2>/dev/null 1>/dev/null | |
# | |
# Remove any other occurence of this dir, skipping the top of the stack | |
for ((cnt=1; cnt <= 10; cnt++)); do | |
x2=$(dirs +${cnt} 2>/dev/null) | |
[[ $? -ne 0 ]] && return 0 | |
[[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}" | |
if [[ "${x2}" == "${the_new_dir}" ]]; then | |
popd -n +$cnt 2>/dev/null 1>/dev/null | |
cnt=cnt-1 | |
fi | |
done | |
return 0 | |
} | |
alias cd=cd_func | |
if [[ $BASH_VERSION > "2.05a" ]]; then | |
# ctrl+w shows the menu | |
bind -x "\"\C-w\":cd_func -- ;" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment