How do you do block comments in YAML? - comments

This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
How do I comment a block of lines in YAML?

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

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.

I am not trying to be smart about it, but if you use Sublime Text for your editor, the steps are:
Select the block
Cmd + / on Mac or Ctrl + / on Linux and Windows
Profit
I'd imagine that other editors have similar functionality too. Which one are you using? I'd be happy to do some digging.

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:
Select a multiple-line column after entering visual block via Ctrl+v.
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.

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:
If the comments become large and complex and have a repeating pattern, you may promote them from plain text blocks to objects
Your app may -in the future- read or update those comments

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.

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 (#).

If you are using Eclipse with the YEdit plugin (an editor for .yaml files), you can comment-out multiple lines by:
selecting lines to be commented, and then
Ctrl + Shift + C
And to uncomment, follow the same steps.

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.

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.
There are other 'weird' ways too: Right-click to see 'Command Palette' or F1
Then choose a cursor option.
Now it is just a matter of #.
Or even smarter [Ctrl + K] + [Ctrl + C]

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

Related

How can I refactor an existing source code file to normalize all use of tab?

Sometimes I find myself editing a C source file which sees both use of tab as four spaces, and regular tab.
Is there any tool that attempts to parse the file and "normalize" this, i.e. convert all occurrences of four spaces to regular tab, or all occurrences of tab to four spaces, to keep it consistent?
I assume something like this can be done even with just a simple vim one-liner?
There's :retab and :retab! which can help, but there are caveats.
It's easier if you're using spaces for indentation, then just set 'expandtab' and execute :retab, then all your tabs will be converted to spaces at the appropriate tab stops (which default to 8.) That's easy and there are no traps in this method!
If you want to use 4 space indentation, then keep 'expandtab' enabled and set 'softtabstop' to 4. (Avoid modifying the 'tabstop' option, it should always stay at 8.)
If you want to do the inverse and convert to tabs instead, you could set 'noexpandtab' and then use :retab! (which will also look at sequences of spaces and try to convert them back to tabs.) The main problem with this approach is that it won't just consider indentation for conversion, but also sequences of spaces in the middle of lines, which can cause the operation to affect strings inside your code, which would be highly undesirable.
Perhaps a better approach for replacing spaces with tabs for indentation is to use the following substitute command:
:%s#^\s\+#\=repeat("\t", indent('.') / &tabstop).repeat(" ", indent('.') % &tabstop)#
Yeah it's a mouthful... It's matching whitespace at the beginning of the lines, then using the indent() function to find the total indentation (that function calculates indentation taking tab stops in consideration), then dividing that by the 'tabstop' to decide how many tabs and how many spaces a specific line needs.
If this command works for you, you might want to consider adding a mapping or :command for it, to keep it handy. For example:
command! -range=% Retab <line1>,<line2>s#^\s\+#\=repeat("\t", indent('.') / &tabstop).repeat(" ", indent('.') % &tabstop)
This also allows you to "Retab" a range of the file, including one you select with a visual selection.
Finally, one last alternative to :retab is that to ask Vim to "reformat" your code completely, using the = command, which will use the current 'indentexpr' or other indentation configurations such as 'cindent' to completely reindent the block. That typically respects your 'noexpandtab' and 'smarttabstop' options, so it use tabs and spaces for indentation consistently. The downside of this approach is that it will completely reformat your code, including changing indentation in places. The upside is that it typically has a semantic understanding of the language and will be able to take that in consideration when reindenting the code block.

VIM for Windows: How to edit to the end of line but before EOL character

What I am trying to do:
For example, I have a line in my code that looks like this
something.something()
I would like to add print() around it:
print(something.something())
How I am doing it:
I type in vim: ^c$print()<Esc>P meaning:
put cursor to the beginning of the line,
change entire line,
type in print(),
paste entire line back before print's ).
The Problem:
Unfortunately the c$ part cut the EOL character as well. so the subsequent P operation will just paste the line on top of print(). So the end result will look like this:
something.something()
print()
My Thoughts:
Right now the work around is using v mode to highlight entire line except for the EOL character first, then do the above.
I am looking for something akin to ct$ ci$, but none of them works. my line doesn't always end with (), it could be __dict__ or just plain text, so cf) is handy but I am looking for something more universal. Thanks.
Of course it's doable out of the box.
Assuming your description of what you are doing is exact, the reason what you are doing doesn't work is most likely caused by something in your config because c$ (or its better alternative C) should never yank the EOL.
Here is a demonstration using your method as described in your question:
^c$print()<Esc>P
and the method I would use:
^Cprint(<C-r>")<Esc>
I don't think you want to be going into edit mode at all. Just do:
:s/something.something()/print(&)/g
Note that you can do this pretty easily interactively (eg, you don't have to type 'something.something()') by yanking something.something into the unnamed register (eg, put your cursor on the text and hit 'yiw', but what gets yanked exactly will depend on the current setting of iskeyword), and typing :s/<ctrl>r"/...
Or, as Christian Gibbons points out in the comments, if you want to replace the entire line you can simply do:
:s/.*/print(&)
Try ^cg_print()<Esc>P.
The g_ movement means "to the last non-blank character of the line", and since in Windows it appears the carriage return is part of the line if you yank/delete, using _g instead of $ on Windows may be advisable.
If you find yourself almost never needing $, you can swap the two commands in your .vimrc:
onoremap g_ $
onoremap $ g_

How to align texts in Sublime Text 3?

I use Sublime Text 3 Plug-in called AlignTab to align my code like this:
Version : 1.4.1
Author : Ken Wheeler
Website : http://kenwheeler.github.io
Docs : http://kenwheeler.github.io/slick
Repo : http://github.com/kenwheeler/slick
Issues : http://github.com/kenwheeler/slick/issues
Now, I want to my texts to stay next to the colon like this:
Version: 1.4.1
Author: Ken Wheeler
Website: http://kenwheeler.github.io
Docs: http://kenwheeler.github.io/slick
Repo: http://github.com/kenwheeler/slick
Issues: http://github.com/kenwheeler/slick/issues
How do I accomplish that ?
You can do this with AlignTab, no need for a different plugin. You just need to use the more advanced regex functionality as described on the GitHub page.
Bring up the Command Palette (Ctrl+Shift+P on Windows or cmd+shift+P on Mac), type in "AlignTab", press enter, and type this in and hit enter:
:/r0clf1
Props to #Hank for including 0 spaces option, didn't even realize he didn't want the space.
Explanation:
the : finds the colon
the / says okay now here come some arguments about what I want you to do with the colon
the r means right-justify the first column
the 0 means 0 spaces after the right column
the c means center the middle column (which is just the colon, so it doesn't do anything)
the l means left-justify the right column
the f1 means only do this for the first match on the line
The AlignTab docs or the linked examples have enough info to cover what you need.
If you highlight the original text, then Cmd + Shift + P (or Ctrl Shift P on windows) and enter AlignTab Live Preview mode, you can enter a regex and use the special rlc syntax.
The syntax is:
\s+:/r0c1l
The regex are \s+ (greater than 1 spaces) to the first :, then r0 (align right, 0 spaces), c1 (center mark with 1 padding to the right), then l (left align the remaining text). There's no need for an 'f1' at the end of the expression since the regex matches the leading spaces before your first colon (per your example).
If you had newly written text without the pre-formatting that you've already done, like:
Version: 1.4.1
Author: Ken Wheeler
Website: http://kenwheeler.github.io
Docs: http://kenwheeler.github.io/slick
Repo: http://github.com/kenwheeler/slick
Issues: http://github.com/kenwheeler/slick/issues
Then to convert to your desired format below, will be:
:/r0c1lf1
Converted below:
Version: 1.4.1
Author: Ken Wheeler
Website: http://kenwheeler.github.io
Docs: http://kenwheeler.github.io/slick
Repo: http://github.com/kenwheeler/slick
Issues: http://github.com/kenwheeler/slick/issues
This result is actually better than your original question text, because there's no leading space.
P.S. I found that the Tablular examples from the AlignTab docs to be slighty more helpful, although it doesn't represent the real implementation of AlignTab.

Commenting multiple lines in an ipython cell

Is there a way to comment multiple lines of code in an ipython cell in one go?
You can use triple-quoted strings. Although this prints a repeats out statement, which I would like to avoid.
'''
This is a multiline
comment.
'''
To comment multiple lines you can use:
ctrl + /
This won't print a repeat out statement the way the triple quotes does.
""" This is a multi line
comment.
"""
Out[1]:' This is a multi line \n comment.\n'
A better way is to use Alt-# shortcut.
This is a readline binding, so it will work on any shell which has GNU readline available, like Bash.
To use it simply type the code in an ipython block like:
In [1]: if True:
...: pass
Then to comment the complete block, press Alt-# anywhere within the cell.
The code will then change to:
In [1]: #if True:
...: # pass
...: #
To uncomment, we need to pass a numerical argument to this readline function.
It can be done by Alt-some_number anywhere within the cell.
Note that most of the terminals have keybindings for changing tabs mapped to Alt- 1 through 9. So if you have a tab opened, try to use a number that won't be allotted to a tab. Like, if you have 5 tabs opened, use Alt-6 (Any number works. It just needs to be passed to readline instead of being intercepted by the terminal)
For me Ctrl + ^/~. The other solutions didn't work for me.
I'm using windows 10 and Jupyter Notebook.

Is there a way to delete all comments in a file using Notepad++?

Notepad++ obviously recognizes all comments as such. Is there a way to simply delete all?
Edit: Stat-R's bookmark method has helped greatly, not only for removing comments but for conditionally removing lines in general.
For a general file, first of all you need to know the comment operator of the language you are writing the file in. For example, in java script the comment operator is //.
For the following code...
In NP++, you need to
Mark the lines that contains '//'. Make sure the bookmark option is enabled.
Then, choose from NP++ menu Search>Bookmark>Remove Bookmarked lines
EDIT:
Another solution after #Chris Mirno 's suggestion is as follows:
Use regular expression. See the image below. It is self explanatory
To understand it better, refer to these
In the Find & Replace Dialog, put the following regex and adjust the search options as depicted.
/\*.*?\*/
Replace with: (empty)
Select Mode: Regular Expression AND .(dot) matches newline
This should remove all your C style comments spanned across lines.
Star-R and Chris Mirno Answer are also Correct and Good.
But For Line Comment:
//.*?(?=\r?$)
Explanation:
// will be the Starting Position
.*? Will be any character
(?=\r?$) will search to the end of the line (as it is required in line comment)
Note:
But Still check each of the line because for example if your code contains soap format like
//www.w3.org/2001/XMLSchema-instance\x2......");
it will capture this line because the starting is // and it goes to end of the line so watch out for this :)
Warning to all using Stat-R's solution:
This method will remove lines of code if formatted like this:
echo "hello"; //This comment will be detected
Following his method, the entire line will be removed.
Therefore make sure to go through and make these comments, their own line before doing this method.
I have had some luck running a macro for the above. Basically:
search for // (F3)
select to end of line (shift+end)
delete (delete)
Put // into the search dialog by just searching for it once. Then record the three steps in a macro, then play it back until EOF.
The first time I did it I had a problem, but then it worked, not sure what I did differently.
Anton Largiader's answer was the most reliable one, including complex inline comments.
However, it will leave many empty lines, including ones with empty characters (space, tabs...) so I would just add another step to make it almost perfect:
After running the macro, just do:
Edit > Line Operations > Remove Empty Lines
OR
Edit > Line Operations > Remove Empty Lines (Containing Blank Characters)
1st option is good if you wish to remove only really empty lines
2nd options will remove every empty line even containing space etc. so there will be no more actual spacing left between code blocks. 1st option might be the safest with some manual cleanup afterwards.
As someone suggested in another post, the simplest and most reliable is maybe to export the all text in .RTF format using Menu Plugin-->NppExport-->Export to RTF and then:
-Open the newly created file in Word
-Select any part of any comment
-On the top-right side of Word clic Select--> Select all texts with similar formatting
-Remove the selected comments all at once (del or cut if doesn't work)
To remove Powershell comments if someone find it handy:
Removing Comment in a Powershell using Notepad ++
To find just lines beginning with # (and not with # elsewhere in the line).
Notepad++ SEARCH Menu > Find
‘Mark‘ Tab – fill in as below.
Select ‘Mark All’ (clear all marks if used previously).
Regex ^[#}
enter image description here
SEARCH Menu > bookmark > Remove (or do anything on the list with
them)
Clear all marks to reset
You can select no comments just code by doing the following:
Regex ^[^#}
enter image description here
Enter ctrl+shift+K to remove comment

Resources