Skip to content

Instantly share code, notes, and snippets.

@Trundle
Last active September 21, 2016 19:19
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 Trundle/683a399e496f28be89d1af914c349382 to your computer and use it in GitHub Desktop.
Save Trundle/683a399e496f28be89d1af914c349382 to your computer and use it in GitHub Desktop.
Show current working directory in prompt

Show current working directory in prompt

  1. Save the above snippet in a file (for example show_cwd_in_prompt.py)
  2. Set the environment variable PYTHONSTARTUP to the path of the file
  3. Enjoy bpython
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>
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