xuhdev
xuhdev

Reputation: 9360

In zsh, how to save interrupted (^C) commands to history automatically?

When a command is executed, the command is saved to history. Is there an option in zsh which automatically save the commands interrupted by Ctrl-C to history?

Upvotes: 1

Views: 369

Answers (1)

Adaephon
Adaephon

Reputation: 18399

If you mainly want to put a command back and run another first, I would suggest using the push-line widget, which is made just for that. It puts the current command on hold and clears the command line. After running another command, the original command is automatically restored. If you want to run multiple commands, you can repeat the process. As it works on a stack, you can even put multiple commands on hold. They will be restored in reverse order.

From the zshzle(1) manpage:

push-line (^Q ESC-Q ESC-q) (unbound) (unbound)

Push the current buffer onto the buffer stack and clear the buffer. Next time the editor starts up, the buffer will be popped off the top of the buffer stack and loaded into the editing buffer.

As you can see, by default it is bound in emacs mode (bindkey -e) to ^[q, ^[Q and ^Q. That is Alt+q (or Esc, q), Alt+Shift+q (or Esc, Shift+q) and Ctrl+q. The last of which only works if the option FLOW_CONTROL is disabled (setopt noflowcontrol).


There is also the push-line-or-edit widget. It behaves the same on a primary (PS1) prompt. But on a secondary (PS2) prompt it makes the whole multi-line construct (not just the current line) editable. Just like when you pull a multi-line construct from history.

Upvotes: 2

Related Questions