- Save the above snippet in a file (for example
show_cwd_in_prompt.py
) - Set the environment variable
PYTHONSTARTUP
to the path of the file - Enjoy bpython
Last active
September 21, 2016 19:19
-
-
Save Trundle/683a399e496f28be89d1af914c349382 to your computer and use it in GitHub Desktop.
Show current working directory in prompt
This file contains 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
bpython version 0.16.dev52 on top of Python 3.5.2+ /tmp/_env/bin/python | |
/tmp/bpython> import os | |
/tmp/bpython> os.chdir("/home/andy") | |
~> os.chdir("/home/andy/") | |
~> os.chdir("/home/andy/src") | |
~/src> |
This file contains 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
import os | |
import sys | |
original_hook = sys.displayhook | |
_home = os.path.expanduser("~") | |
def set_ps1_to_cwd(): | |
cwd = os.getcwd() | |
home_prefix = os.path.commonpath([_home, cwd]) | |
if home_prefix == _home: | |
cwd = "~" + cwd[len(home_prefix):] | |
sys.ps1 = cwd + "> " | |
def cwd_displayhook(x): | |
original_hook(x) | |
set_ps1_to_cwd() | |
sys.displayhook = cwd_displayhook | |
set_ps1_to_cwd() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment