Skip to content

Instantly share code, notes, and snippets.

@sfaleron sfaleron/mydirs.py
Last active Dec 1, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.