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文件读写并使用mysql批量插入示例分享(python操作mysql)
Feb 17 Python
Python ValueError: invalid literal for int() with base 10 实用解决方法
Jun 21 Python
Python批量创建迅雷任务及创建多个文件
Feb 13 Python
深入解析Python中的descriptor描述器的作用及用法
Jun 27 Python
Python基于回溯法子集树模板实现8皇后问题
Sep 01 Python
python2 与python3的print区别小结
Jan 16 Python
python实现的接收邮件功能示例【基于网易POP3服务器】
Sep 11 Python
pytorch 修改预训练model实例
Jan 18 Python
Python 动态变量名定义与调用方法
Feb 09 Python
使用Python获取当前工作目录和执行命令的位置
Mar 09 Python
PyTorch中的C++扩展实现
Apr 02 Python
python实现录音功能(可随时停止录音)
Oct 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
ThinkPHP模版中导入CSS和JS文件的方法
2014/11/29 PHP
php中二分法查找算法实例分析
2016/09/22 PHP
Yii2实现ActiveForm ajax提交
2017/05/26 PHP
javascript中clipboardData对象用法详解
2015/05/13 Javascript
javascript随机显示背景图片的方法
2015/06/18 Javascript
js仿QQ邮箱收件人选择与搜索功能
2017/02/10 Javascript
jQuery实现元素的插入
2017/02/27 Javascript
Three.js利用顶点绘制立方体的方法详解
2017/09/27 Javascript
vue生成token保存在客户端localStorage中的方法
2017/10/25 Javascript
javascript高级模块化require.js的具体使用方法
2017/10/31 Javascript
原生js实现自定义消息提示框
2020/11/19 Javascript
基于Vue3.0开发轻量级手机端弹框组件V3Popup的场景分析
2020/12/30 Vue.js
[05:06]DOTA2-DPC中国联赛 正赛 VG vs Magma选手采访
2021/03/11 DOTA
简明 Python 基础学习教程
2007/02/08 Python
Python迭代用法实例教程
2014/09/08 Python
python的re模块应用实例
2014/09/26 Python
python处理图片之PIL模块简单使用方法
2015/05/11 Python
Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
2015/07/27 Python
python print 按逗号或空格分隔的方法
2018/05/02 Python
详解Python利用random生成一个列表内的随机数
2019/08/21 Python
python实现两个字典合并,两个list合并
2019/12/02 Python
python全局变量引用与修改过程解析
2020/01/07 Python
VSCode 自定义html5模板的实现
2019/12/05 HTML / CSS
阳光体育:Sunny Sports(购买露营和远足设备)
2018/08/07 全球购物
完美实现CSS垂直居中的11种方法
2021/03/27 HTML / CSS
小学教师的个人自我鉴定
2013/10/24 职场文书
校领导推荐信
2013/11/01 职场文书
办公室前台的岗位职责
2013/12/20 职场文书
婚礼证婚人证婚词
2014/01/08 职场文书
读书活动总结范文
2014/04/26 职场文书
积极向上的团队口号
2014/06/06 职场文书
自我查摆剖析材料
2014/10/11 职场文书
标枪加油稿
2015/07/22 职场文书
2016年优秀共产党员先进事迹材料
2016/02/29 职场文书
Golang标准库syscall详解(什么是系统调用)
2021/05/25 Golang
在ubuntu下安装go开发环境的全过程
2022/08/05 Golang