Commenting multiple lines in an ipython cell - comments

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.

Related

How to provide arguments to Readline functions via inputrc

I'm trying to use the insert-comment function provided by readline to uncomment a line.
According to the documentation:
If a numeric argument is supplied, this command acts as a toggle: if the characters at the beginning of the line do not match the value of comment-begin, the value is inserted, otherwise the characters in comment-begin are deleted from the beginning of the line
I have following line in my .inputrc for it:
"\eW": insert-comment 1
Which maps insert-comment to Alt-W (just for testing. I intend to remap Alt-# when it works)
On reloading .inputrc, entering some text on the terminal (like #123) and then pressing Alt-W, # gets prepended to whatever I type (like ##123), the same behaviour as Alt-#.
How do I use insert-comment function as a toggle mapped to a custom key sequence?
As far as I know, Readline doesn't support adding an argument in a binding like this. You can define a macro, though. For example, suppose you left insert-comment bound to Alt-W. Then you could define Alt-# as
"\e#": "\e1\eW"
The \e1 sets the argument to 1, followed by \eW to invoke insert-comment with the current argument.

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_

Adding colors in python interpreter prompt, does not wrap properly

I would like to have a more colorful Python prompt in the terminal, just for readability. I currently have:
sys.ps1 = '\033[96m>>> \033[0m'
sys.ps2 = '\033[96m... \033[0m'
in my PYTHONSTARTUP file, which does give it colors as desired. However, any text over a line does not wrap properly. The text goes to the end of the line, and instead of immediately starting a new line, starts overwriting the beginning of the first line before starting a new line. As you might imagine, this is actually rather unreadable. How can I fix this behavior?
Try the following:
sys.ps1 = '\001\033[96m\002>>> \001\033[0m\002'
sys.ps2 = '\001\033[96m\002... \001\033[0m\002'
This answer to a similar question explains why the \001 and \002 are necessary.
Is there some reason to not use IPython? IPython does provide color prompts, etc. out of the box...

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

How do you do block comments in YAML?

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

Resources