为python命令行添加Tab键自动补全

2016-09-20  本文已影响434人  invalid_name
  1. 编写一个Tab键自动补全功能的脚本
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['HOMEPATH'], '.pythonhistory')   
  try:   
      readline.read_history_file(histfile)   
  except IOError:   
      pass   
  atexit.register(readline.write_history_file, histfile)   
           
  del os, histfile, readline, rlcompleter

2.脚本写好后保存为tab.py 文件,并放到python安装目录下的 Lib\site-packages 文件夹下。

3.脚本中有使用到 readline 模块, 所以还要安装 readline

#使用pip安装 readline
pip install readline

4.使用方法
import tab

Paste_Image.png
上一篇 下一篇

猜你喜欢

热点阅读