5

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.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Paolo
  • 1,557
  • 3
  • 18
  • 28

2 Answers2

6
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"'
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Hey thanks Chepner, I was wondering why ^M did not work. Also, I was wondering where can I found out more about \C and \e and any other escape sequence characters. – Paolo Nov 22 '13 at 18:47
  • See Readline Notation in the `bash` man page. Looks like "C-M" is sufficient; I'm not sure if "\C-M" is an acceptable alternative, or if the "\C" is just treated by `bash` as an escaped "C" which is treated as a "C". – chepner Nov 22 '13 at 19:33
0
$ alias ^M='echo foobar'
$ ^M
foobar
Sergey Fedorov
  • 2,169
  • 1
  • 15
  • 26