How to provide arguments to Readline functions via inputrc - bash

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.

Related

How to merge lines previously broken by black due to violating the line length constraint when removing a few arguments?

Assume I have a single-line function call that exceeds black's line length constraint. Black will reformat this function call to a multi-line function call to satisfy the constraint, e.g.,
replace_config(user_args, tune_args, container_type, class_name, param_names)
replace_config(
user_args,
tune_args,
container_type,
class_name,
param_names,
)
Further assume I would like to remove the last three arguments, which are optional, from this function call. My new code would look like this:
replace_config(
user_args,
tune_args,
)
How can I tell black to shorten this multi-line function call to a single-line function call because it no longer violates the line length constraint?
The output should look like this:
replace_config(user_args, tune_args)
You're running into the magic trailing comma feature. Black will never collapse any collections or function argument/parameter lists if it has a trailing comma.
You can either disable the magic trailing comma feature by passing -C or --skip-magic-trailing-comma, or simply just remove the trailing comma (and the next time Black formats the file, it'll happily collapse the function call as you'd expect).
FWIW there's an open issue on the issue tracker about this.

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 pass infomation to POV-Ray from a shell script

How do I pass a variable from a shell script to POV-Ray? My desire would the ability to pass a variable as a command-line argument and then use that value in the .ini file or .pov file
Something like
POV-Ray +pass var=$imageNumber file.pov
And then being able to use var in POV-Ray
I realize that I could edit the .ini and .pov files in the script or use modulus to use the single frame variable as two variables, but both those solutions seem awkward.
I want to generate 1000s of extremely similar scenes. Each scene is exactly the same except that a heightmap uses a different image file as its source. Normally, I would use the animation tools in POV-Ray to generate multiple frames. However, I am already using the animation tools to cycle over a different property in each scene.
For *nix systems, use POV-Ray's file handling system to open the standard-in file in your .pov file
#fopen STDIN "/dev/stdin" read
#read (STDIN, var1, var2)
This will read from the standard-in for a comma separated list of POV literals. However, POV-Ray doesn't handle reading from a pipe; Thus, use herestrings (or heredocuments if you must use only sh compatible syntax) to fill stdin for POV-Ray.
For example, if run in the shell (works for bash):
povray "example.pov" <<<'"hello","world"'
Will fill the variables var1 and var2 from above with the values "hello" and "world" respectively. Note that quotes must be included around each string value in the list. This is because POV wants POV literals in the 'file' we are passing.
If you want to use an .ini file instead, just call the .ini file in place of the .pov file and everything will work as expected.
If you want more or less variable to be passed to the POV file, add or remove variable names from the #read directive and extend or trim the number you are passing to the same length.'
You can also pass shell variables like this. If foo contains "hello" including the quotes, and "example.pov" is expecting one string in the herestring, then
povray "example.pov" <<<$foo
will pass hello to the variable in the #read directive.
Additionally, you can other POV literals than stings, in that case use the relevant POV syntax the that literal type. However, you can't put POV expressions into the herestring. See the wiki page for more information.
As of POV-Ray 3.7 you can now declare constants in the INI file, and therefore the command line, with Declare=MyValue=24. This would be the same as a #declare MyValue=24; in a scene file. The value on the right-hand side must be a constant float value.
see the relevant manual entry
As long as you don't pass fractional values (or use extremely large sequence numbers), you should be able to use this as a component in the file name.

bash readline bind with dynamic prefix argument?

Is there a way to make a bash keybinding that moves the cursor N characters forward, where N is variable? I was hoping I could do
bind '"\e\C-i": "\e$(echo "$N")\C-f"'
but of course the $() isn't expanded beforehand.
Note that I don't want to have to specify the prefix manually – I want a function to give it (or if it's read from an env-var or a file as a workaround).

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.

Resources