Skip to content

Instantly share code, notes, and snippets.

@pirate
Last active February 26, 2016 08:28
Show Gist options
  • Save pirate/1259eec02266d3fdd9f6 to your computer and use it in GitHub Desktop.
Save pirate/1259eec02266d3fdd9f6 to your computer and use it in GitHub Desktop.
Pythonrc with Shortcuts for ls, ll, cd, and quit
"""Some common helper functions to put in your ~/.pythonrc to make the python REPL behave more like a real shell."""
import os
def cd(folder):
"""e.g. cd('/bin')"""
os.chdir(folder)
def quit():
raise SystemExit(0)
def listdir(dir=None):
return os.listdir(dir or os.getcwd())
class InstantShortcut:
"""run a command instantly on __repr__, also supports adding parens to call it with args"""
def __init__(self, command, delimiter='\n'):
self.__call__ = command
self.delimiter = delimiter
def __repr__(self):
return self.delimiter.join(self())
quit = q = InstantShortcut(quit)
ls = InstantShortcut(listdir, ' ')
ll = InstantShortcut(listdir, '\n')
# usage: type `ll` and press enter in a REPL to list the current directory on seperate lines
# type just `quit` and press enter to quit python instead of having to type quit()
@pirate
Copy link
Author

pirate commented Feb 26, 2016

screen shot 2016-02-26 at 12 25 41 am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment