为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

时间: 2024-08-25 00:08:46

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

Python添加tab自动补全及命令历史功能。

1. 新建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 rea

python命令行添加自动补全和命令历史功能

# python startup file import readline import rlcompleter import atexit import os # tab completion readline.parse_and_bind('tab: complete') # history file histfile = os.path.join(os.environ['HOME'], '.pythonhistory') try: readline.read_history_file(hi

使用tab自动补全mysql命令

1.使用mysql --help查看mysql的命令参数 注意 --auto-rehash       Enable automatic rehashing. One doesn't need to use 'rehash' to get table and field completion, but startup and reconnecting may take a longer time. Disable with --disable-auto-rehash. (Defaults to

python交互模式下的tab自动补全

python在交互模式下,输入命令的时候按tab键就按时间tab键的功能输出,在书写python时多有不便. 把下面的代码写入一个文件tab.py,放到/usr/lib/python2.6/site-packages/下 #!/usr/bin/env python import sys import readline import rlcompleter import atexit import os readline.parse_and_bind('tab:complete') histfil

python的tab自动补全

很久不写blog了. 最近发现python的自动补全是个好东西. 分享一下. 参考了:http://www.cnblogs.com/allenblogs/archive/2011/04/21/2023288.html 和 软件手册 下载地址:http://vim.sourceforge.net/scripts/script.php?script_id=850 直接粘贴了. ------------------------------ vim编辑器中 #mkdir -p ~/.vim/after/

CentOs6.5 更新python2.7,以及tab自动补全

#安装python2.7之前,准备工作: yum install python-devel gcc patch readline* -y #解压并安装: tar xf Python-2.7.10.tar.xz cd Python-2.7.10 sed -i 's/#readline/readline/g' Modules/Setup.dist ./configure --prefix=/usr/local/python make && make install #更改系统python调用

python2.7 tab,自动补全

python <tab>自动补全 一.这个方法可以修改shell命令行的自动补全 1.获取python目录[我使用的是64位ubuntu系统] [~$]python Python 2.7.3 (default, Apr 10 2013, 06:20:15) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more 

sudo和man的tab自动补全

要加入sudo和man的tab自动补全功能,只需在~/.bashrc中加入: #Enabling tab-completioncomplete -cf sudocomplete -cf man

Python基础 (tab自动补全)

Python 自动补全 1.添加python自动补全的脚本 vim ~/.pythonstartup #添加如下 #!/usr/bin/python import sys import readline import rlcompleter import atexit import os # tab completion readline.parse_and_bind('tab: complete') # history file histfile = os.path.join(os.envir