0

It happens quite often to me, that while entering a complex command I realize that I need to enter some other commands first.

Being a vi user, I'd love to 0 D, enter a different command and later paste the deleted command line. Unfortunally, nobody (including myself) bothered to implement c&p-Registers in the vi mode of zsh.

So right now I Insert some x at the beginning of the command to make it fail, do my other stuff, fecth the old command from the history and remove the x. And each time I ask myself: Is there some easier way to do the same thing?

Not a duplicate! Please note that I mentioned I‘m using vi mode. None of the answers of the other question works for vi mode. They are only for emacs mode, even if they don‘t mention, so they are misleading. Please reopen so people can find the correct answer.

Philippos
  • 13,453
  • If you follow that link it is simply a mess. –  Oct 14 '19 at 21:37
  • "This inserts a # in front of the line, and adds it to the command line history." This one liner from accepted answer is quite lost in all this. And the idea works for bash and emacs mode, too. –  Oct 14 '19 at 22:14

1 Answers1

1

Enable the interactivecomments shell option with

setopt interactivecomments

and use the # action in normal/command mode on the command line (i.e. press Esc followed by #). This inserts a # in front of the line, immediately submits the line (which will be ignored since it's a comment), and adds it to the command line history. This works on a line by line basis, i.e. it does not work too well with multi-line commands unfortunately.

Using the # action on a line that is already commented out (e.g. fetched from the command line history) removes the # from the start of the line and submits it.

The interactivecomments shell option is by default unset in interactive shells, but set in non-interactive shells.

Kusalananda
  • 333,661
  • in bash it is the same key combination, different name. Of course "Home" and then a normal "#" is not much longer (but still uses a shortcut to a begin-of-line...). Shows what the command line of shells are "meant" for. NOT for editing multi-line commands. But for managing command lines. I think this is independent of zsh/bash and vi/emacs mode. –  Oct 14 '19 at 13:05
  • @rastafile Yes, it‘s independent, which makes it the perfect answer, because other possibilities are only for emacs mode or only for bash – Philippos Oct 15 '19 at 04:03
  • I also think this answer is good, better than the dupl. link. I just try to say it could be formulated more universal. From your original hack "xxx" (which was mine too!) to my HOME,#,RETURN to one-key zsh/bash bindings: it is the same idea: turn the dilemma into a saved comment. –  Oct 15 '19 at 04:47
  • dilemma being: you dont want to execute line. You want to clear line. But you dont want to loose line. So you want to save line. But not execute it. –  Oct 15 '19 at 04:50