Readline 库的快捷键绑定和其它一些参数可以通过名为 key-name: function-name
或者: "string": function-name
选项可以如下设置: set option-name value
例如: # I prefer vi-style editing:
set editing-mode vi
# Edit using a single line:
set horizontal-scroll-mode On
# Rebind some keys:
Meta-h: backward-kill-word
"\C-u": universal-argument
"\C-x\C-r": re-read-init-file
需要注意的是 Python 中默认 Tab: complete
到你的 自动完成变量和模块名也可以激活生效。要使之在解释器交互模式中可用,在你的启动文件中加入下面内容: [1] import rlcompleter, readline
readline.parse_and_bind('tab: complete')
这个操作将 更有用的初始化文件可能是下面这个例子这样的。要注意一旦创建的名字没用了,它会删掉它们;因为初始化文件作为解释命令与之在同一个命名空间执行,在交互环境中删除命名带来了边际效应。可能你发现了它体贴的保留了一些导入模块,类似 # 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
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
|
Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )
GMT+8, 2024-9-21 03:29 , Processed in 0.032653 second(s), 18 queries .