Created
February 18, 2011 23:31
-
-
Save znbailey/834600 to your computer and use it in GitHub Desktop.
Add this to your .bashrc or .bash_profile to set up "bookmarks" for your directories. When in a directory, type "s <name>" to store the current path as a bookmark with that name.
To move to a bookmark, type "g <name>". Tab completion also works!
To l
This file contains 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
# Add this to your .bashrc or .bash_profile to set up "bookmarks" for your directories. | |
# When in a directory, type "s <name>" to store the current path as a bookmark with that name. | |
# To move to a bookmark, type "g <name>". Tab completion also works! | |
# To list all bookmarks, type "l". | |
# | |
# Credit to: Karthick from http://www.huyng.com/archives/quick-bash-tip-directory-bookmarks/492/ | |
function s { | |
cat ~/.sdirs | grep -v "export DIR_$1=" > ~/.sdirs1 | |
mv ~/.sdirs1 ~/.sdirs | |
echo "export DIR_$1=\"$PWD\"" >> ~/.sdirs | |
} | |
function l { | |
source ~/.sdirs | |
env | grep "^DIR_" | cut -c5- | grep "^.*=" | |
} | |
# enable custom tab completion | |
shopt -s progcomp | |
# jump to bookmark | |
function g { | |
source ~/.sdirs | |
cd "$(eval $(echo echo $(echo \$DIR_$1)))" | |
} | |
# list bookmarks without dirname | |
function _l { | |
source ~/.sdirs | |
env | grep "^DIR_" | cut -c5- | grep "^.*=" | cut -f1 -d "=" | |
} | |
# completion command for g | |
function _gcomp { | |
local curw | |
COMPREPLY=() | |
curw=${COMP_WORDS[COMP_CWORD]} | |
COMPREPLY=($(compgen -W '`_l`' -- $curw)) | |
return 0 | |
} | |
# bind completion command for g to _gcomp | |
complete -F _gcomp g | |
# Usage: g [TAB] |
@stevepiercy I just fixed that by adding quotes around the stored directory paths as well as around the path passed to the cd command. not sure if both are necessary but it works now!
Thank you for the fast fix! That did the trick.
Nice, Zach. I could definitely use this to switch back and forth between project contexts.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How would I add support for directories whose names have spaces in them?