码迷,mamicode.com
首页 > 其他好文 > 详细

交互式输入编辑与历史命令补全

时间:2015-10-10 09:04:28      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

1.行编辑

        如果支持,在交互式命令输入中,当前行可以使用以下的快捷键进行编辑:

        Ctrl+A:将光标移动到行开始位置

        Ctrl+E:将光标移动到行结束位置

        Ctrl+B:将光标往左移动一个位置

        Ctrl+F:将光标往右移动一个位置

        Backspace擦除光标左边的一个字符

        Ctrl+D:擦除光标右侧一个字符

        Ctrl+K:擦除光标右侧所有字符

2.历史命令补全

        历史命令补全工作原理如下:将所有从命令行中输入的非空行保存在历史缓存中,当你在新的一行中输入命令 时,使用Ctrl+p输入历史命令中的上一条命令,使用Ctrl+N输入历史命令中的下一条命令。

 3.快捷键设置

        通过~/.inputrc可以自定义快捷键。格式如下所示

key-name: function-name
#or
"string": function-name
        也可以使用set命令进行设置:
  1. set option-name value
        例如:
  1. # I prefer vi-style editing:
    set editing-mode vi
    # Edit using a single line:
    set horizontal-scroll-mode On
    # Rebind some keys:
    Meta-h: backward-kill-word
    "\C-u": universal-argument
    "\C-x\C-r": re-read-init-file
        注意在Python中Tab键默认绑定的方法是插入一个Tab字符,而不是补全文件名,你可以自己进行修改:
  1. Tab: complete
        自动补全变量名或者模块名也是可行的,为了在命令行中支持这个功能,需要在启动文件中输入以下代码:
  1. import rlcompleter, readline
    readline.parse_and_bind(tab: complete)
        这样就将Tab键绑定到了命令补全函数,当连续两次单击tab键的时候,它将会检查Python中声明的变量名,当前的局部变量名,,以及可用的模块名。例如下面的例子,
 1 # Add auto-completion and a stored history file of commands to your Python
 2 # interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
 3 # bound to the Esc key by default (you can change it - see readline docs).
 4 #
 5 # Store the file in ~/.pystartup, and set an environment variable to point
 6 # to it:  "export PYTHONSTARTUP=~/.pystartup" in bash.
 7 
 8 import atexit
 9 import os
10 import readline
11 import rlcompleter
12 
13 historyPath = os.path.expanduser("~/.pyhistory")
14 
15 def save_history(historyPath=historyPath):
16     import readline
17     readline.write_history_file(historyPath)
18 
19 if os.path.exists(historyPath):
20     readline.read_history_file(historyPath)
21 
22 atexit.register(save_history)
23 del os, atexit, readline, rlcompleter, save_history, historyPath

 

交互式输入编辑与历史命令补全

标签:

原文地址:http://www.cnblogs.com/fireflow/p/4865675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!