Bind a key to newline in Bash, without submitting command

7

1

I want to press M-j and have readline place my cursor on the next line without entering a command. Thus I should be able to go back a character onto the prev. line. This answer states that it cannot be done. I think it's wrong.

You can kill the newline character, and yank it. This achieves what I want to do. However I just can't create the binding.

To test this out type: echo <<EOFRETURNENTERC-cC-pM-1C-dC-c

Now you should be on a newline, type C-y. It does what I want! But what character is this? What binding works in an inputrc file?

EDIT: It turns out the character is a line-feed. Still not sure how to bind it.

cdosborn

Posted 2015-03-29T02:52:11.023

Reputation: 532

1You can type a newline as C-v C-j, which is mostly useful for breaking lines up while line editing. The linked question is referring to up and down cursor motions in a multi-line line edit. I'm not sure that either of those are what you want to do. – rici – 2015-03-30T04:11:36.903

I added some to clarify, I want to be on the next line and able to go back. – cdosborn – 2015-03-30T04:21:15.317

1On my system, @rici's shortcuts seem to achieve what you're asking for. What happens on yours? – Dennis – 2015-03-30T05:28:27.917

That is the correct behavior, The following binding works "\ej": "\C-v\C-j" based on how my terminal interprets meta. Please post an answer for me to accept @rici – cdosborn – 2015-03-30T06:24:48.400

Answers

11

You can type a newline character without having the line accepted by using readline's "quoted-insert", usually bound to C-v, followed by the newline character C-j.

"quoted-insert" is also useful for typing a tab character without invoking tab completion (C-vC-i).

rici

Posted 2015-03-29T02:52:11.023

Reputation: 3 493

Wow this is really useful, anyway to make this automatic when I use \\ to escape the newline when creating a multiline command? – CMCDragonkai – 2016-04-06T11:09:06.060

@cmcdragonkai: not that I know of but you could bind some key sequence to \\\C-j. – rici – 2016-04-06T14:46:58.050