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使用asyncio包处理并发详解
Sep 09 Python
python 3.6 +pyMysql 操作mysql数据库(实例讲解)
Dec 20 Python
matplotlib绘制动画代码示例
Jan 02 Python
python实现守护进程、守护线程、守护非守护并行
May 05 Python
python读取Excel实例详解
Aug 17 Python
Python设计模式之工厂方法模式实例详解
Jan 18 Python
代码实例讲解python3的编码问题
Jul 08 Python
python3.6中@property装饰器的使用方法示例
Aug 17 Python
Python中filter与lambda的结合使用详解
Dec 24 Python
Python3 selenium 实现QQ群接龙自动化功能
Apr 17 Python
pycharm第三方库安装失败的问题及解决经验分享
May 09 Python
Keras 切换后端方式(Theano和TensorFlow)
Jun 19 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
全国FM电台频率大全 - 15 山东省
2020/03/11 无线电
打造计数器DIY三步曲(下)
2006/10/09 PHP
php实现用户在线时间统计详解
2011/10/08 PHP
php+Ajax无刷新验证用户名操作实例详解
2019/03/04 PHP
php自定义排序uasort函数示例【二维数组按指定键值排序】
2019/06/19 PHP
node-webkit打包成exe文件被360误报木马的解决方法
2015/03/11 Javascript
JavaScript使用DeviceOne开发实战(三)仿微信应用
2015/12/02 Javascript
SublimeText自带格式化代码功能之reindent
2015/12/27 Javascript
莱鸟介绍javascript onclick事件
2016/01/06 Javascript
详解javascript跨浏览器事件处理程序
2016/03/27 Javascript
BootStrap glyphicons 字体图标实现方法
2016/05/01 Javascript
node.js 动态执行脚本
2016/06/02 Javascript
封装获取dom元素的简单实例
2016/07/08 Javascript
JS实现电商放大镜效果
2017/08/24 Javascript
Vue组件之Tooltip的示例代码
2017/10/18 Javascript
微信小程序中进行地图导航功能的实现方法
2018/06/29 Javascript
微信小程序实现弹出菜单
2018/07/19 Javascript
利用Node.js批量抓取高清妹子图片实例教程
2018/08/02 Javascript
小试小程序云开发(小结)
2019/06/06 Javascript
layui之table checkbox初始化时选中对应选项的方法
2019/09/02 Javascript
Python实现的一个简单LRU cache
2014/09/26 Python
详细介绍Ruby中的正则表达式
2015/04/10 Python
python通过cookie模拟已登录状态的初步研究
2016/11/09 Python
windows环境下tensorflow安装过程详解
2018/03/30 Python
解决Python 命令行执行脚本时,提示导入的包找不到的问题
2019/01/19 Python
python实现两个文件夹的同步
2019/08/29 Python
浅谈Python的方法解析顺序(MRO)
2020/03/05 Python
Fossil美国官网:化石手表、手袋、首饰及配饰
2019/02/17 全球购物
舞会礼服和舞会鞋:PromGirl
2019/04/22 全球购物
前处理组长岗位职责
2014/03/01 职场文书
幼儿园小班教师寄语
2014/04/03 职场文书
党员承诺书怎么写
2014/05/20 职场文书
预备党员公开承诺书
2014/05/28 职场文书
2014年银行客户经理工作总结
2014/11/12 职场文书
实习单位指导教师评语
2014/12/30 职场文书
安装harbor作为docker镜像仓库的问题
2022/06/14 Servers