命令行Zsh中的注释

时间:2012-07-26 13:58:55

标签: command-line zsh

我最近在Ubuntu上从Bash切换到了Zsh,我很高兴。但是,有一些我真正想念的东西,我没有找到如何实现同样的目标。

在Bash中,每当我输入一个长命令并注意到我之前必须运行其他东西时,我只需要像下面那样对它进行评论:

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!

然而,这种非常复杂的情况并不像使用zsh那样容易解决,因为#mysuperlongcommand将会这样运行(并导致:zsh: command not found: #mysuperlongcommand

5 个答案:

答案 0 :(得分:144)

刚刚开始尝试zsh,我也遇到了这个问题。您可以setopt interactivecomments激活bash风格的评论。

答案 1 :(得分:36)

我用

bindkey "^Q" push-input

从zsh手册:

  

将整个当前多行结构推送到缓冲区堆栈并返回顶级(PS1)提示符。如果当前的解析器构造只是一行,这就像推线。下次编辑器启动或弹出get-line时,该构造将弹出缓冲区堆栈的顶部并加载到编辑缓冲区中。

所以它看起来像这样:

> long command
Ctrl+Q => long command disappears to the stack
> forgotten command
long command reappears from stack
> long command

此外,如果您设置INTERACTIVE_COMMENTS选项(setopt INTERACTIVE_COMMENTS),您将能够像过去一样在交互式shell中使用注释。

答案 2 :(得分:18)

我发现自己经常这样做。我做的是剪切long命令,执行需要先执行的命令然后再粘贴long命令。这很简单: CTRL + U 将当前命令切换到缓冲区, CTRL + Y 粘贴它。适用于zsh和bash。

答案 3 :(得分:0)

除了@Lajnold建议的setopt interactivecomments外,您可能还想添加类似以下内容的内容,以防止某些注释写入历史记录(来自https://superuser.com/questions/352788/how-to-prevent-a-command-in-the-zshell-from-being-saved-into-history):

这将覆盖ZSH内置函数zshaddhistory():

  • 将记录从第1列开始的注释,而不是一个或多个空格(即,我想返回的#somecommand)
  • 不会记录以第1列开头且后面有一个或多个空格的注释
  • 不会记录缩进的注释,该注释由第1列中的空格填充
  • 不会在第1列中记录带有空格的命令(用于运行您不想记录的命令的便捷快捷方式
setopt interactivecomments

function zshaddhistory() {
  emulate -L zsh
  if ! [[ "$1" =~ "(^#\s+|^\s+#|^ )" ]] ; then
      print -sr -- "${1%%$'\n'}"
      fc -p
  else
      return 1
  fi
}

作为参考,这是默认的zshaddhistory()http://zsh.sourceforge.net/Doc/Release/Functions.html

zshaddhistory() {
  print -sr -- ${1%%$'\n'}
  fc -p .zsh_local_history
}

答案 4 :(得分:0)

: sh generate_sample.sh arg1

加":"不会执行zsh中的命令。

sh generate_sample.sh : arg1

现在注释了 arg1。

我在 Mac OS Big Sur 上使用过多次。