sun*_*256 326
在bash中:
#!/bin/bash
echo before comment
: <<'END'
bla bla
blurfl
END
echo after comment
Run Code Online (Sandbox Code Playgroud)
在'和'周围的END分隔符是重要的,否则里面的东西例如像块$(command)将被解析并执行.
Osc*_*Ryz 86
shell脚本没有块注释.
使用vi(是vi),您可以轻松地从第n行到第m行发表评论
<ESC>
:10,100s/^/#/
Run Code Online (Sandbox Code Playgroud)
(读取,从第10行到第100行用#符号替换行开头(^).)
并且不发表评论
<ESC>
:10,100s/^#//
Run Code Online (Sandbox Code Playgroud)
(读取,从第10行到第100行替换行开头(^)后跟#注释//.)
vi几乎在任何地方都是通用的/bin/sh.
小智 48
您可以使用:
if [ 1 -eq 0 ]; then
echo "The code that you want commented out goes here."
echo "This echo statement will not be called."
fi
Run Code Online (Sandbox Code Playgroud)
dev*_*ull 25
以下应该适用于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"
Run Code Online (Sandbox Code Playgroud)
执行上面的代码会导致:
This is outside the commented block
Run Code Online (Sandbox Code Playgroud)
例如,为了取消注释如此评论的代码块
alias BEGINCOMMENT="if : ; then"
Run Code Online (Sandbox Code Playgroud)
代替
alias BEGINCOMMENT="if [ ]; then"
Run Code Online (Sandbox Code Playgroud)
在上面的例子中.
ste*_*anB 14
在Vim:
shift-V (进入可视模式),向上按下块中的高亮线:s/^/#/该命令将如下所示:
:'<,'>s/^/#
Run Code Online (Sandbox Code Playgroud)打进去
例如
shift-V
jjj
:s/^/#
<enter>
Run Code Online (Sandbox Code Playgroud)
使用: '在打开和'关闭。
例如:
: '
This is a
very neat comment
in bash
'
Run Code Online (Sandbox Code Playgroud)
这是来自拉斯维加斯的榜样发现这里
您可以使用 Vi/Vim 的 Visual Block 模式,该模式专为以下内容而设计:
Ctrl-V
Highlight first element in rows you want commented
Shift-i
#
esc
Run Code Online (Sandbox Code Playgroud)
取消注释将是:
Ctrl-V
Highlight #'s
d
l
Run Code Online (Sandbox Code Playgroud)
这是 vi 执行此类操作的交互方式,而不是计算或读取行号。
最后,在 Gvim 中,您可以使用 ctrl-q 进入 Visual Block 模式,而不是 ctrl-v (因为这是粘贴的快捷方式)。
老实说,为什么要进行如此多的过度设计......
我认为编写主动代码来生成被动代码确实是一种不好的做法。
我的解决方案:大多数编辑器都有块选择模式。只需使用它将 # 添加到要注释掉的所有行即可。有什么大不了的...
记事本示例:
要创建:Alt - 鼠标向下拖动,按 #。
要删除:Alt-鼠标向下拖动,Shift-右箭头,删除。