Python实现Tab自动补全和历史命令管理的方法_python

本文实例讲述了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. 下载插件:
       下载地址:http://www.jb51.net/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
, 管理
, 方法
, tab
, 自动补全
历史命令
tab命令补全的实现、python tab补全、python3 tab补全、linux python tab补全、mac python tab补全,以便于您获取更多的相关知识。

时间: 2024-08-08 16:17:07

Python实现Tab自动补全和历史命令管理的方法_python的相关文章

给Python IDLE加上自动补全和历史功能_python

许多时候,我们使用Python,并不用写一个程序,一些不复杂的任务,我更喜欢在 IDLE(也就是交互式提示模式)下输入几行代码完成.然而,在这个模式下编辑代码,也有不够便利的地方,最主要的就是,不能用Tab自动补全,不能记忆 上一次输入的命令(没办法,谁让我们在Shell下习惯了呢). 这时候,我们可以直接使用Python启动脚本,解决这个问题. 启动脚本的程序非常简单,这里不多说明,只给出代码: import readline import rlcompleter import atexit

PHP自动补全表单的两种方法

效果图: 第一种:从数据库中检索之后补全 第二种:邮箱等纯前端的补全 先说第二种,使用开源的插件,所以相对简单. github上面的项目 completer. https://github.com/fengyuanchen/completer 做法特别容易,github上面有详细的文档. 一开始尝试用这个来配上自己的后台代码,做成第一种的自动补全,搞了半天失败了.可能本人js太差,改动太多的话,代码很复杂,除非认真研究上面这个开源项目. 主要失败在我在后台数据库找出来的完整的模糊查询得到的数据,

python tab自动补全脚本

自动安装方法:脚本http://www.aliyun.com/zixun/aggregation/18479.html">下载地址(linux下可用wget下载):http://www.linuxyw.com/scripts/python-tab.sh执行完脚本后,需要断开SSH重新登陆才可自动补全. 手动修改方法:1.进入用户宿主目录,建立 .pythonstartup文件,比如你现在是root用户,则进入/root/,然后vim .pythonstartup.pythonstartup

在Ubuntu中实现python按tab键补全

 刚学习python,其实一切都很好接受,因为有过C语言的基础,感觉一切都来得那么自然,python极其精简的语法,让我真心是爱上这种语言!相信往后python一定会在我的IT生涯中大放光彩!     好,废话说完了,入正题. 1.问题引出:默认情况下python交互界面的tab键         在linux下,或在路由器.交换机上,按tab键按得很爽,什么不完整的,tab一下都出来了,无奈,在linux中安装的python,默认情况是没有tab功能的,也就是在python的交互界面中,tab

python实现定制交互式命令行的方法_python

Python的交互式命令行可通过启动文件来配置. 当Python启动时,会查找环境变量PYTHONSTARTUP,并且执行该变量中所指定文件里的程序代码.该指定文件名称以及地址可以是随意的.按Tab键时会自动补全内容和命令历史.这对命令行的有效增强,而这些工具则是基于readline模块实现的(这需要readline程序库辅助实现). 此处为大家举一个简单的启动脚本文件例子,它为python命令行添加了按键自动补全内容和历史命令功能. [python@python ~]$ cat .python

Python 自动补全(vim)_python

一.vim python自动补全插件:pydiction 可以实现下面python代码的自动补全: 1.简单python关键词补全 2.python 函数补全带括号 3.python 模块补全 4.python 模块内函数,变量补全 5.from module import sub-module 补全 想为vim启动自动补全需要下载插件,地址如下: http://vim.sourceforge.net/scripts/script.php?script_id=850 https://github

转:VIM python 自动补全插件:pydiction

1.这是一个相当不错的 Python 代码自动完成的脚本. 2. 3.可以实现下面python代码的自动补全: 4. 5.简单python关键词补全 6.python 函数补全带括号 7.python 模块补全 8.python 模块内函数,变量补全 9.from module import sub-module 补全 10.pydiction 插件安装 11. 12.pydiction 1.0 之后版本安装配置 13. 14.适用VIM7之后的版本. 15. 16.1.python_pydic

详解jQuery UI库中文本输入自动补全功能的用法_jquery

自动补全(autocomplete),是一个可以减少用户输入完整信息的UI 工具.一般在 输入邮箱.搜索关键字等,然后提取出相应完整字符串供用户选择. 一.调用autocomplete()方法 $('#email').autocomplete({ source : ['aaa@163.com', 'bbb@163.com', 'ccc@163.com'], }); 二.修改autocomplete()样式   由于autocomplete()方法是弹窗,然后鼠标悬停的样式.通过Firebug 想

editplus-Editplus写python自动补全问题

问题描述 Editplus写python自动补全问题 Editplus写python. 从网上下载的python.acp自动补全文件是空格补全,很不习惯呀.想问问能不能改成tab键补全 解决方案 所以你应该用vscode vim emac这样的,支持插件扩展的编辑器. 解决方案二: 用pycharm吧 功能强大 自动补齐 语法检查 pep标准检测 解决方案三: 这里有个教程,有空可以参考配置一下:http://www.pythoner.com/178.html 解决方案四: 你去修改那个acp文