Skip to content

Instantly share code, notes, and snippets.

@imzjy
Created March 13, 2013 06:50
Show Gist options
  • Save imzjy/5149867 to your computer and use it in GitHub Desktop.
Save imzjy/5149867 to your computer and use it in GitHub Desktop.
The simulation of the d command in the Zsh. we got the initial script from http://aijazansari.com/2010/02/20/navigating-the-directory-stack-in-bash/
# An enhanced 'cd' - push directories
# onto a stack as you navigate to it.
#
# The current directory is at the top
# of the stack.
#
function stack_cd {
if [ $1 ]; then
# use the pushd bash command to push the directory
# to the top of the stack, and enter that directory
pushd "$1" > /dev/null
else
# the normal cd behavior is to enter $HOME if no
# arguments are specified
pushd $HOME > /dev/null
fi
}
# the cd command is now an alias to the stack_cd function
#
alias cd=stack_cd
# Swap the top two directories on the stack
#
function swap {
pushd > /dev/null
}
# s is an alias to the swap function
alias s=swap
# Pop the top (current) directory off the stack
# and move to the next directory
#
function pop_stack {
popd > /dev/null
}
alias p=pop_stack
# Display the stack of directories and prompt
# the user for an entry.
#
# If the user enters 'p', pop the stack.
# If the user enters a number, move that
# directory to the top of the stack
# If the user enters 'q', don't do anything.
#
function display_stack
{
dirs -v
echo -n "#: "
read dir
if [[ $dir = 'p' ]]; then
pushd > /dev/null
elif [[ $dir != 'q' ]]; then
d=$(dirs -l +$dir);
popd +$dir > /dev/null
pushd "$d" > /dev/null
fi
}
alias d=display_stack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment