Why to view and clear your command history

If you use the Terminal for troubleshooting, you probably know that you can press the Up Arrow key to see your recently used commands. At the prompt, you'll just keep tapping that key until you see the command you'd like to execute again, and then press Return. Apparently, that was the last command I used.

Using the RStudio Terminal

Now if only I had any memory of why I needed to do that. Anyway, that sure is a groovy way to avoid retyping a lot of stuff, but did you know that you can actually search through the commands you've typed instead of just looking through your history?

So if you remember, for example, that you ran a defaults write command a while back but don't know what it was, you can search through Terminal to find it. Long listing of parent directory cd..

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Translate all. Mar 13, Translates into French. Jan 4, Update korean translation.

Clear the terminal's command history - Mac OS X Hints

Feb 24, Sep 18, Russian translation added. Oct 10, Apr 19, Translation into Ukrainian. As in the example above, the primary use of this feature is to simply enter two exclamation points to reference the last command, but when used with the output of the "history" command you can be more specific on the commands you target. For example, in the history output you will see a number next to each command that identifies it, and you can run a desired command again by its number:.

Therefore, if you have supplied multiple commands but wish to access a prior one to run as admin and redirect its output to a text file for troubleshooting purposes, then you can output a brief history with "history 10" and then run the command "sudo! You can also rerun a prior command by its relative position using negative numbers. For example, to run the last command you can enter "!!

MAC OS - How to Open Terminal (Command Prompt) on Mac

In addition to referencing the history by number, you can do so by name. For example, if you have run a command that begins with "pico" to invoke this editor on a document but since then have changed directory or listed directory contents a few times, you can immediately repeat the command that began with "pico" by the following command:. This can be used to locate the last command that begins with the provided text, but a final approach here is to repeat the last command that includes the mentioned text, which may be convenient to pick out one of many commands that were run with "sudo.

The question marks here are like wild cards, so you can remove the one at the end to have the first command run that ended in the included string, which might be a convenient way to re-edit a specific script file if you have just run commands to edit a number of different scripts. NOTE: A similar approach to the exclamation point can be done by entering the command "fc -s" alone or followed by the name of the prior command to run. This can be aliased or scripted as a separate command, but is a bit redundant to the use of the exclamation point. The Terminal history will not be written to the history file until you close the current Terminal session, so this approach can be useful for transfering commands from one Terminal session to another.

For instance, if you have two Terminals open and enter a complex command in one, to get it to the second Terminal window, simply close the first this will write its history buffer to the history file , and then in the second window type "history -r" to re-read the saved history file. With these actions done, then scroll up using the arrow keys in the second Terminal window you can also use the history function to location the position of the command from the closed Terminal session.

Installing oh-my-zsh:

After you scroll through the second window's history buffer it will start revealing the commands in the history file, with the first one being the complex command from the initial Terminal window. Using the history in the manners mentioned can be useful, but in addition you may wish to remove an entry or two from the history, or clear it completely if needed. To do this, you can either close the Terminal windows and then remove the history file, or in what is a simpler approach, run "history -c" in the Terminal to clear the history, followed by entering "history -w" to write the now empty history file.

These steps will clear the entire history, which may not always be desired, especially if you only wish to remove a single command that perhaps shows the location of a private file or is one you would perhaps not wish to inadvertently run again.