iPython类似Shell的命令历史记录


24

对于不在ubuntu上进行python编程的人来说,ipython是类固醇上的python shell,但它具有这一惊人的功能,它不仅可以基于已知名称自动完成(例如,按tab时bash的方式相同),但是,如果您开始键入命令并按下,它不会滚动浏览整个历史记录(例如bash),而只会滚动显示以相同字母组成的最近命令。

因此,如果您执行了一些长命令,例如scp -r -P 8000 -l user server.com:~/dir/to/copy ./后面跟随了其他几个命令。如果您开始键入scp并按下,bash将显示之前显示的命令,而不是滚动浏览整个历史记录。

bash有这样的扩展名吗?或是否有提供这种功能的外壳?

Answers:


26

Bash也确实具有该功能,但是默认情况下未启用它。您可以通过以下方式将其绑定到上/下光标~/.inputrc

"\e[A": history-search-backward
"\e[B": history-search-forward

我更喜欢将其绑定到Ctrl+ up / down:

"\e[1;5A": history-search-backward
"\e[1;5B": history-search-forward

编辑:为了保护ctrl+leftctrl+right对整个单词移动前进和后退,还包括在这些行~/.inputrc的文件:

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word

使用此技巧,现在我无法在终端中使用Ctrl +向左/向右跳转到下一个/上一个单词,这对我来说是毁灭性的。也许有解决方法?
zetah 2012年

@zetah我编辑了答案以解决单词移动的问题
2013年

1
要保留所有默认值,您还可以添加$include /etc/inputrc,最好在第一行。
Tulio Casagrande

9

尝试按Ctrl+ R,然后输入几个字母。它也以相反的顺序工作。


6

并且不要忘记bash中出色的历史记录扩展快捷方式。1个

我会在手册页中发布一些节选,以防您没有在手臂上刺青(或记住)。

   Event Designators  
       An event designator is a reference to a command line entry in the  his
       tory list.

       !      Start  a  history substitution, except when followed by a blank,
              newline, carriage return, = or ( (when the extglob shell  option
              is enabled using the shopt builtin).
       !n     Refer to command line n.
       !-n    Refer to the current command line minus n.
       !!     Refer to the previous command.  This is a synonym for `!-1'.
       !string
              Refer to the most recent command starting with string.
       !?string[?]
              Refer  to the most recent command containing string.  The trail‐
              ing ? may be omitted if string is followed immediately by a new‐
              line.
       ^string1^string2^
              Quick  substitution.  Repeat the last command, replacing string1
              with string2.  Equivalent to ``!!:s/string1/string2/'' (see Mod‐
              ifiers below).
       !#     The entire command line typed so far.

我经常使用该功能来引用上一个命令的最后一个“单词”。例如,

mkdir /foo/shmoo/adir.horribilus.foo
cp file1 file2 file3 file4 !$ 
ls -l !$

在这两种情况下,都是!$matchs /foo/shmoo/adir.horribilus.foo


1 ...取自csh。为了减轻bash功能盗窃的范围,bash手册页说

   The shell supports a history expansion feature that is similar  to  the
   history  expansion in csh.  

因此,这是“相似的”。任何一种都可能闯入cshtcsh。或您不使用的任何csh后代,因为它不如bash


0

还有另外一种类似于@ AK2提到上面,但你不必创建一个新的文件.inputrc文件。

相反,如果您具有sudo权限,则可以在/ etc / inputrc文件中启用它。此文件中有各种键盘设置,包括历史记录搜索功能(至少适用于18.04)。/ etc / inputrc的摘录为:

# alternate mappings for "page up" and "page down" to search the history
# "\e[5~": history-search-backward
# "\e[6~": history-search-forward

只需使用sudo文件编辑器(例如$ sudo vim)取消对底部两行的注释,新的终端会话将具有历史记录搜索功能(适用于所有用户)。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.