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实现ip查询示例
Mar 26 Python
跟老齐学Python之编写类之三子类
Oct 11 Python
Python常用的爬虫技巧总结
Mar 28 Python
Python基于Pymssql模块实现连接SQL Server数据库的方法详解
Jul 20 Python
Python yield与实现方法代码分析
Feb 06 Python
Python 修改列表中的元素方法
Jun 26 Python
Flask-Mail用法实例分析
Jul 21 Python
Python实现投影法分割图像示例(二)
Jan 17 Python
浅谈pytorch池化maxpool2D注意事项
Feb 18 Python
python3实现往mysql中插入datetime类型的数据
Mar 02 Python
python数据库编程 Mysql实现通讯录
Mar 27 Python
TensorFlow固化模型的实现操作
May 26 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
用户的详细注册和判断
2006/10/09 PHP
PHP JSON 数据解析代码
2010/05/26 PHP
PHP中usort在值相同时改变原始位置问题的解决方法
2011/11/27 PHP
ThinkPHP文件上传实例教程
2014/08/22 PHP
PHP也能干大事 随机函数
2015/04/14 PHP
PHP 闭包详解及实例代码
2016/09/28 PHP
PHP/HTML混写的四种方式总结
2017/02/27 PHP
IOS 开发之NSDictionary转换成JSON字符串
2017/08/14 PHP
Jquery+CSS3实现一款简洁大气带滑动效果的弹出层
2013/05/15 Javascript
jquery实现的随机多彩tag标签随机颜色和字号大小效果
2014/03/27 Javascript
js简单时间比较的方法
2016/08/02 Javascript
教你快速搭建Node.Js服务器的方法教程
2017/03/30 Javascript
nodejs中request库使用HTTPS代理的方法
2019/04/30 NodeJs
手把手带你入门微信小程序新框架Kbone的使用
2020/02/25 Javascript
[04:56]经典回顾:前Ehome 与 前LGD
2015/02/26 DOTA
Python学习笔记(一)(基础入门之环境搭建)
2014/06/05 Python
python正则表达式之作业计算器
2016/03/18 Python
Windows下安装Django框架的方法简明教程
2018/03/28 Python
python3通过selenium爬虫获取到dj商品的实例代码
2019/04/25 Python
python实现二分类的卡方分箱示例
2019/11/22 Python
利用python 下载bilibili视频
2020/11/13 Python
用Python实现定时备份Mongodb数据并上传到FTP服务器
2021/01/27 Python
各大浏览器 CSS3 和 HTML5 兼容速查表 图文
2010/04/01 HTML / CSS
EQVVS官网:设计师男装和女装
2018/10/24 全球购物
英国最大的在线亚洲杂货店:Red Rickshaw
2020/03/22 全球购物
面向对象编程是如何提高软件开发水平的
2014/05/06 面试题
有关打架的检讨书
2014/01/25 职场文书
银行优秀员工事迹材料
2014/05/29 职场文书
党的群众路线批评与自我批评范文
2014/10/16 职场文书
机关党员四风问题个人整改措施
2014/10/26 职场文书
清洁工工作总结
2015/08/11 职场文书
2016五一手机促销广告语
2016/01/28 职场文书
用golang如何替换某个文件中的字符串
2021/04/25 Golang
Python Django ORM连表正反操作技巧
2021/06/13 Python
python自动计算图像数据集的RGB均值
2021/06/18 Python
javascript代码简写的几种常用方式汇总
2021/08/23 Javascript