如何在 YAML 中阻止注释?

发布于 2024-08-21 23:45:26 字数 23 浏览 5 评论 0原文

如何在 YAML 中注释一行行?

How do I comment a block of lines in YAML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

顾铮苏瑾 2024-08-28 23:45:26

YAML 支持内联注释,但不支持块注释。

来自维基百科

注释以数字符号 (#) 开头,可以从一行中的任意位置开始,一直持续到行尾

与 JSON 的比较,也来自 维基百科

语法差异很微妙,在实践中很少出现:JSON 允许扩展字符集,如 UTF-32,YAML 需要在逗号、等号和冒号等分隔符后有一个空格,而 JSON 则不然,并且 JSON 扩展的一些非标准实现包含 Javascript 的 /* ... */ 注释的语法。处理此类边缘情况可能需要在解析为内联 YAML 之前对 JSON 进行少量预处理。

# If you want to write
# a block-commented Haiku
# you'll need three pound signs

YAML supports inline comments, but does not support block comments.

From Wikipedia:

Comments begin with the number sign ( # ), can start anywhere on a line, and continue until the end of the line

A comparison with JSON, also from Wikipedia:

The syntax differences are subtle and seldom arise in practice: JSON allows extended charactersets like UTF-32, YAML requires a space after separators like comma, equals, and colon while JSON does not, and some non-standard implementations of JSON extend the grammar to include Javascript's /* ... */ comments. Handling such edge cases may require light pre-processing of the JSON before parsing as in-line YAML.

# If you want to write
# a block-commented Haiku
# you'll need three pound signs
×纯※雪 2024-08-28 23:45:26

规范仅描述了一种标记注释的方式:

明确的注释由“#”指示符标记。

就这样。没有任何块评论。

The specification only describes one way of marking comments:

An explicit comment is marked by a “#” indicator.

That's all. There aren't any block comments.

江挽川 2024-08-28 23:45:26

我并不是想聪明地处理它,但如果你使用 Sublime Text 作为你的编辑器,步骤如下:

  1. 选择块
  2. 在 Mac 上
  3. Cmd + / 或在 Linux 和 Windows 上选择 Ctrl + / Profit

I'我想其他编辑器也有类似的功能。您使用的是哪一款?我很乐意做一些挖掘。

I am not trying to be smart about it, but if you use Sublime Text for your editor, the steps are:

  1. Select the block
  2. Cmd + / on Mac or Ctrl + / on Linux and Windows
  3. Profit

I'd imagine that other editors have similar functionality too. Which one are you using? I'd be happy to do some digging.

孤君无依 2024-08-28 23:45:26

在 Vim 中,您可以执行以下操作之一:

  • 注释所有行: :%s/^/#
  • 注释第 10 - 15 行: :10,15s/^/#
  • 注释第 10 行到当前行::10,.s/^/#
  • 注释第 10 行到结束::10,$s/^/#

或使用可视块:

  1. 选择通过 Ctrl+v 进入可视块后的多行列。
  2. r 后按 # 注释掉替换所选内容的多行块,或按 Shift+i# Esc 在选择之前插入注释字符。

In Vim you can do one of the following:

  • Comment all lines: :%s/^/#
  • Comment lines 10 - 15: :10,15s/^/#
  • Comment line 10 to current line: :10,.s/^/#
  • Comment line 10 to end: :10,$s/^/#

or using visual block:

  1. Select a multiple-line column after entering visual block via Ctrl+v.
  2. Press r followed by # to comment out the multiple-line block replacing the selection, or Shift+i#Esc to insert comment characters before the selection.
怀中猫帐中妖 2024-08-28 23:45:26

另一种方法:

如果

  • 您的 YAML 结构具有明确定义的字段供您的应用程序使用
  • ,并且您可以自由添加不会干扰您的应用程序的其他字段

,那么

  • 您可以在任何级别添加新字段块文本字段命名为“描述”或“评论”或“注释”或任何

示例:

而不是

# This comment
# is too long

使用

Description: >
  This comment
  is too long

Comment: >
    This comment is also too long
    and newlines survive from parsing!

更多优点:

  1. 如果评论变得庞大而复杂并且具有重复模式,您可以将它们从纯文本块提升为对象
  2. 您的应用程序将来可能会读取或更新这些评论

An alternative approach:

If

  • your YAML structure has well defined fields to be used by your app
  • AND you may freely add additional fields that won't mess up with your app

then

  • at any level you may add a new block text field named like "Description" or "Comment" or "Notes" or whatever

Example:

Instead of

# This comment
# is too long

use

Description: >
  This comment
  is too long

or

Comment: >
    This comment is also too long
    and newlines survive from parsing!

More advantages:

  1. If the comments become large and complex and have a repeating pattern, you may promote them from plain text blocks to objects
  2. Your app may -in the future- read or update those comments
清引 2024-08-28 23:45:26

在 YAML 中阻止注释的一种方法是使用 Notepad++ 等文本编辑器一次将 #(注释)标签添加到多行。

在 Notepad++ 中,您可以使用所选文本的“块注释”右键单击选项来执行此操作。

Woo 图片!

One way to block commenting in YAML is by using a text editor like Notepad++ to add a # (comment) tag to multiple lines at once.

In Notepad++ you can do that using the "Block Comment" right-click option for selected text.

Woo Images!

残花月 2024-08-28 23:45:26

Emacs 有 comment-dwim (Do What I Mean) - 只需选择块并执行以下操作:

M-;

这是一个切换开关 - 用它来注释和取消注释块。

如果您没有安装 yaml-mode,您需要告诉 Emacs 使用井号字符 (#)。

Emacs has comment-dwim (Do What I Mean) - just select the block and do a:

M-;

It's a toggle - use it to comment AND uncomment blocks.

If you don't have yaml-mode installed you will need to tell Emacs to use the hash character (#).

无言温柔 2024-08-28 23:45:26

对于 Windows 上的 RubyMine 用户:

  • 在编辑器中打开文件。

  • 选择块并按:

    Ctrl + /

  • 您将获得以#开头的选定块。

现在,如果您想取消注释块,请再次按相同的组合键 Ctrl + 正斜杠

For RubyMine users on Windows:

  • Open the file in the editor.

  • Select the block and press:

    Ctrl + /,

  • And you will have the selected block starting with #.

Now if you want to uncomment the commented block, press the same key combination Ctrl + forward slash again.

熊抱啵儿 2024-08-28 23:45:26

Azure DevOps 浏览器(管道 YAML 编辑器)中,

Ctrl + K + C 注释块

Ctrl + K + U 取消注释块

还有一个“切换块评论”选项,但这对我不起作用。

输入图片此处描述

还有其他“奇怪”的方法:右键单击查看“命令面板”或 F1

在此处输入图像描述

然后选择光标选项。

输入图片此处描述

现在只需 # 即可。

甚至更智能 [Ctrl + K] + [Ctrl + C]

In the Azure DevOps browser (pipeline YAML editor),

Ctrl + K + C Comment Block

Ctrl + K + U Uncomment Block

There also a 'Toggle Block Comment' option, but this did not work for me.

Enter image description here

There are other 'weird' ways too: Right-click to see 'Command Palette' or F1

Enter image description here

Then choose a cursor option.

Enter image description here

Now it is just a matter of #.

Or even smarter [Ctrl + K] + [Ctrl + C]

方圜几里 2024-08-28 23:45:26

如果您使用EclipseYEdit 插件(.yaml 文件的编辑器),您可以通过以下方式注释掉多行:

  1. 选择要注释的行注释,然后
  2. Ctrl + Shift + C

要取消注释,请按照相同的步骤操作。

If you are using Eclipse with the YEdit plugin (an editor for .yaml files), you can comment-out multiple lines by:

  1. selecting lines to be commented, and then
  2. Ctrl + Shift + C

And to uncomment, follow the same steps.

醉态萌生 2024-08-28 23:45:26

.gitlab-ci.yml 文件中,以下内容有效:

要注释掉一个块(多行):选择整个块部分>
Ctrl K C

要取消注释已注释掉的块(多行):选择
整块断面> Ctrl K U

In a .gitlab-ci.yml file, the following works:

To comment out a block (multiline): Select the whole block section >
Ctrl K C

To uncomment already commented out block (multiline): Select the
whole block section > Ctrl K U

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文