2016-04-29 06:33:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-11-23 14:10:22 +00:00
|
|
|
"""
|
|
|
|
IPython autojump magic
|
2010-11-25 14:19:33 +00:00
|
|
|
|
2012-11-23 14:10:22 +00:00
|
|
|
Written by keith hughitt <keith.hughitt@gmail.com>, based on an earlier
|
|
|
|
version by Mario Pastorelli <pastorelli.mario@gmail.com>.
|
2010-11-25 14:33:53 +00:00
|
|
|
|
2013-12-28 13:13:47 +00:00
|
|
|
To install, create a new IPython user profile by running:
|
2012-11-23 14:10:22 +00:00
|
|
|
|
|
|
|
ipython profile create
|
2013-06-05 13:26:30 +00:00
|
|
|
|
2013-12-28 13:13:47 +00:00
|
|
|
And copy this file into the "startup" folder of your new profile (e.g.
|
2012-11-23 14:10:22 +00:00
|
|
|
"$HOME/.config/ipython/profile_default/startup/").
|
|
|
|
|
|
|
|
@TODO: extend %cd to call "autojump -a"
|
|
|
|
"""
|
2013-12-28 13:13:47 +00:00
|
|
|
from subprocess import PIPE
|
2016-04-29 06:33:04 +00:00
|
|
|
from subprocess import Popen
|
2013-12-28 13:13:47 +00:00
|
|
|
|
|
|
|
from IPython.core.magic import register_line_magic
|
2010-11-25 14:19:33 +00:00
|
|
|
|
2014-01-07 15:27:05 +00:00
|
|
|
ip = get_ipython() # noqa
|
2010-11-25 14:19:33 +00:00
|
|
|
|
2013-12-28 13:13:47 +00:00
|
|
|
|
2012-11-23 14:10:22 +00:00
|
|
|
@register_line_magic
|
|
|
|
def j(path):
|
|
|
|
cmd = ['autojump'] + path.split()
|
2013-12-28 13:13:47 +00:00
|
|
|
newpath = Popen(
|
2016-04-29 06:33:04 +00:00
|
|
|
cmd,
|
|
|
|
stdout=PIPE,
|
|
|
|
shell=False).communicate()[0].strip()
|
2013-12-28 13:13:47 +00:00
|
|
|
|
2010-11-25 14:33:53 +00:00
|
|
|
if newpath:
|
2013-06-05 13:26:30 +00:00
|
|
|
ip.magic('cd %s' % newpath.decode('utf-8'))
|
2010-11-25 14:19:33 +00:00
|
|
|
|
2017-06-01 05:52:05 +00:00
|
|
|
|
2012-11-23 14:10:22 +00:00
|
|
|
# remove from namespace
|
|
|
|
del j
|