Searching your command history on macOS terminal

落花浮王杯 提交于 2020-06-24 03:22:11

问题


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

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


回答1:


How about using Ctrl+R for searching on the Terminal Utility in Mac for searching on the command history,

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

Well for controlling how long the history would be retained that depends on a few shell environment variables, HISTFILESIZE which is nothing but number of lines of history you want to retain. Set a huge value for it in .bash_profile for it to take effect

HISTFILESIZE=10000000 



回答2:


Use Ctrl + R for searching a command from history in Terminal.

(reverse-i-search)`': 

Type any substring of the command you want to search e.g. 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 + R for next match until you find your command.

Once you found your command press Return to execute it.

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

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




回答3:


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:

nano ~/.bash_history

Or open with your text editor (default is 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.




回答4:


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

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

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 .inputrc to bind the up/down arrow key to history-search-backward and history-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 .bashrc rather than .inputrc, you can use:

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



回答5:


Use this command -

  history

This works on both OSX and Linux.

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

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

 echo $HISTSIZE



回答6:


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.




回答7:


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 search navigate back to history, match previous commands increamentally and return the entire command.

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



来源:https://stackoverflow.com/questions/41780746/searching-your-command-history-on-macos-terminal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!