`
willzh
  • 浏览: 296768 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

readline

阅读更多
There are two ways to configure the underlying readline library, using a configuration file or the parse_and_bind() function. Configuration options include the keybinding to invoke completion, editing modes (vi or emacs), and many other values. Refer to the GNU readline library documentation for details.

The easiest way to enable tab-completion is through a call to parse_and_bind(). Other options can be set at the same time. This example changes the default editing controls to use “vi” mode instead of the default of “emacs”. To edit the line, press ESC then normal vi navigation keys.
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

The same configuration can be stored as instructions in a file read by the library with a single call. If myreadline.rc contains:
# Turn on tab completion
tab: complete

# Use vi editing mode instead of emacs
set editing-mode vi


the file can be read with read_init_file():
import readline

readline.read_init_file('myreadline.rc')

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

相关推荐

Global site tag (gtag.js) - Google Analytics