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实现文件按照日期命名的方法
Jul 09 Python
python+unittest+requests实现接口自动化的方法
Nov 29 Python
python使用pdfminer解析pdf文件的方法示例
Dec 20 Python
python批量识别图片指定区域文字内容
Apr 30 Python
Django 路由控制的实现
Jul 17 Python
python常见字符串处理函数与用法汇总
Oct 30 Python
Python基于requests实现模拟上传文件
Apr 21 Python
python中如何进行连乘计算
May 28 Python
python获取整个网页源码的方法
Aug 03 Python
Python通过format函数格式化显示值
Oct 17 Python
Python实现归一化算法详情
Mar 18 Python
python使用opencv对图像添加噪声(高斯/椒盐/泊松/斑点)
Apr 06 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
1 Tube Radio
2021/03/02 无线电
yii框架源码分析之创建controller代码
2011/06/28 PHP
ThinkPHP使用心得分享-上传类UploadFile的使用
2014/05/15 PHP
php实现XML和数组的相互转化功能示例
2017/02/08 PHP
javascript:以前写的xmlhttp池,代码
2008/05/18 Javascript
JS IE和FF兼容性问题汇总
2009/02/09 Javascript
一个js写的日历(代码部分网摘)
2009/09/20 Javascript
对frameset、frame、iframe的js操作示例代码
2013/08/16 Javascript
jquery实现在页面加载的时自动为日期插件添加当前日期
2014/08/20 Javascript
node.js中的fs.openSync方法使用说明
2014/12/17 Javascript
javascript实现rgb颜色转换成16进制格式
2015/07/10 Javascript
jQuery插件实现静态HTML验证码校验
2015/11/06 Javascript
jquery+php实现滚动的数字特效
2015/11/29 Javascript
ionic隐藏tabs的方法
2016/08/29 Javascript
vuejs实现本地数据的筛选分页功能思路详解
2017/11/15 Javascript
获取本机IP地址的实例(JavaScript / Node.js)
2017/11/24 Javascript
jquery ajax加载数据前台渲染方式 不用for遍历的方法
2018/08/09 jQuery
JS函数参数的传递与同名参数实例分析
2020/03/16 Javascript
[01:08]DOTA2次级职业联赛 - Wings 战队宣传片
2014/12/01 DOTA
Python实现的科学计算器功能示例
2017/08/04 Python
Python Opencv实现图像轮廓识别功能
2020/03/23 Python
Pandas之Dropna滤除缺失数据的实现方法
2019/06/25 Python
如何学习Python time模块
2020/06/03 Python
关于python scrapy中添加cookie踩坑记录
2020/11/17 Python
CSS3中媒体查询结合rem布局适配手机屏幕
2019/06/10 HTML / CSS
html5中canvas学习笔记1-画板的尺寸与实际显示尺寸
2013/01/06 HTML / CSS
使用html5 canvas绘制圆环动效
2019/06/03 HTML / CSS
全球销量第一生发产品:Viviscal
2017/12/21 全球购物
如何安装ruby on rails
2014/02/09 面试题
本科毕业自我鉴定
2014/03/20 职场文书
企业新年寄语
2014/04/04 职场文书
教师文明餐桌光盘行动倡议书
2015/04/28 职场文书
2015年度电厂个人工作总结
2015/05/13 职场文书
幼儿园体操比赛口号
2015/12/25 职场文书
Python 的演示平台支持 WSGI 接口的应用
2022/04/20 Python
Vue router配置与使用分析讲解
2022/12/24 Vue.js