• Linux python <tab>自动补全


    为Python添加交互模式下TAB自动补全以及命令历史功能。

    1.获取python目录

    [root@localhost ~]# python
    Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.path
    ['', '/usr/lib/python2.6/site-packages/paramiko-1.7.7.1-py2.6.egg', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gst-0.10', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib64/python2.6/site-packages/webkit-1.0', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info']
    >>> 
    

    从上面看出python在我系统上的路径是  /usr/lib/python2.6/site-packages

    2.切换至该目录写个tab.py的脚本,脚本目录就是处理python中<tab>事件,脚本内容如下

     [root@localhost]# cd /usr/lib/python2.6/site-packages

    [root@localhost python2.6/]# vim tab.py

    #!/usr/bin/python
    # python tab 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.切换至自己主目录

    [root@localhost python2.6]# cd 
    [root@localhost  ~]# vim .bashrc 
    

    4. 增加环境变量

    export PYTHONSTARTUP=/usr/lib/python2.6/site-packages/tab.py  

    5.配置环境变量生效

    [root@localhost ~]# source .bashrc  
    

      

      

      

      

  • 相关阅读:
    Linux中连接mysql执行sql文件
    各种编码转化
    PHP 创建缩略图
    erlang入门之编译和运行
    mac下的virtualbox启动失败处理
    golang回调函数的例子
    cocos2d-x的popScene的动画效果
    ssdb的golang驱动的同步问题
    贴一段demo代码,演示channel之间的同步
    golang的指针到string,string到指针的转换
  • 原文地址:https://www.cnblogs.com/saneri/p/5714880.html
Copyright © 2020-2023  润新知