Python实现Tab自动补全和历史命令管理的方法


Posted in Python onMarch 12, 2015

本文实例讲述了Python实现Tab自动补全和历史命令管理的方法。分享给大家供大家参考。具体分析如下:

Python的startup文件,即环境变量 PYTHONSTARTUP 对应的文件

1. 为readline添加tab键自动补全的功能

2. 像Shell一样管理历史命令

代码如下:

import rlcompleter

import readline

import atexit

import os

# http://stackoverflow.com/questions/7116038/python-tab-completion-mac-osx-10-7-lion

if 'libedit' in readline.__doc__:

    readline.parse_and_bind('bind ^I rl_complete')

else:

    readline.parse_and_bind('tab: complete')

histfile = os.path.join(os.environ['HOME'], '.pyhist')

try:

    readline.read_history_file(histfile)

except IOError:

    pass

atexit.register(readline.write_history_file, histfile)

del readline, rlcompleter, histfile, os

希望本文所述对大家的Python程序设计有所帮助。

一。这个方法可以修改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 information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', 
'/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', 
'/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']
>>>

从上面看出python在我电脑上的路径是 /usr/lib/python2.7

2.切换至该目录写个startup.py的脚本,脚本目录就是处理python中<tab>事件,脚本内容如下

#!/usr/bin/python 
# python startup file 
     
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.environ['HOME'], '.pythonhistory') 
try: 
  readline.read_history_file(histfile) 
except IOError: 
  pass 
atexit.register(readline.write_history_file, histfile) 
     
del os, histfile, readline, rlcompleter

3.切换至自己主目录

[/usr/lib/python2.7$]cd 

[~$]vi .bashrc

4. 增加环境变量

#for python

export PYTHONSTARTUP=/usr/lib/python2.7/startup.py

5.配置环境变量生效

[~$]source .bashrc

PYTHONSTARTUP是什么东西呢?

If this is the name of a readable file, the Python commands in that file are executed before the first prompt 

is displayed in interactive mode.  The file is executed in the same name space where interactive commands are

executed so that  objects defined  or  imported in it can be used without qualification in the interactive session.  

You can also change the prompts sys.ps1 and sys.ps2 in this file.

二。这个方法能在VIM中自动补全

    1. 下载插件:
       下载地址:https://3water.com/softs/305586.html

   2.拷贝致相应的目录

unzip  pydiction-1.2.1.zip

cp python_pydiction.vim  /usr/share/vim/vim73/ftplugin

mkdir  /usr/share/vim/vim73/pydiction

cp complete-dict  /usr/share/vim/vim73/pydiction/

cp pydiction.py  /usr/share/vim/vim73/pydiction/

 3.修改vim配置文件

 

 let g:pydiction_location = '/usr/share/vim/vim73/pydiction/complete-dict'

let g:pydiction_menu_height = 20

 

 OK,测试是否生效吧

Python 相关文章推荐
详解python并发获取snmp信息及性能测试
Mar 27 Python
JavaScript实现一维数组转化为二维数组
Apr 17 Python
用python处理图片实现图像中的像素访问
May 04 Python
python数据结构学习之实现线性表的顺序
Sep 28 Python
Selenium的使用详解
Oct 19 Python
在IPython中进行Python程序执行时间的测量方法
Nov 01 Python
pyhanlp安装介绍和简单应用
Feb 22 Python
Python I/O与进程的详细讲解
Mar 08 Python
python实现支付宝转账接口
May 07 Python
python用for循环求和的方法总结
Jul 08 Python
django框架用户权限中的session缓存到redis中的方法
Aug 06 Python
利用python清除移动硬盘中的临时文件
Oct 28 Python
Python实现将n个点均匀地分布在球面上的方法
Mar 12 #Python
Python求解平方根的方法
Mar 11 #Python
python自动格式化json文件的方法
Mar 11 #Python
python处理csv数据的方法
Mar 11 #Python
python模拟鼠标拖动操作的方法
Mar 11 #Python
Python创建系统目录的方法
Mar 11 #Python
Python实现从订阅源下载图片的方法
Mar 11 #Python
You might like
PHP网站基础优化方法小结
2008/09/29 PHP
php 获得汉字拼音首字母的函数
2009/08/01 PHP
ThinkPHP中create()方法自动验证实例
2017/04/26 PHP
PHP基于ip2long实现IP转换整形
2020/12/11 PHP
接收键盘指令的脚本
2006/06/26 Javascript
extjs 学习笔记(三) 最基本的grid
2009/10/15 Javascript
JS实现在Repeater控件中创建可隐藏区域的代码
2010/09/16 Javascript
利用jQuery接受和处理xml数据的代码(.net)
2011/03/28 Javascript
js hover 定时器(实例代码)
2013/11/12 Javascript
js实现省市联动效果的简单实例
2014/02/10 Javascript
js实现获取焦点后光标在字符串后
2014/09/17 Javascript
详解JS异步加载的三种方式
2017/03/07 Javascript
vue注册组件的几种方式总结
2018/03/08 Javascript
解决v-for中使用v-if或者v-bind:class失效的问题
2018/09/25 Javascript
JavaScript常见继承模式实例小结
2019/01/11 Javascript
JS多个异步请求 按顺序执行next实现解析
2019/09/16 Javascript
JavaScript手写数组的常用函数总结
2020/11/22 Javascript
[06:50]DSPL次级职业联赛十强晋级之路
2014/11/18 DOTA
[46:38]完美世界DOTA2联赛PWL S2 Magma vs PXG 第三场 11.28
2020/12/02 DOTA
Python数据库的连接实现方法与注意事项
2016/02/27 Python
Python处理XML格式数据的方法详解
2017/03/21 Python
windows下python安装paramiko模块和pycrypto模块(简单三步)
2017/07/06 Python
python中 chr unichr ord函数的实例详解
2017/08/06 Python
Vue的el-scrollbar实现自定义滚动
2018/05/29 Python
python pillow模块使用方法详解
2019/08/30 Python
详解python破解zip文件密码的方法
2020/01/13 Python
如何在 Django 模板中输出 &quot;{{&quot;
2020/01/24 Python
pytorch dataloader 取batch_size时候出现bug的解决方式
2020/02/20 Python
Jupyter Notebook 远程访问配置详解
2021/01/11 Python
html5 乒乓球(碰撞检测)实例二
2013/07/25 HTML / CSS
美国羊皮公司:Overland
2018/01/15 全球购物
园林资料员岗位职责
2013/12/30 职场文书
2015年加油站站长工作总结
2015/05/27 职场文书
golang DNS服务器的简单实现操作
2021/04/30 Golang
Matplotlib可视化之添加让统计图变得简单易懂的注释
2021/06/11 Python
git stash(储藏)的用法总结
2022/06/25 Servers