zoukankan      html  css  js  c++  java
  • 交互式输入编辑与历史命令补全

    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-xC-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
  • 相关阅读:
    mysql登录等
    软工实践寒假作业(1/2)
    结对作业二——顶会热词统计的实现
    基于okhttp的安卓端网络编程
    Le vent se lève, il faut tenter de vivre
    软件评测
    一道算法题
    结对作业一
    软工实践寒假作业(2/2)
    实验六:Mininet脚本实现控制交换机行为
  • 原文地址:https://www.cnblogs.com/fireflow/p/4865675.html
Copyright © 2011-2022 走看看