1

enter image description here

When use the terminal in macOS, press command+shift+N can turn on this "New Command" window, which doesn't store all the commands executed in the terminal.

From ~/.zsh_history, I can see that the commands were executed in the form of xxxxx;exit

By deleting those lines from ~/.zsh_history, the wrong commands still remain in the New Command Window. How can I delete those lines like asitop and asitop show cores so that they won't show in New Command.

Thanks

PS: This Command+Shift+N New Window is quite useful, feels like a with ... as ... function in python and will completely quit when closing, unlike directly executing some commands which may leave frozen threads after ctrl+c.

1 Answer 1

1

That history is stored in the preferences. You clear it this way:

defaults delete com.apple.Terminal CommandHistory

Making selective deletions is more complicated. I haven't tested these steps myself, but they should work.

Step 1

The output of this command

defaults read com.apple.Terminal CommandHistory

will look like this:

 (
    "first line",
    "second line",
    ...
    "last line"
 )

Copy this text to an editor and make the changes. Be sure to end each line except the last with a comma. Copy the edited text to the Clipboard.

Step 2

Enter this (partial) command:

defaults write com.apple.Terminal -array CommandHistory '

You should get a secondary prompt on a new line, since the single quote is unmatched. Paste the text from the Clipboard, then enter a single quote. You may have to quit and relaunch Terminal to see the effect.

2
  • I don't want to clear the entire history. Instead, I want to delete some of them. The rest are useful and I don't want to type them again...
    – Crear
    Commented Nov 5 at 19:30
  • Accepted and copy-pasted some of the commands back. Thanks!
    – Crear
    Commented Nov 5 at 19:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .