Skip to content

Instantly share code, notes, and snippets.

@sfaleron
Last active December 1, 2017 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfaleron/25d46ab5125016d93075 to your computer and use it in GitHub Desktop.
Save sfaleron/25d46ab5125016d93075 to your computer and use it in GitHub Desktop.
Quickly access directories from anywhere, with a shell function and Python script
from __future__ import print_function
# The definitions are in the file "mydirs.dat" which is in the same directory
# as this file. Lines of this file may be blank or a label/directory pair,
# colon delimited. These can contain just about anything that doesn't confuse
# the shell, except colons and newlines, of course.
# Into your .bashrc, .profile, .zshrc, or whatever:
# cd_()
# {
# cd `python /home/memyselfi/python/mydirs.py $@`
# }
import sys
import os.path as osp
DIRS = dict( [[i.strip() for i in ln.split(':')] \
for ln in open(osp.join(osp.dirname(__file__),
'mydirs.dat'), 'r') if ln.strip()] )
if len(sys.argv) < 2:
print('Usage: cd_ [directory lookup key]', file=sys.stderr)
tgt = '.'
else:
tgt = DIRS.get(sys.argv[1], None)
if not tgt:
print('cd_: unrecognized key: "{0}"'.format(sys.argv[1]), file=sys.stderr)
tgt = '.'
if tgt == '.':
print('recognized keys:', ', '.join(DIRS.keys()), file=sys.stderr)
print(tgt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment