是否有一种简单的方法来注释掉shell脚本中的代码块?


当前回答

在一个:

去你想评论的第一行 shift-V(进入可视模式),向上向下突出块中的行 执行以下选择:s/^/#/ 命令如下所示: : <、>年代/ ^ / # 回车

e.g.

shift-V
jjj
:s/^/#
<enter>

其他回答

使用:'打开'和'关闭。

例如:

: '
This is a
very neat comment
in bash
'

这是维加斯的例子

在一个:

去你想评论的第一行 shift-V(进入可视模式),向上向下突出块中的行 执行以下选择:s/^/#/ 命令如下所示: : <、>年代/ ^ / # 回车

e.g.

shift-V
jjj
:s/^/#
<enter>

我喜欢单线开闭:

if [ ]; then ##
    ...
    ...
fi; ##

“##”可以帮助我轻松地找到块注释的开始和结束。我可以在“##”后面加上一个数字,如果我有很多的话。要关闭注释,我只需在“[]”中插入一个“1”。我还避免了在注释块中使用单引号时遇到的一些问题。

以下命令适用于sh、bash、ksh和zsh。

注释的代码块可以放在BEGINCOMMENT和ENDCOMMENT中:

[ -z $BASH ] || shopt -s expand_aliases
alias BEGINCOMMENT="if [ ]; then"
alias ENDCOMMENT="fi"

BEGINCOMMENT
  echo "This line appears in a commented block"
  echo "And this one too!"
ENDCOMMENT

echo "This is outside the commented block"

执行上述代码将导致:

This is outside the commented block

例如,为了取消注释所注释的代码块

alias BEGINCOMMENT="if : ; then"

而不是

alias BEGINCOMMENT="if [ ]; then"

在上面的例子中。

让我们把这些最好的想法和建议结合起来。

alias _CommentBegin_=": <<'_CommentEnd_'"

如前所述,单引号是非常重要的,因为没有它们 $(commandName)和${varName}将被计算。

你可以这样使用它:

_CommentBegin_
echo "bash code"
or 
none code can be in here
_CommentEnd_

别名使使用更加明显和更好看。