3

I am writing a *nix shell, I want support the key-combination of Ctrl-Enter. And the library I use is GNU readline.

I have spent several hours on it, but can't find the way.

I can bind Enter successfully, like:

========= ~/.inputrc ===============

"\C-M": "sdf"

====================================

When I press Enter, I got "sdf" echoed, just as expected.

I can also bind Esc,Enter successfully(note, here is key serial, not combination), like:

========= ~/.inputrc ===============

"\e\C-M": "sdf"

====================================

When I pressed Esc and then pressed Enter, "sdf" was echoed as expected.

However, the following line can not produce a valid Ctrl-Enter combination:

========= ~/.inputrc ===============

"\C\C-M": "sdf"

====================================

And, "\C-\C-M" doesn't work too.

Here are two related questions:

how to bind the 'Enter key'

How to bind Ctrl-Enter in fish?

Can any one help me ?

Community
  • 1
  • 1
weiweishuo
  • 847
  • 7
  • 15
  • The answer in the second linked question should tell you that it's not really possible. – Some programmer dude Aug 12 '16 at 12:13
  • The difference between Esc and Control is that `Esc` can be pressed and released before the next key. `\C-M` works because most terminals generate ASCII 13 when you hit enter or Control-m. You need to hold the control key down along with another key for it to mean anything; what would it mean to hold it down twice? – chepner Aug 12 '16 at 12:49
  • "what would it mean to hold it down twice? " – weiweishuo Aug 12 '16 at 15:07
  • "what would it mean to hold it down twice? " I am sorry, where did i indicate that , forgive my poor English . I know your meaning more or less. But it's strange that zsh support Alt+Enter, and some *nix shell support Shift+Enter( I saw it when google). @chepner – weiweishuo Aug 12 '16 at 15:17
  • The *shell* doesn't know anything about the keyboard; it just gets a series of bytes on its input file. Your terminal emulator produces that series of bytes, and one of the things it does is write ASCII 13 when you press the Enter key. As such, `Control-Enter` just isn't something you can represent in a single byte. – chepner Aug 12 '16 at 15:29
  • I see, just use "\C-J" : "sdf" See http://stackoverflow.com/questions/598113/can-terminals-detect-shift-enter-or-control-enter – weiweishuo Aug 13 '16 at 23:02
  • I see, just use "\C-J" : "sdf" See http://stackoverflow.com/questions/598113/can-terminals-detect-shift-enter-or-control-enter – weiweishuo Aug 13 '16 at 23:03

1 Answers1

1

You don't mention which terminal emulator you use - which matters, because in the end, it is the terminal that decides what your program reads from it when you press CTRL+ENTER

A way to make this work is (assuming you use xterm):

  • Make xterm send e.g. CTRL+k when you press CTRL+ENTER, by adding a line in your .Xdefaults:

    XTerm.vt100.translations: #override Ctrl<Key>Return: string("\013")

  • Bind this to whatever you want in .inputrc:

    "\C-k": "sdf"

Not very elegant, as this will only work in xterm. But, as far as i can see, the only way to get what you want.

Hans Lub
  • 5,513
  • 1
  • 23
  • 43
  • That has the considerable disadvantage of not letting you use Ctrl-K, which is commonly used by some applications. Perhaps you could map Ctrl-Enter to some other sequence, not just one character. But you probably can't expect other users to set up the same mapping (even assuming they use xterm). It might be reasonable the OP isn't expecting anyone else to use the application. – Keith Thompson Aug 23 '16 at 23:18