Skip to content

Instantly share code, notes, and snippets.

@wrecklass
Created December 12, 2018 19:36
Show Gist options
  • Save wrecklass/b1ce38a39a2ccedb0d3046d30e5f83da to your computer and use it in GitHub Desktop.
Save wrecklass/b1ce38a39a2ccedb0d3046d30e5f83da to your computer and use it in GitHub Desktop.
CD Function and some aliases to make a cd history list easily usable.
#!/bin/bash
# Source this file in your bash_profile or bashrc as you like.
# Once sourced, cd to a few directories and then try `cd --` to see history.
# cd -# where # is the number of the directory you wish to return to.
# Works on macOS, Linux and Cygwin, although the aliases
# would have to be changed for different OSes.
alias cd="cd_func"
alias mkcd="_mkcd"
# Simple mnemonics for often used directories
alias cde="_cde"
alias cdf="_cdf"
alias cdg="_cdg"
alias cdl="_cdl"
alias cdo="_cdo"
alias cds="_cds"
PROGDIR_X86="${PROGDIR} (x86)"
# ctrl-w shows the menu
# bind -x "\"\C-w\":cd_func -- ;"
_cdl() {
if [ $# -gt 0 ]; then
cd_func "/Users/${USER}/Downloads/${*}"
else
cd_func "/Users/${USER}/Downloads"
fi
}
_cde() {
if [ $# -gt 0 ]; then
cd_func "/Users/${USER}/Desktop/${*}"
else
cd_func "/Users/${USER}/Desktop"
fi
}
_cdg() {
if [ $# -gt 0 ]; then
cd_func "/Users/${USER}/src/github/${*}"
else
cd_func "/Users/${USER}/src/github"
fi
}
_cdo() {
if [ $# -gt 0 ]; then
cd_func "/Users/${USER}/Documents/${*}"
else
cd_func "/Users/${USER}/Documents"
fi
}
_cdf() {
if [ $# -gt 0 ]; then
cd_func "/Users/${USER}/src/flagship/${*}"
else
cd_func "/Users/${USER}/src/flagship"
fi
}
_cds() {
if [ $# -gt 0 ]; then
cd_func "/Users/${USER}/src/${*}"
else
cd_func "/Users/${USER}/src"
fi
}
_mkcd() {
mkdir -p "$*"
cd_func "$*"
}
# function cd_func
# This function defines a 'cd' replacement function capable of keeping,
# displaying and accessing history of visited directories, up to 10 entries.
# source this file and try 'cd --'.
# acd_func 1.0.5, 10-nov-2004
# Petar Marinov, http:/geocities.com/h2428, this is public domain
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
#
# When argument is '-N' 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
# settitle "-$(basename $SHELL) : ${PWD/$HOME/'~'}"
[[ $? -ne 0 ]] && return 1
the_new_dir=$(pwd)
#
# Trim down everything beyond 11th entry
popd -n +11 2>/dev/null 1>/dev/null
# Limit the stack to 10 items
# 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment