python配置tab自動不全

請參考個人博客
python配置tab自動不全


說明

有時候Centos系統默認安裝的python進入交互模式下不能使用tab快捷鍵功能,這個時候需要自己進行相關配置

tab.py

#!/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.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

配置

這里以python.27為例,把
  • 上面的tab.py 放到 /usr/lib/python2.7/site-packages/ 下面,需要先導入sys,再倒入tab模塊
  • 可以把tab.py的內容放到 /root/.pythontab文件中,然后在/root/.bash_profile中添加export PYTHONSTARTUP=~/.pythontab, 這種方式不需要再次導入 tab 模塊

測試

root@pts/2 $ python 
Python 2.7.5 (default, Sep 15 2016, 22:37:39) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import tab
>>> sys.
sys.__class__(              sys.__stdout__              sys.executable              sys.path
sys.__delattr__(            sys.__str__(                sys.exit(                   sys.path_hooks
sys.__dict__                sys.__subclasshook__(       sys.exitfunc(               sys.path_importer_cache

附加

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 namespace 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.

簡單來說就是這個文件會在第一次進入交互模式的時候會被執行,所以把tab.py 加入到這個變量設定的文件中就可以達到自動導入tab.py,實現自動補全的功能
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容