Comments in command-line Zsh
Command LineZshCommand Line Problem Overview
I switched quite recently from Bash to Zsh on Ubuntu and I'm quite happy about it. However, there is something I really miss and I did not find how to achieve the same thing.
In Bash, whenever I was typing a long command and noticed I had to run something else before, I just had to comment it out like in the following:
me@home> #mysuperlongcommand with some arguments
me@home> thecommandIhavetorunfirst #and then: then up up
me@home> #mysuperlongcommand with some arguments #I just need to uncomment it!
However, this quite recurrent situation is not as easy to address as with zsh
, given #mysuperlongcommand
will be run as such (and resulting in: zsh: command not found: #mysuperlongcommand
.
Command Line Solutions
Solution 1 - Command Line
Having just started trying out zsh, I ran into this problem too. You can do setopt interactivecomments
to activate the bash-style comments.
The Z Shell Manual indicates that while this is default behavior for ksh
(Korn shell) and sh
(Bourne shell), and I am guessing also for bash
(Bourne-again shell), it is not default for zsh
(Z shell):
> In the following list, options set by default in all emulations are marked <D>
; those set by default only in csh, ksh, sh, or zsh emulations are marked <C>
, <K>
, <S>
, <Z>
as appropriate.
> INTERACTIVE_COMMENTS (-k) <K>
<S>
> Allow comments even in interactive shells.
Solution 2 - Command Line
I use
bindkey "^Q" push-input
From the zsh manual:
> Push the entire current multiline construct onto the buffer stack and return to the top-level (PS1) prompt. If the current parser construct is only a single line, this is exactly like push-line. Next time the editor starts up or is popped with get-line, the construct will be popped off the top of the buffer stack and loaded into the editing buffer.
So it looks like this:
> long command
Ctrl+Q => long command disappears to the stack
> forgotten command
long command reappears from stack
> long command
Also, if you set the INTERACTIVE_COMMENTS
option (setopt INTERACTIVE_COMMENTS
), you will be able to use comments in interactive shells like you are used to.
Solution 3 - Command Line
I find myself doing this often as well. What I do is cut the long command, execute the command that needs to go first and then paste the long command back in. This is easy: CTRL+U cuts the current command into a buffer, CTRL+Y pastes it. Works in zsh and bash.
Solution 4 - Command Line
: sh generate_sample.sh arg1
The addition of ":" doesn't execute the command in zsh.
sh generate_sample.sh : arg1
Now the arg1 is commented.
I am on Mac OS Big Sur and used it multiple times.
Edit: ":" procedure works with giving no spaces. ": command" is correct but ":command" isn't
Solution 5 - Command Line
Add the line setopt INTERACTIVE_COMMENTS
to your ~/.zshrc
file, save it, and relaunch the shell.
This is to clarify on @Lajnold's answer and @Hamish Downer's comment. It just took me a little bit to figure out how to make this change permanent. You probably want to add that line before exporting variables, so maybe add it toward the top of the file. I already had setopt PROMPT_SUBST
in my ~/.zshrc
file, so I just added it after that line. This will ensure the settings are loaded every time you launch the zsh terminal.
Solution 6 - Command Line
In addition to setopt interactivecomments
, suggested by @Lajnold, you might also want to add something like the following to prevent certain comments from being written to history (from https://superuser.com/questions/352788/how-to-prevent-a-command-in-the-zshell-from-being-saved-into-history):
This overrides the ZSH built-in function zshaddhistory():
- Will log comments that start in column 1 not followed by one or more spaces (i.e. #somecommand that I want to come back to)
- Won't log comments that start in column 1 followed by one or more spaces
- Won't log indented comments, padded by spaces from column 1
- Won't log commands with a space in column 1 (handy shortcut for running commands that you don't want logged
setopt interactivecomments
function zshaddhistory() {
emulate -L zsh
if ! [[ "$1" =~ "(^#\s+|^\s+#|^ )" ]] ; then
print -sr -- "${1%%$'\n'}"
fc -p
else
return 1
fi
}
For reference, this is the default zshaddhistory() http://zsh.sourceforge.net/Doc/Release/Functions.html
zshaddhistory() {
print -sr -- ${1%%$'\n'}
fc -p .zsh_local_history
}