bash 在 macOS 终端上搜索您的命令历史记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41780746/
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-09-08 22:29:48  来源:igfitidea点击:

Searching your command history on macOS terminal

bashmacoshistory

提问by Aipi

What is the shortcut to search my command history in macOS terminal?

在 macOS 终端中搜索我的命令历史记录的快捷方式是什么?

For how long is the history available for searching? Where is it stored?

历史记录可用于搜索多长时间?它存储在哪里?

回答by Inian

How about using Ctrl+Rfor searching on the TerminalUtility in Macfor searching on the command history,

如何使用Ctrl+Rfor 在Terminal实用程序中Mac搜索命令历史记录,

dudeOnMac: freddy$ whoami
freddy
(reverse-i-search)`who': whoami

Well for controlling how long the historywould be retained that depends on a few shellenvironment variables, HISTFILESIZEwhich is nothing but number of lines of history you want to retain. Set a huge value for it in .bash_profilefor it to take effect

用于控制history将保留多长时间取决于一些shell环境变量,HISTFILESIZE这只是您要保留的历史行数。为其设置一个巨大的值以使其.bash_profile生效

HISTFILESIZE=10000000 

回答by Sahil Chhabra

Use Ctrl+ Rfor searching a command from history in Terminal.

使用Ctrl+R从终端的历史记录中搜索命令。

(reverse-i-search)`': 

Type any substring of the command you want to search e.g. grep

键入要搜索的命令的任何子字符串,例如 grep

(reverse-i-search)`grep': grep "XYZ" abc.txt

It will return the latest command that matches your input. If that is not the command you were searching for, keep pressing Ctrl+ Rfor next match until you find your command.

它将返回与您的输入匹配的最新命令。如果那不是您要搜索的命令,请继续按Ctrl+R进行下一个匹配,直到找到您的命令。

Once you found your command press Returnto execute it.

一旦你找到你的命令按下Return执行它。

If you want to exit without running any command, press Ctrl+ G

如果你想退出而不运行任何命令,请按Ctrl+G

PS: This answer is same as suggested by Inian, just giving more details for easy usage.

PS:此答案与 Inian 建议的相同,只是提供了更多详细信息以方便使用。

回答by balazs630

The command history is stored under your home folder in a hidden file called .bash_history. To view it's content in nano, use the following command in Terminal:

命令历史记录存储在您的主文件夹下一个名为.bash_history的隐藏文件中。要在 nano 中查看其内容,请在终端中使用以下命令:

nano ~/.bash_history

Or open with your text editor (default is TextEdit):

或者用你的文本编辑器打开(默认是 TextEdit):

open ~/.bash_history

In my case it's a very long list and as I scroll through seems like the last ~500 command is stored here.

就我而言,这是一个很长的列表,当我滚动浏览时,似乎最后一个 ~500 命令存储在这里。

回答by matt123788

Migrating an answer to SO from this answeron the Unix and Linux Stack Exchange:

从Unix 和 Linux Stack Exchange 上的这个答案迁移到 SO 的答案

Pressing ctrl+Rwill open the history-search-backward. Now start typing your command, this will give the first match. By pressing ctrl+Ragain (and again) you can cycle through the history.

ctrl+R将向后打开历史搜索。现在开始输入您的命令,这将给出第一个匹配项。通过再次(再一次)按ctrl+ R,您可以循环浏览历史记录。

If you like to be super lazy you can bind the up/down arrow keys to perform this search, I have the following in my .inputrcto bind the up/down arrow key to history-search-backwardand history-search-forward:

如果你喜欢超级懒惰,你可以绑定向上/向下箭头键来执行这个搜索,我有以下内容.inputrc将向上/向下箭头键绑定到history-search-backwardhistory-search-forward

# Key bindings, up/down arrow searches through history
"\e[A": history-search-backward
"\e[B": history-search-forward
"\eOA": history-search-backward
"\eOB": history-search-forward

Just type something (optional), then press up/down arrow key to search through history for commands that begin with what you typed.

只需输入一些内容(可选),然后按向上/向下箭头键即可在历史记录中搜索以您输入的内容开头的命令。

To do this in .bashrcrather than .inputrc, you can use:

要在.bashrc而不是 中执行此操作.inputrc,您可以使用:

bind '"\e[A": history-search-backward'

回答by Manas Sharma

Use this command -

使用这个命令 -

  history

This works on both OSX and Linux.

这适用于 OSX 和 Linux。

History is stored in ~/.zsh_history or ~/.bash_history or ~/.history depending on your shell.

历史记录存储在 ~/.zsh_history 或 ~/.bash_history 或 ~/.history 中,具体取决于您的外壳。

History is stored for 1000 or 2000 lines depending on your system.

历史记录可存储 1000 或 2000 行,具体取决于您的系统。

 echo $HISTSIZE

回答by Hung Om

For those who want to search specific command from history, you can do so with reverse-i-search.Reverse search allow you to type in any key words(any) that is part of the command you are looking for and reverse searchnavigate back to history, match previous commands increamentally and return the entire command.

对于那些想要从历史记录中搜索特定命令的人,您可以使用reverse-i-search.Reverse search 来实现。反向搜索允许您输入属于您正在查找的命令的一部分的任何关键字(任何)并reverse search导航回历史记录,逐步匹配以前的命令并返回整个命令。

It is especially useful as when one cannot remember all handy lengthy commands they use often.To do reverse-search ctrl+ Rand type any clue you have and that will return your previous commands matching the words you type. Then once found the command, hit Enterto execute it directly from search.

当人们无法记住他们经常使用的所有方便的冗长命令时,它特别有用。要进行反向搜索ctrl+R并键入您拥有的任何线索,这将返回与您键入的单词匹配的先前命令。然后一旦找到命令,Enter直接从搜索中点击执行它。

回答by Cam_Aust

To review or recall recently used commands, you can just press the up arrow key to sequentially read back through the history stored in .bash_history.

要查看或调用最近使用的命令,您只需按向上箭头键即可依次读回 .bash_history 中存储的历史记录。