2016-05-12 81 views
2

當我運行一個使用somevar = sys.stdin.readline()的程序時,在輸入文本到標準輸入時,我無法使用我的vi命令(我在我的.bashrc中有set -o vi)。在從python腳本中讀取標準輸入時,有沒有辦法啓用這個功能?我正在使用python2.7。Python:vi模式從標準輸入讀取時

+1

見:https://pymotw.com/2/readline/,之後,https://docs.python.org/2/library/readline .html和https://cnswww.cns.cwru.edu/php/chet/readline/rltop.html –

+0

但請注意,'readline'模塊隻影響'raw_input()',並且對sys.stdin沒有影響.readline()'。 –

回答

2

如果您安裝了readline模塊,請嘗試使用它。下面的代碼片段,我從這個page複製:

import readline 

readline.parse_and_bind('tab: complete') 
readline.parse_and_bind('set editing-mode vi') 

while True: 
    line = raw_input('Prompt ("stop" to quit): ') 
    if line == 'stop': 
     break 
    print 'ENTERED: "%s"' % line