iPython喜欢Shell的命令历史

对于那些不在ubuntu上进行python编程的人来说,ipython是类固醇上的python shell,但它具有这个惊人的function,它不仅可以根据已知名称进行自动完成(即按Tab键时bash的function相同),但是如果你开始输入一个命令然后按下它,它就不会滚动整个历史记录(比如bash),而只会通过最近以相同字符串开头的命令滚动。

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

bash有这样的扩展吗? 或者有没有提供这种function的shell?

Bash也有这个function,但默认情况下没有启用。 您可以通过将其粘贴到~/.inputrc其绑定到上/下光标:

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

我更喜欢将它绑定到Ctrl +向上/向下:

 "\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 + R ,然后输入几个字母。 它也以相反的顺序工作。

并且不要忘记bash中奇妙的历史扩展捷径。 1

我正在发布一些摘录,如果你没有在他们的arm上纹身(或记住它们)。

  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 !$ 

在这两种情况下, !$匹配/foo/shmoo/adir.horribilus.foo


1 …取自csh。 为了减轻bashfunction被盗的范围,bash手册页说

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

所以,它是“相似的”。 任何这些都可能在cshtcsh破坏。 或者你没有使用的任何csh后代,因为它不像bash那么精彩。