数か月前、私は標準の Python インタラクティブ インタープリターでタブ補完を実現する方法を詳述したブログ記事を書きました。IPython Unicode の問題が原因で標準インタープリターに切り替える必要がある場合があることを考えると、非常に便利であることがわかりました。
最近、私は OS X でいくつかの作業を行いました。不満なことに、このスクリプトは OS X のターミナル アプリケーションでは機能しないようです。OS X の経験のある方が、ターミナルでも動作するようにトラブルシューティングを手伝ってくれることを願っています。
以下のコードを再現しています
import atexit
import os.path
try:
import readline
except ImportError:
pass
else:
import rlcompleter
class IrlCompleter(rlcompleter.Completer):
"""
This class enables a "tab" insertion if there's no text for
completion.
The default "tab" is four spaces. You can initialize with '\t' as
the tab if you wish to use a genuine tab.
"""
def __init__(self, tab=' '):
self.tab = tab
rlcompleter.Completer.__init__(self)
def complete(self, text, state):
if text == '':
readline.insert_text(self.tab)
return None
else:
return rlcompleter.Completer.complete(self,text,state)
#you could change this line to bind another key instead tab.
readline.parse_and_bind('tab: complete')
readline.set_completer(IrlCompleter('\t').complete)
# Restore our command-line history, and save it when Python exits.
history_path = os.path.expanduser('~/.pyhistory')
if os.path.isfile(history_path):
readline.read_history_file(history_path)
atexit.register(lambda x=history_path: readline.write_history_file(x))
IrlCompleter
が真のタブで初期化されるように、ブログ投稿のバージョンから少し編集したことに注意してください。これは、ターミナルの Tab キーによって出力されるようです。