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日期操作学习笔记
Oct 07 Python
本地文件上传到七牛云服务器示例(七牛云存储)
Jan 11 Python
浅析Python中的getattr(),setattr(),delattr(),hasattr()
Jun 14 Python
django框架如何集成celery进行开发
May 24 Python
python 调用win32pai 操作cmd的方法
May 28 Python
Python实现模拟分割大文件及多线程处理的方法
Oct 10 Python
Python代码缩进和测试模块示例详解
May 07 Python
对python-3-print重定向输出的几种方法总结
May 11 Python
django的登录注册系统的示例代码
May 14 Python
对Tensorflow中的变量初始化函数详解
Jul 27 Python
Python3 log10()函数简单用法
Feb 19 Python
关于python中密码加盐的学习体会小结
Jul 15 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连接sql server 2005环境配置及问题解决
2014/08/08 PHP
PHP 9 大缓存技术总结
2015/09/17 PHP
学习php设计模式 php实现命令模式(command)
2015/12/08 PHP
ThinkPHP3.2框架使用addAll()批量插入数据的方法
2017/03/16 PHP
Yii2使用$this-&gt;context获取当前的Module、Controller(控制器)、Action等
2017/03/29 PHP
PHP实现的堆排序算法详解
2017/08/17 PHP
Yii框架核心组件类实例详解
2019/08/06 PHP
你所要知道JS(DHTML)中的一些技巧
2007/01/09 Javascript
使用typeof方法判断undefined类型
2014/09/09 Javascript
为什么Node.js会这么火呢?Node.js流行的原因
2014/12/01 Javascript
一款简单的jQuery图片标注效果附源码下载
2016/03/22 Javascript
很棒的js选项卡切换效果
2016/07/15 Javascript
详解nodejs微信公众号开发——3.封装消息响应模块
2017/04/10 NodeJs
详解Vue.js组件可复用性的混合(mixin)方式和自定义指令
2017/09/06 Javascript
js 获取json数组里面数组的长度实例
2017/10/31 Javascript
Angular 4根据组件名称动态创建出组件的方法教程
2017/11/01 Javascript
Angular移动端页面input无法输入的解决方法
2017/11/14 Javascript
js断点调试心得分享(必看篇)
2017/12/08 Javascript
详解VUE2.X过滤器的使用方法
2018/01/11 Javascript
webpack4 升级迁移的实现
2018/09/12 Javascript
JavaScript 点击触发复制功能实例详解
2018/11/02 Javascript
JavaScript实现简单计算器功能
2019/12/19 Javascript
基于node+websocket+html实现腾讯课堂聊天室聊天功能
2020/03/04 Javascript
解决pandas.DataFrame.fillna 填充Nan失败的问题
2018/11/06 Python
Python如何应用cx_Oracle获取oracle中的clob字段问题
2019/08/27 Python
python 创建一维的0向量实例
2019/12/02 Python
Python 私有属性和私有方法应用场景分析
2020/06/19 Python
HTML5 Web Workers之网站也能多线程的实现
2013/04/24 HTML / CSS
html5使用canvas绘制一张图片
2014/12/15 HTML / CSS
AmazeUI 按钮交互的实现示例
2020/08/24 HTML / CSS
俄罗斯极限运动网上商店:Board Shop №1
2020/12/18 全球购物
斯福泰克软件测试面试题
2015/02/16 面试题
银行演讲稿范文
2014/01/03 职场文书
社保缴纳证明申请书
2014/11/03 职场文书
财务个人年度总结范文
2015/02/26 职场文书
opencv用VS2013调试时用Image Watch插件查看图片
2021/07/26 Python