sm7
sm7

Reputation: 61

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?

Upvotes: 2

Views: 281

Answers (1)

chepner
chepner

Reputation: 531095

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.

Upvotes: 4

Related Questions