Paolo
Paolo

Reputation: 1637

How to bind the 'Enter key' in Bash readline?

So I learned how to use bind yesterday.

By typing Ctrl+v followed by a key in the terminal, I get a raw character that represents the key. For example: Ctrl+v followed by Esc returns ^[.

My question is, how can I bind the "enter key". The Enter Key returns ^M but when I type the command

bind '"\e^M":"foobar"'

pressing the enter key does not result in foobar being typed in my terminal.

Upvotes: 5

Views: 3144

Answers (2)

chepner
chepner

Reputation: 531085

bind '"\e^M":"foobar"'

binds Escape-Enter, not Enter. You just want

bind '"^M":"foobar"'

^M must be the actual control character, not ^ and M. A little easier to type is

bind '"\C-M":"foobar"'

Upvotes: 6

Sergey Fedorov
Sergey Fedorov

Reputation: 2169

$ alias ^M='echo foobar'
$ ^M
foobar

Upvotes: 0

Related Questions