今天偶然把困扰好久的Python命令行给解决了,简直不能更爽,于是特此记录一下。
很早之前就曾经尝试过给Python命令行加上Tab自动补全,可是网上的办法试了半天也没有作用,一直没搞清楚是什么情况,以为是Mac太奇葩,于是就一直搁置,今天心血来潮,又搜了一下,搜到了这个,说是升到Lion之后才跪的,好吧,我买电脑的时候就已经是Lion了吧,真悲催……
于是乎,赶快按照里面给的方案加上了几句话,果断就生效了,终于不用再羡慕用node命令行时补全的爽了~~~不得不说stack overflow大法好,果然都是我没有用stack overflow的习惯惹的祸
。
然后这次还顺手把历史记录一起给加上了,这样新开python的时候可以上箭头找到上一次运行时的命令了,最后的.pystartup文件如下:
# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Store the file in ~/.pystartup, and set an environment variable to point
# to it: "export PYTHONSTARTUP=~/.pystartup" in bash.
import atexit
import os
import readline
import rlcompleter
# for Mac OS X
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath
当然,不要忘了在shell启动脚本(我是.zshrc)中加上:
export PYTHONSTARTUP=~/.pystartup