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模块学习 datetime介绍
Aug 27 Python
python中的列表推导浅析
Apr 26 Python
在Python中关于中文编码问题的处理建议
Apr 08 Python
用pickle存储Python的原生对象方法
Apr 28 Python
Django实现组合搜索的方法示例
Jan 23 Python
详解python的sorted函数对字典按key排序和按value排序
Aug 10 Python
Python 脚本获取ES 存储容量的实例
Dec 27 Python
Python中print和return的作用及区别解析
May 05 Python
关于pytorch多GPU训练实例与性能对比分析
Aug 19 Python
wxPython实现分隔窗口
Nov 19 Python
Python partial函数原理及用法解析
Dec 11 Python
Python使用socket去实现TCP客户端和TCP服务端
Apr 12 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获取当前月与上个月月初及月末时间戳的方法
2016/12/05 PHP
两个SUBMIT按钮,如何区分处理
2006/08/22 Javascript
JavaScript中的Screen屏幕对象
2008/01/16 Javascript
ie6下png图片背景不透明的解决办法使用js实现
2013/01/11 Javascript
Javascript基础教程之while语句
2015/01/18 Javascript
纯js实现页面返回顶部的动画(超简单)
2017/08/10 Javascript
JavaScript实现的数字与字符串转换功能示例
2017/08/23 Javascript
表格展示利器 Bootstrap Table实例代码
2017/09/06 Javascript
vue 实现在函数中触发路由跳转的示例
2018/09/01 Javascript
Vue+Express实现登录状态权限验证的示例代码
2019/05/05 Javascript
vue-router的两种模式的区别
2019/05/30 Javascript
前端开发之便利店收银系统代码
2019/12/27 Javascript
vuex刷新后数据丢失的解决方法
2020/10/18 Javascript
JavaScript 实现继承的几种方式
2021/02/19 Javascript
[32:17]完美世界DOTA2联赛循环赛LBZS vs Forest第二场 10月30日
2020/10/31 DOTA
关于pip的安装,更新,卸载模块以及使用方法(详解)
2017/05/19 Python
Python实现比较扑克牌大小程序代码示例
2017/12/06 Python
Python实现的朴素贝叶斯分类器示例
2018/01/06 Python
详解python数据结构和算法
2019/04/18 Python
在python plt图表中文字大小调节的方法
2019/07/08 Python
TensorFlow车牌识别完整版代码(含车牌数据集)
2019/08/05 Python
使用python+poco+夜神模拟器进行自动化测试实例
2020/04/23 Python
python使用yaml 管理selenium元素的示例
2020/12/01 Python
如何进行Linux分区优化
2016/09/13 面试题
介绍一下你对SOA的认识
2016/04/24 面试题
毕业生在校学习的自我评价分享
2013/10/08 职场文书
离婚财产处理协议书
2014/09/30 职场文书
八项规定整改方案
2014/10/01 职场文书
干部四风问题整改措施思想汇报
2014/10/13 职场文书
司机个人年终总结
2015/03/03 职场文书
中学生社会实践教育活动总结
2015/05/06 职场文书
领导干部学习心得体会
2016/01/23 职场文书
2016年党员岗位承诺书
2016/03/24 职场文书
《岳阳楼记》原文、译文赏析
2019/09/10 职场文书
Python如何利用正则表达式爬取网页信息及图片
2021/04/17 Python
Java 深入探究讲解简单工厂模式
2022/04/07 Java/Android