Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comments in command-line Zsh

People also ask

How do you comment a block in shell script?

In Shell or Bash shell, we can comment on multiple lines using << and name of comment. we start a comment block with << and name anything to the block and wherever we want to stop the comment, we will simply type the name of the comment.

Can I use bash commands in zsh?

A ZSH shell script is a text file that contains instructions or commands to be executed by the ZSH shell. The ZSH shell is an extended version of the Bourne Again Shell; thus, most commands and scripts written for bash will work on ZSH.

What is zsh in command line?

Zsh is not a terminal emulator; it's a shell that runs inside a terminal emulator. So, to launch Zsh, you must first launch a terminal window such as GNOME Terminal, Konsole, Terminal, iTerm2, rxvt, or another terminal of your preference. Then you can launch Zsh by typing: $ zsh.


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.


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.


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.


: 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