Skip to content

Instantly share code, notes, and snippets.

@alexsancho
Created December 8, 2011 19:01
Show Gist options
  • Select an option

  • Save alexsancho/1448065 to your computer and use it in GitHub Desktop.

Select an option

Save alexsancho/1448065 to your computer and use it in GitHub Desktop.
rvm after_cd hook that adds current path to directory stack.
#!/usr/bin/env bash
#####################################################################
# after_cd_push_pwd
#####################################################################
#
# rvm after_cd hook that adds current path to directory stack.
# It limits the stack size to MAX value and removes duplicated
# dirs.
#
# rvm overwrites the default popd command and by unknown reasons changes
# the default behaviour executing builtin command without args. That's
# why I need to call the bultin command to make this work.
#
# Combining the below keyboard mappings gives a nice way to navigate on current directory history
#
# "\e1": "cd ~+1\C-M" # go to nth dir on stack
# "\e2": "cd ~+2\C-M" # go to nth dir on stack
# "\e3": "cd ~+3\C-M" # go to nth dir on stack
# "\e4": "cd ~+4\C-M" # go to nth dir on stack
# "\e5": "cd ~+5\C-M" # go to nth dir on stack
# "\e6": "cd ~+6\C-M" # go to nth dir on stack
# "\e7": "cd ~+7\C-M" # go to nth dir on stack
# "\e8": "cd ~+8\C-M" # go to nth dir on stack
# "\e9": "cd ~+9\C-M" # go to nth dir on stack
#
# "\e0": "pushd -n +0 > /dev/null\C-M" # adds current dir to stack
#
# "\eb": "cd $(eval echo $(dirs -0))\C-M" # go to last dir on stack
# "\ez": 'cd -\C-Mls\C-M' # go to previous dir and ls
# "\ex": 'cd !$\C-Mls\C-M' # go to previous dir on last command from history and ls
# "\el": "dirs -l -p -v\C-M" # show dir stack
#
#####################################################################
local p i MAX=10 LEN=${#DIRSTACK[@]}
pushd -n "$(pwd)" > /dev/null || return 1
if [[ $LEN -gt 1 ]]; then
for ((i=2; i <= $LEN; i++)); do
p=$(eval echo ~$i)
if [[ "$p" = "$PWD" ]]; then
builtin popd -n +$i >& /dev/null
fi
done
fi
if [[ $LEN -gt $MAX ]]; then
for ((i=$LEN; i >= $MAX; i--)); do
builtin popd -n -$i >& /dev/null
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment