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 运算符 供重载参考
Jun 11 Python
python网络编程示例(客户端与服务端)
Apr 24 Python
Python编程实现生成特定范围内不重复多个随机数的2种方法
Apr 14 Python
python实现生命游戏的示例代码(Game of Life)
Jan 24 Python
解决Python找不到ssl模块问题 No module named _ssl的方法
Apr 29 Python
pyQt5实时刷新界面的示例
Jun 25 Python
Django配置文件代码说明
Dec 04 Python
tf.concat中axis的含义与使用详解
Feb 07 Python
Python pip安装模块提示错误解决方案
May 22 Python
PyInstaller的安装和使用的详细步骤
Jun 02 Python
Python爬取网站图片并保存的实现示例
Feb 26 Python
python四个坐标点对图片区域最小外接矩形进行裁剪
Jun 04 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
DOTA2 1月28日更新:监管系统降临刀塔世界
2021/01/28 DOTA
PHP读取txt文件的内容并赋值给数组的代码
2011/11/03 PHP
PHP写的求多项式导数的函数代码
2012/07/04 PHP
php使用gettimeofday函数返回当前时间并存放在关联数组里
2015/03/19 PHP
js给dropdownlist添加选项的小例子
2013/03/04 Javascript
鼠标移入移出事件改变图片的分辨率的两种方法
2013/12/17 Javascript
如何改进javascript代码的性能
2015/04/02 Javascript
js获取图片宽高的方法
2015/11/25 Javascript
关于javascript获取内联样式与嵌入式样式的实例
2017/06/01 Javascript
利用webstrom调试Vue.js单页面程序的方法教程
2017/06/06 Javascript
微信小程序 本地图片按照屏幕尺寸处理
2017/08/04 Javascript
新手vue构建单页面应用实例代码
2017/09/18 Javascript
微信小程序的日期选择器的实例详解
2017/09/29 Javascript
jQuery EasyUI 选项卡面板tabs的使用实例讲解
2017/12/25 jQuery
详解webpack-dev-server使用http-proxy解决跨域问题
2018/01/13 Javascript
JS闭包原理与应用经典示例
2018/12/20 Javascript
Layui 数据表格批量删除和多条件搜索的实例
2019/09/04 Javascript
JS操作字符串转数字的常见方法示例
2019/10/29 Javascript
Vue filter 过滤器、以及在table中的使用介绍
2020/09/07 Javascript
[56:38]DOTA2-DPC中国联赛正赛Aster vs Magma BO3 第一场 3月5日
2021/03/11 DOTA
用Python实现一个简单的能够上传下载的HTTP服务器
2015/05/05 Python
举例讲解Python设计模式编程的代理模式与抽象工厂模式
2016/01/16 Python
python区块及区块链的开发详解
2019/07/03 Python
python redis 批量设置过期key过程解析
2019/11/26 Python
Python+PyQt5实现灭霸响指功能
2020/05/25 Python
HTML5 canvas基本绘图之图形组合
2016/06/27 HTML / CSS
总经理助理岗位职责
2013/11/08 职场文书
《两个铁球同时着地》教学反思
2014/02/13 职场文书
综合实践活动方案
2014/02/14 职场文书
春季运动会广播稿大全
2014/02/19 职场文书
售后服务经理岗位职责
2014/02/25 职场文书
火灾现场处置方案
2014/05/28 职场文书
小学公民道德宣传日活动总结
2015/03/23 职场文书
详解MySQL InnoDB存储引擎的内存管理
2021/04/08 MySQL
用python画城市轮播地图
2021/05/28 Python
JS前端可视化canvas动画原理及其推导实现
2022/08/05 Javascript