为Python添加交互模式下TAB自动补全以及命令历史功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为Python添加交互模式下TAB自动补全以及命令历史功能相关的知识,希望对你有一定的参考价值。

接上篇文章
新建Python环境变量配置文件:
在宿主用户目录下
vim .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
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


设置python 环境变量:
永久生效:
echo “export PYTHONSTARTUP=~/.pystartup” >> /etc/profile
source /etc/profile

验证:
[[email protected] ~]# python27
Python 2.7.9 (default, Jun 29 2016, 16:05:04) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> e
elif        else        enumerate(  eval_r(       except      exec        execfile(   exit(       
>>> e

本文出自 “jinchuang” 博客,请务必保留此出处http://jinchuang.blog.51cto.com/8690689/1844413

以上是关于为Python添加交互模式下TAB自动补全以及命令历史功能的主要内容,如果未能解决你的问题,请参考以下文章

交互模式下PYTHON自动补全和VIM中PYTHON自动补全

Win10(PowerShell)下Python命令行tab自动补全

python环境tab命令自动补全

说明tab键补全命令以及补全路径的原理

python tab 自动补全

给python2.6的交互模式加一个TAB补全