Undo/Redo in Terminal

Could anybody explain how Undo and Redo work in Terminal?

If you open the Terminal’s Edit menu, Undo and Redo are listed as Command-Z and Shift-Command-Z, but they are always grayed out, and I did’t figured out how to make them work. (E.g., I tried to paste something and then to undo it using Command-Z.) How to make them work?

(To my surpise, to undo something, one can use Control-Underscore (or Control-Hyphen, at least on my US keyboard), but Control-Underscore, though it works, is not listed in Terminal’s menus, and I simply found it somewhere using Google.)

Sonoma 14.5

I may miss something and be completely on the wrong path here, but it does make perfectly sense that “Undo” and “Redo” cannot work in Terminal. By design. If you issue a command and hit enter, the command will be executed. The Terminal window is just the “terminal” that is accepting your commands. Those commands will be run on your Mac (basically different “apps” at times) or maybe even on remote computers. The Terminal app has no power to “undo” anything in different “apps” oder even on remote computers. I do not know if there is any scenario where Undo and Redo are active in Terminal. I would not be surprised if there is none.

Same with Safari, Firefox or whatever: you can visit websites. You can fill forms with your data. There is no undo after you have visited a website oder entered data. Undo and Redo are grayed out there, too.

Undo and Redo become active if an app has the capability to offer this functionality. If not, those options are grayed out.

Agreed, but then why they are included in the menu? In both Terminal and iTerm2.

Apple being Apple, probably. Keeping consistency with macOS design guidelines?

Katie

1 Like

I am no developer, so somebody else might chime in with more knowledge.

I think there are some standard elements in the menu that come with macOS. When an app is able to offer the command shown in the menu, it is active. If not, it is grayed out.

It does make sense to me because if it is grayed out, you know that this is a feature is not available at the moment. So if you are trying to redo or undo and it doesn’t work and it is grayed out in the menu, you know: not available.

Not sure creating fake menu items is encouraged or justified by Apple guidelines. BTW, macOS HIGs are very interesting reading, especially the old ones.

I don’t know about Terminal, but iTerm’s undo just restores your session if you accidentally close a window. I don’t think redo does anything.

In a command-line terminal on macOS, the standard undo and redo keyboard shortcuts like Cmd + Z and Cmd + Shift + Z typically do not work as they do in graphical text editors. However, you can use the following methods to achieve similar functionality:

Undo in Terminal:

  1. Undo a Typing Mistake:

• If you make a mistake while typing a command, you can often press Ctrl + U to delete the entire line or Ctrl + W to delete the last word.

• To undo a specific character, you can use Ctrl + H (which acts like a backspace).

  1. Undo a Previous Command (if executed):

• There’s no built-in undo feature for commands that have already been executed in the terminal. However, you can view and recall previous commands using the command history by pressing the Up Arrow key.

Redo in Terminal:

  1. Redo Typing:

• Terminals do not have a direct “redo” feature, but you can use Ctrl + Y to yank (paste) the most recently deleted text if you’ve used Ctrl + U, Ctrl + W, or Ctrl + K (which cuts text to the end of the line).

  1. Re-execute Commands:

• You can press the Up Arrow key to navigate through the history of commands and re-execute a command by pressing Enter.

People who often work with the terminal know these basic things. The thread is about why the menu item exists but seems to be always disabled.

@sangadi This and another recent post of yours feel a lot like LLM generations.

Not sure what exactly you meant to say. No, it’s not LLM.

I was replying to the other member. Edited to be more clear.

1 Like

@cornchip Yes. It was indeed from a LLM. Technical subject matter won’t change if its LLM or even taken from a text book direct. Guilty as charged I was interested in knowing myself and I was researching if there were any reasoning behind it by myself. I even tried editing a file in vi and seeing if any of these settings come to life when you yank a line and want to undo or redo. Inserting a line, deleting a word. Appending and then try to delete it. But no luck.

So I was inquisitive on seeing what a LLM had to say. I just posted the feedback which was no different than I already knew.

But honestly Yes it was LLM output. I will refrain from any posts from LLM apart from the Battery Optimization and this thread already posted.

1 Like

If you open up Settings for Terminal.app and go to Profiles, try changing (for example) Cursor from Block to Underline. Then look under the Edit menu again:

2 Likes

Thanks, Celsius! I should add to your answer that it only works until the Settings window is closed.

1 Like