Skip to content

Instantly share code, notes, and snippets.

@romannurik
Created February 27, 2010 22:12
Show Gist options
  • Save romannurik/317004 to your computer and use it in GitHub Desktop.
Save romannurik/317004 to your computer and use it in GitHub Desktop.
cdg directory navigation scripts + bash completion
# cdg command
# A smarter cd, works well with up: http://daniele.livejournal.com/76011.html
# Help: cdg -h
# Show all links: cdg
# Delete link: cdg -d foo
# Clear all: cdg -c
# Store link to working directory: cdg -w foo
# Go to link: cdg foo
function cdg() {
mkdir -p ~/.cdg
dir=""
if [ -z "$1" ]; then
ls -Al ~/.cdg
elif [ "-h" == "$1" ]; then
echo 'Usage: cdg [-h | <name> | -w <name> | -d name | -c]' >&2
return
elif [ "-c" == "$1" ]; then
for name in $(ls ~/.cdg); do
unlink ~/.cdg/${name}
done
elif [ "-w" == "$1" ]; then
if [ -z "$2" ]; then
echo 'Usage: cdg -w <name>' >&2
return
fi
ln -s $(pwd) ~/.cdg/$2
elif [ "-d" == "$1" ]; then
if [ -z "$2" ]; then
echo 'Usage: cdg -d <name>' >&2
return
fi
unlink ~/.cdg/$2
else
cd -P ~/.cdg/$1
fi
}
#!/bin/sh
# Bash completion for cdg
function _cdg() {
mkdir -p ~/.cdg
local word=${COMP_WORDS[COMP_CWORD]}
local names=$(ls ~/.cdg/)
COMPREPLY=($(compgen -W "${names}" -- "${word}"))
if [ -z ${COMPREPLY} ]; then COMPREPLY=""; fi
}
complete -o nospace -o default -F _cdg cdg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment