Skip to content

Instantly share code, notes, and snippets.

@kidpixo
Created February 5, 2021 12:55
Show Gist options
  • Save kidpixo/3f6afc41e90d388b388de8c58b6f8b70 to your computer and use it in GitHub Desktop.
Save kidpixo/3f6afc41e90d388b388de8c58b6f8b70 to your computer and use it in GitHub Desktop.
alias ..='cd ..'
alias ...='cd ../../../'
################################################
# [bash - aliasing cd to pushd - is it a good idea? - Unix & Linux Stack Exchange](https://unix.stackexchange.com/a/4291/187865)
# You can then navigate around on the command-line a bit like a browser.
# - cd changes the directory.
# - back goes to the previous directory that you cded from.
# - flip will move between the current and previous directories without popping them from the directory stack.
pushd()
{
# recreate cd standard behaviour with no arguments: go to ~
if [ $# -eq 0 ]; then
DIR="${HOME}"
else
DIR="${1%/}" # bash only!!
fi
builtin pushd "${DIR}" > /dev/null
# Remove paths duplication in DIRSTACK
dedup
#echo -n "DIRSTACK: "
#dirs -l
}
pushd_builtin()
{
builtin pushd > /dev/null
echo -n "DIRSTACK: "
dirs
}
popd()
{
builtin popd > /dev/null
echo -n "DIRSTACK: "
dirs
}
alias cd='pushd'
complete -o nospace -o plusdirs -F _fzf_dir_completion cd
alias back='popd'
alias flip='pushd_builtin'
alias pu='pushd'
alias po='popd'
alias di='dirs -v -p -l'
alias dif='dirs_fzf' # my custom dirs+fzf script
# deduplicate DISRSTACK , see removing duplicates from pushd/popd paths https://unix.stackexchange.com/a/288532
dedup(){
declare -a new=() copy=("${DIRSTACK[@]:1}")
declare -A seen
local v i
seen[$PWD]=1
for v in "${copy[@]}"
do if [ -z "${seen[$v]}" ]
then new+=("$v")
seen[$v]=1
fi
done
dirs -c
for ((i=${#new[@]}-1; i>=0; i--))
do builtin pushd -n "${new[i]}" >/dev/null
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment