资讯专栏INFORMATION COLUMN

python-tab补全模块

sixgo / 3373人阅读

摘要:补全模块安装模块编写补全模块内容查看模块内容版本一下使用这行内容下使用这行内容版本二查看默认可以查找到的包目录就是默认可以模块的路径把移动到查看的目录中的一个即可测试结果使用补全每次都要先导入补全模块

python tab补全模块 安装readline模块
[root@zabbix-server ~]# pip install readline
编写补全模块内容

查看模块内容:
版本一
[root@zabbix-server ~]# cat tab.py

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# __created by junxi__

# The script is used by python tab Completion script
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")        # linux下使用这行内容
# histfile = os.path.join(os.environ["HOMEPATH"], ".pythonhistory")   # win10下使用这行内容
try:
    readline.read_history_file(histfile)
except IOError:
    pass
# atexit.register(readline.write_history_file, histfile)
# del histfile

版本二
[root@zabbix-server ~]# cat tab.py

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# __created by junxi__

# The script is used by python tab Completion script

import readline,rlcompleter

### Indenting
class TabCompleter(rlcompleter.Completer):
    """Completer that supports indenting"""
    def complete(self, text, state):
        if not text:
            return ("    ", None)[state]
        else:
            return rlcompleter.Completer.complete(self, text, state)

readline.set_completer(TabCompleter().complete)

### Add autocompletion
if "libedit" in readline.__doc__:
    readline.parse_and_bind("bind -e")
    readline.parse_and_bind("bind "	" rl_complete")
else:
    readline.parse_and_bind("tab: complete")

### Add history
import os
histfile = os.path.join(os.environ["HOME"], ".pyhist")
try:
    readline.read_history_file(histfile)
except IOError:
    pass
import atexit
atexit.register(readline.write_history_file, histfile)
del histfile
查看python默认可以查找到的包目录(就是默认可以import 模块的路径)
[root@zabbix-server ~]# python
Python 2.7.5 (default, Nov  6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
["", "/usr/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg", "/usr/lib64/python27.zip", "/usr/lib64/python2.7", "/usr/lib64/python2.7/plat-linux2", "/usr/lib64/python2.7/lib-tk", "/usr/lib64/python2.7/lib-old", "/usr/lib64/python2.7/lib-dynload", "/usr/lib64/python2.7/site-packages", "/usr/lib64/python2.7/site-packages/gtk-2.0", "/usr/lib/python2.7/site-packages"]
把tab.py移动到sys.path查看的目录中的一个即可
[root@zabbix-server ~]# mv tab.py /usr/lib/python2.7/site-packages
测试结果(使用tab补全每次都要先导入tab.py补全模块)
[root@zabbix-server ~]# python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tab
>>> import os
>>> os.
Display all 249 possibilities? (y or n)
os.EX_CANTCREAT             os.__package__              os.listdir(
os.EX_CONFIG                os.__reduce__(              os.lseek(
os.EX_DATAERR               os.__reduce_ex__(           os.lstat(
os.EX_IOERR                 os.__repr__(                os.major(
os.EX_NOHOST                os.__setattr__(             os.makedev(
os.EX_NOINPUT               os.__sizeof__(              os.makedirs(
os.EX_NOPERM                os.__str__(                 os.minor(

ok

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/41609.html

相关文章

  • 使用 Vim 搭建 Lua 开发环境

    摘要:使用搭建开发环境原文链接是一个模块化的,针对这一语言的支持主要依靠模块以及与之相关的其它模块。在阅读这篇文章之前,可以先阅读使用搭建基础的开发环境,对语言相关以外的功能有一个大致的了解。该模块默认使用这一异步语法检查工具。 使用 Vim 搭建 Lua 开发环境 原文链接:https://spacevim.org/cn/use-v... SpaceVim 是一个模块化的 Vim IDE...

    Freeman 评论0 收藏0
  • Tab模块命令行补全

    #!/usr/bin/env 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.p...

    LinkedME2016 评论0 收藏0
  • atom中最好的js代码片段

    摘要:使用编辑器有一段时间一直没找到比较顺手的代码片段补全于是决定自己整合一个全的于是便有了这个代码片段包中包名叫有如下特性集成代码补全集成中模块的代码补全集成中模块的代码补全集成集成中操作的代码集成中和模块的补全集成中代码补全集成中集成中的 使用atom编辑器有一段时间,一直没找到比较顺手的代码片段补全,于是决定自己整合一个全的,于是便有了这个代码片段包,atom中包名叫 best-js-...

    hsluoyz 评论0 收藏0
  • SpaceVim - 一个模块化加载的vim配置

    摘要:是一个社区驱动的模块化配置集合,其中包含了多种功能模块,并且针对做了功能优化。有多种功能模块可供选择,用户只需要选择需要的模块,就可以配置出一个适合自己的开发环境。 SpaceVim 中文手册 showImg(https://segmentfault.com/img/remote/1460000007968382);showImg(https://segmentfault.com/im...

    megatron 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<