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中操作列表之list.extend()方法的使用
May 20 Python
使用Python设计一个代码统计工具
Apr 04 Python
python format 格式化输出方法
Jul 16 Python
在python plt图表中文字大小调节的方法
Jul 08 Python
简单介绍python封装的基本知识
Aug 10 Python
Python序列类型的打包和解包实例
Dec 21 Python
python新式类和经典类的区别实例分析
Mar 23 Python
基于python实现FTP文件上传与下载操作(ftp&amp;sftp协议)
Apr 01 Python
python输入一个水仙花数(三位数) 输出百位十位个位实例
May 03 Python
在echarts中图例legend和坐标系grid实现左右布局实例
May 16 Python
Python 判断时间是否在时间区间内的实例
May 16 Python
如何用Python绘制3D柱形图
Sep 16 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 实现判断用户是否手机访问
2015/01/21 PHP
php+ajax实现无刷新数据分页的办法
2015/11/02 PHP
学习php设计模式 php实现桥梁模式(bridge)
2015/12/07 PHP
PHP消息队列用法实例分析
2016/02/12 PHP
PHP扩展框架之Yaf框架的安装与使用
2016/05/18 PHP
thinkPHP5框架实现基于ajax的分页功能示例
2018/06/12 PHP
很可爱的输入框
2008/08/03 Javascript
js保留小数点后几位的写法
2014/01/03 Javascript
JS组件Form表单验证神器BootstrapValidator
2016/01/26 Javascript
Bootstrap源码解读下拉菜单(4)
2016/12/23 Javascript
Vue 2.X的状态管理vuex记录详解
2017/03/23 Javascript
实例讲解javascript实现异步图片上传方法
2017/12/05 Javascript
详解Vue-Router源码分析路由实现原理
2019/05/15 Javascript
Python中几种操作字符串的方法的介绍
2015/04/09 Python
Python使用flask框架操作sqlite3的两种方式
2018/01/31 Python
Python面向对象基础入门之编码细节与注意事项
2018/12/11 Python
Pyqt5 基本界面组件之inputDialog的使用
2019/06/25 Python
Django命名URL和反向解析URL实现解析
2019/08/09 Python
python图片指定区域替换img.paste函数的使用
2020/04/09 Python
windows上彻底删除jupyter notebook的实现
2020/04/13 Python
python3.7+selenium模拟淘宝登录功能的实现
2020/05/26 Python
Python selenium模块实现定位过程解析
2020/07/09 Python
python使用matplotlib绘制折线图的示例代码
2020/09/22 Python
Ubuntu20.04环境安装tensorflow2的方法步骤
2021/01/29 Python
英国最大的在线亚洲杂货店:Red Rickshaw
2020/03/22 全球购物
自我鉴定模板
2013/10/29 职场文书
超市业务员岗位职责
2013/12/05 职场文书
2014年入党积极分子党课学习心得体会模板
2014/04/03 职场文书
材料化学专业求职信
2014/07/15 职场文书
商场租赁意向书
2014/07/30 职场文书
党支部活动策划方案
2014/08/18 职场文书
餐饮食品安全责任书
2015/01/29 职场文书
一文带你探究MySQL中的NULL
2021/11/11 MySQL
vue3获取当前路由地址
2022/02/18 Vue.js
JS轻量级函数式编程实现XDM三
2022/06/16 Javascript