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 相关文章推荐
gearman的安装启动及python API使用实例
Jul 08 Python
python实现得到一个给定类的虚函数
Sep 28 Python
列举Python中吸引人的一些特性
Apr 09 Python
在Python中使用pngquant压缩png图片的教程
Apr 09 Python
简单介绍Python中的floor()方法
May 15 Python
Python+django实现简单的文件上传
Aug 17 Python
Python自动化运维_文件内容差异对比分析
Dec 13 Python
解决python字典对值(值为列表)赋值出现重复的问题
Jan 20 Python
pytorch之ImageFolder使用详解
Jan 06 Python
PyQt5实现简单的计算器
May 30 Python
Python RabbitMQ实现简单的进程间通信示例
Jul 02 Python
Python开发.exe小工具的详细步骤
Jan 27 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程序员的13个好习惯小结
2012/02/20 PHP
phpinfo()中Loaded Configuration File(none)的解决方法
2017/01/16 PHP
Laravel validate error处理,ajax,json示例
2019/10/25 PHP
对google个性主页的拖拽效果的js的完整注释[转]
2007/04/10 Javascript
简单的Jquery全选功能
2013/11/07 Javascript
js 获取页面高度和宽度兼容 ie firefox chrome等
2014/05/14 Javascript
Javascript快速排序算法详解
2014/12/03 Javascript
node.js中的fs.truncate方法使用说明
2014/12/15 Javascript
jQuery实现向下滑出的二级菜单效果实例
2015/08/22 Javascript
js实现仿微博滚动显示信息的效果
2015/12/21 Javascript
javascript类型系统_正则表达式RegExp类型详解
2016/06/24 Javascript
浅谈Angular中ngModel的$render
2016/10/24 Javascript
js中的面向对象入门
2017/03/06 Javascript
js Dom实现换肤效果
2017/10/21 Javascript
vue-cli项目根据线上环境分别打出测试包和生产包
2018/05/23 Javascript
Vue父子组建的简单通信之控制开关Switch的实现
2018/06/04 Javascript
解决在layer.open中使用时间控件laydate失败的问题
2019/09/11 Javascript
[42:56]VGJ.S vs Serenity 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
Python设计模式之单例模式实例
2014/04/26 Python
详解Python使用simplejson模块解析JSON的方法
2016/03/24 Python
Python更新数据库脚本两种方法及对比介绍
2017/07/27 Python
Django中更改默认数据库为mysql的方法示例
2018/12/05 Python
python实现对任意大小图片均匀切割的示例
2018/12/05 Python
使用python进行波形及频谱绘制的方法
2019/06/17 Python
python命令行工具Click快速掌握
2019/07/04 Python
python 进程的几种创建方式详解
2019/08/29 Python
使用virtualenv创建Python环境及PyQT5环境配置的方法
2019/09/10 Python
django 框架实现的用户注册、登录、退出功能示例
2019/11/28 Python
python 安装教程之Pycharm安装及配置字体主题,换行,自动更新
2020/03/13 Python
美国相机和电子产品零售商:Beach Camera
2020/11/26 全球购物
班组长工作职责
2013/12/25 职场文书
酒店总经理岗位职责
2014/03/17 职场文书
2015年学校管理工作总结
2015/07/20 职场文书
用Python爬取英雄联盟的皮肤详细示例
2021/12/06 Python
Nginx虚拟主机的配置步骤过程全解
2022/03/31 Servers
vue @click.native 绑定原生点击事件
2022/04/22 Vue.js