使用bash历史记录获取上一个命令,复制它然后"运行"它,但使用命令注释

ale*_*ale 78 linux bash command-line

只是一个提高我的bash技能的问题.我总是这样做:

$ history | grep some_long_command

...
...
123 some_long_command1.........
124 some_long_command2.........
...
Run Code Online (Sandbox Code Playgroud)

然后,我可以通过执行命令运行命令:

!123

但是,我经常想这样做:

some_long_command1foobar
Run Code Online (Sandbox Code Playgroud)

即在我运行它之前更改命令.您可以使用bash来运行此命令:

#some_long_command1

所以它得到了评论.

然后,我不必使用鼠标突出显示命令,编辑它然后运行它(我可以使用键盘 - 更快).

我想我可以写一个脚本来做它,但可能已经在某个地方内置了功能......?

谢谢.

Miq*_*uel 138

我建议您使用ctrl+r并开始键入该命令,而不是使用history 命令.当你按下箭头键就像去修改它一样,它将退出自动完成识别,并允许你在运行之前进行编辑.

更新:此外,如果您想循环包含您刚输入的字符串的不同命令,请继续按下 ctrl+r

  • 如果你使用vi keybindings(`set -o vi`),你也可以使用`/`来搜索历史记录并进行编辑. (8认同)
  • @ADTC我刚刚找到了一种方法来做你和我想要的事情:按`ctr + r`,开始打字,然后当你想找到包含它的所有命令时,只需按住`ctrl + r`即可.换句话说,你想用up/down做什么(以循环方式)用`ctrl + r`完成 (3认同)
  • DavidC,我见过其他人这样做,但它对我不起作用.当我输入命令的前几个字母时,我按下箭头,它忽略我输入的内容并浏览历史记录,好像我根本没有输入任何内容(从最新到最旧).你如何配置你的shell来完成这个? (2认同)

joe*_*t45 59

实际上,您只需附加:p命令即可打印它而无需实际运行它.例如:

$ ls -la
$ !!:p
Run Code Online (Sandbox Code Playgroud)

ls -la在不运行前打印出上一个命令,您只需按(向上)查找并编辑它即可.

你也可以

!123:p
Run Code Online (Sandbox Code Playgroud)

打印出123rd命令作为上一个命令.

  • +1最好的答案,因为它允许通常的历史命令来回答OP:`!mycmd:p`.如果你在Bash中打开了vi命令,那么Pursell的答案也同样好或者更好.如Miquel所提到的Ctrl-r是次佳选择. (2认同)

Pri*_*ley 18

您还可以尝试使用fc命令编辑历史记录中的命令.

WIKI说,

FC是UNIX上的标准程序,该目录或编辑和重新执行,先前命令输入到交互壳.fcbash shell中的内置 命令; help fc将显示使用信息.


除了反向增量搜索(Ctrl+ R),我们还有一些更多的bash快捷方式:

来自man bash:

previous-history (C-p)
    Fetch the previous command from the history list, moving back in the list. 
next-history (C-n)
    Fetch the next command from the history list, moving forward in the list. 
beginning-of-history (M-<)
    Move to the first line in the history. 
end-of-history (M->)
    Move to the end of the input history, i.e., the line currently being entered. 
reverse-search-history (C-r)
    Search backward starting at the current line and moving 'up' through the history as necessary. This is an incremental search. 
forward-search-history (C-s)
    Search forward starting at the current line and moving 'down' through the history as necessary. This is an incremental search. 
non-incremental-reverse-search-history (M-p)
    Search backward through the history starting at the current line using a non-incremental search for a string supplied by the user. 
non-incremental-forward-search-history (M-n)
    Search forward through the history using a non-incremental search for a string supplied by the user.
yank-nth-arg (M-C-y)
    Insert the first argument to the previous command (usually the second word on the previous line) at point. With an argument n, insert the nth word from the previous command (the words in the previous command begin with word 0). A negative argument inserts the nth word from the end of the previous command. Once the argument n is computed, the argument is extracted as if the "!n" history expansion had been specified.
yank-last-arg (M-., M-_)
    Insert the last argument to the previous command (the last word of the previous history entry). With an argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last argument of each line in turn. The history expansion facilities are used to extract the last argument, as if the "!$" history expansion had been specified. 
shell-expand-line (M-C-e)
    Expand the line as the shell does. This performs alias and history expansion as well as all of the shell word expansions. See HISTORY EXPANSION below for a description of history expansion. 
history-expand-line (M-^)
    Perform history expansion on the current line. See HISTORY EXPANSION below for a description of history expansion.
insert-last-argument (M-., M-_)
    A synonym for yank-last-arg. 
operate-and-get-next (C-o)
    Accept the current line for execution and fetch the next line relative to the current line from the history for editing. Any argument is ignored. 
edit-and-execute-command (C-xC-e)
    Invoke an editor on the current command line, and execute the result as shell commands.


Joh*_*nce 10

!123:gs/old/new/
Run Code Online (Sandbox Code Playgroud)

将运行命令123用字符串'new'替换字符串'old'.


Jam*_*ore 6

您可以通过按M- ^(mac上的option-shift-6)进入编辑模式.

输入:

!123M- ^

你将编辑命令#123.这有点像使用ctrl-r,但从感叹号语法开始.