Linux 使用 bash history 获取上一个命令,复制它,然后“运行”它,但注释命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11000410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:48:16  来源:igfitidea点击:

Using bash history to get a previous command, copy it and then 'run' it but with the command commented

linuxbashcommand-line

提问by ale

Just a question to improve my bashskills. I always do this:

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

$ history | grep some_long_command

...
...
123 some_long_command1.........
124 some_long_command2.........
...

I can then run the command the command I found by doing:

然后我可以通过执行以下操作来运行我找到的命令:

!123

!123

However, I often want to do this:

但是,我经常想这样做:

some_long_command1foobar

I.e. change the command before I run it. Can you use bash to run this command instead:

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

#some_long_command1

#some_long_command1

so it gets commented.

所以它被评论了。

Then I don't have to use my mouse to highlight the command, edit it and then run it (I can just use the keyboard - faster).

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

I suppose I could write a script to do it but there might already be functionality built in somewhere....?

我想我可以编写一个脚本来做到这一点,但可能已经在某处内置了功能......?

采纳答案by Miquel

I'd suggest instead of using the history command, you use ctrl+rand start typing that command. When you press an arrow key as if to go to modify it, it will drop out of autocomplete recognition, and will let you edit before running.

我建议您不要使用 history 命令,而是使用ctrl+r并开始键入该命令。当你按下一个箭头键好像要去修改它时,它会退出自动完成识别,并让你在运行前进行编辑。

UPDATE: also, if you want to cycle through the different commands that contain the string you just typed, keep on pressing ctrl+r

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

回答by John Lawrence

!123:gs/old/new/

Will run command 123 replacing the string 'old' with the string 'new'.

将运行命令 123,将字符串 'old' 替换为字符串 'new'。

回答by chepner

You can also put

你也可以放

shopt -s histverify

in your .bash_profile, which causes any history expansion to appear on your command line without running it, allowing you to edit before doing so.

在您的 中.bash_profile,这会导致任何历史扩展出现在您的命令行上而不运行它,允许您在这样做之前进行编辑。

回答by Prince John Wesley

You can also try fccommand to edit the command in the history.

您也可以尝试使用fc命令来编辑历史记录中的命令。

WIKI says,

维基说,

?fc?is a standard program on Unix that lists or edits and reexecutes, commands previously entered to an interactive shell. fcis a built-incommand in the bashshell; help fcwill show usage information.

?fc?是 Unix 上的标准程序,用于列出或编辑和重新执行先前输入到交互式 shell 的命令。fcbashshell的内置命令;help fc将显示使用信息。



Apart from reverse-incremental search(Ctrl+R), we have some more bashshortcuts:

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

From man 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.

回答by joeschmidt45

Actually, you can just append :pto the command to print it without actually running it. For example:

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

$ ls -la
$ !!:p

Will print out ls -laas the previous command without running it, and you can just press (up) to find it and edit it.

ls -la不运行就打印出之前的命令,你可以按(向上)找到它并编辑它。

You can also do

你也可以这样做

!123:p

to print out the 123rd command as your previous command.

打印出第 123 个命令作为您之前的命令。

回答by James Moore

You can get to edit mode by hitting M-^ (option-shift-6 on a mac).

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

Type this:

输入:

!123M-^

!123M-^

And you'll be editing command #123. It's sort of like using ctrl-r, but starting with exclamation-point syntax.

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

回答by Tgr

Instead of using the historycommand, bind history-search-backward/history-search-forwardto key shortcuts which can be remembered easily (I prefer PgUp/PgDown). To do that, put this into your .inputrcfile:

不要使用history命令,而是将history-search-backward/绑定history-search-forward到容易记住的快捷键(我更喜欢 PgUp/PgDown)。为此,请将其放入您的.inputrc文件中:

"<key code>":  history-search-backward
"<key code>":  history-search-forward

To get <key code>, type Ctrl-V <key>in the shell, and replace the starting ^[with \ein whatever was output.

要获得<key code>,类型Ctrl-V <key>的外壳,并更换起动^[\e在什么是输出。

After this is set up, you can just type someand press PgUp to get some_long_command. If you need some_long_command with_some_argbut there is a similar command some_long_command with_some_other_arglater in the history, you can cycle through until you reach it by typing someand then hitting PgUp repeatedly, or you can type some, hit PgUp, move the cursor to where the two commands start to differ, type a few characters and hit PgUp once more. This ability to quickly page through / differentiate between similar commands makes it in my opinion a much more comfortable tool than Ctrl-R.

设置完成后,您只需键入some并按 PgUp 即可获得some_long_command. 如果您需要some_long_command with_some_argsome_long_command with_some_other_arg在历史记录中稍后有类似的命令,您可以通过键入some然后重复点击 PgUp来循环直到到达它,或者您可以键入some,点击 PgUp,将光标移动到两个命令开始不同的地方,输入几个字符并再次点击 PgUp。在我看来,这种快速翻阅/区分类似命令的能力使其成为比Ctrl-R.

回答by Martin Dvorak

You may wan to try "suggest box"-like history https://github.com/dvorka/hstr- it reads Bash history and allows for quick navigation.

您可能想尝试“建议框”之类的历史记录https://github.com/dvorka/hstr- 它读取 Bash 历史记录并允许快速导航。

hh

呵呵

To get the last command simply type hh, navigate to the command and use right arrow to get it on command line (where you can edit it and/or add comment).

要获取最后一个命令,只需键入 hh,导航到该命令并使用向右箭头在命令行上获取它(您可以在其中编辑它和/或添加注释)。

回答by SudhakarM

^p to get the last typed command in unix/solaris

^p 获取 unix/solaris 中最后输入的命令

回答by chuck

Put

alias r='fc -s'

别名 r='fc -s'

in your .bashrc(home dir) then you can just type in

在您的.bashrc(主目录)中,然后您只需输入

r <whatever>

at the command prompt and you will execute a copy of the last <whatever>command (same params) that is in your history. just hit up arrow to see what you have executed if you feel the need.

在命令提示符下,您将执行<whatever>历史记录中最后一个命令(相同参数)的副本。如果您觉得需要,只需点击向上箭头即可查看您已执行的操作。