Last active
December 1, 2017 22:22
-
-
Save sfaleron/25d46ab5125016d93075 to your computer and use it in GitHub Desktop.
Quickly access directories from anywhere, with a shell function and Python script
This file contains hidden or 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
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