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中的startswith和endswith函数使用实例
Aug 25 Python
使用Python编写简单网络爬虫抓取视频下载资源
Nov 04 Python
视觉直观感受若干常用排序算法
Apr 13 Python
Python 读取某个目录下所有的文件实例
Jun 23 Python
Python函数参数操作详解
Aug 03 Python
对python中不同模块(函数、类、变量)的调用详解
Jul 16 Python
python基于FTP实现文件传输相关功能代码实例
Sep 28 Python
哈工大自然语言处理工具箱之ltp在windows10下的安装使用教程
May 07 Python
无惧面试,带你搞懂python 装饰器
Aug 17 Python
如何使用 Flask 做一个评论系统
Nov 27 Python
Python绘制K线图之可视化神器pyecharts的使用
Mar 02 Python
详解Flask开发技巧之异常处理
Jun 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 Cookie的使用教程详解
2013/06/03 PHP
Laravel 框架返回状态拦截代码
2019/10/18 PHP
javascript使用定时函数实现跳转到某个页面
2013/12/25 Javascript
一个html5播放视频的video控件只支持android的默认格式mp4和3gp
2014/05/08 Javascript
如何书写高质量jQuery代码(使用jquery性能问题)
2014/06/30 Javascript
JavaScript变量声明详解
2014/11/27 Javascript
JS实现模拟百度搜索“2012世界末日”网页地震撕裂效果代码
2015/10/31 Javascript
JS实现兼容各种浏览器的高级拖动方法完整实例【测试可用】
2016/06/21 Javascript
AngularJs Injecting Services Into Controllers详解
2016/09/02 Javascript
利用angular.copy取消变量的双向绑定与解析
2016/11/25 Javascript
BOM之navigator对象和用户代理检测
2017/02/10 Javascript
100多个基础常用JS函数和语法集合大全
2017/02/16 Javascript
JS实现css hover操作的方法示例
2017/04/07 Javascript
Easyui和zTree两种方式分别实现树形下拉框
2017/08/04 Javascript
js 开发之autocomplete=&quot;off&quot;在chrom中失效的解决办法
2017/09/28 Javascript
js使用swiper实现层叠轮播效果实例代码
2018/12/12 Javascript
在Python中调用ggplot的三种方法
2015/04/08 Python
轻松实现TensorFlow微信跳一跳的AI
2018/01/05 Python
PHP实现发送和接收JSON请求
2018/06/07 Python
python安装pywin32clipboard的操作方法
2019/01/24 Python
Python 堆叠柱状图绘制方法
2019/07/29 Python
Python TCP通信客户端服务端代码实例
2019/11/21 Python
Tensorflow实现多GPU并行方式
2020/02/03 Python
Python文件操作方法详解
2020/02/09 Python
如何利用python检测图片是否包含二维码
2020/10/15 Python
兰芝美国网上商城:购买LANEIGE睡眠面膜等
2017/06/30 全球购物
澳大利亚百货公司:David Jones
2018/02/08 全球购物
西班牙在线光学:Visual-Click
2020/06/22 全球购物
在DELPHI中调用存储过程和使用内嵌SQL哪种方式更好
2016/11/22 面试题
办加油卡单位介绍信
2014/01/09 职场文书
考试不及格检讨书
2014/01/09 职场文书
最新茶叶店创业计划书
2014/01/14 职场文书
2014个人年度工作总结范文
2014/12/24 职场文书
linux下导入、导出mysql数据库命令的实现方法
2021/05/26 MySQL
python中sqllite插入numpy数组到数据库的实现方法
2021/06/21 Python
Python使用华为API为图像设置多个锚点标签
2022/04/12 Python