Bash – How to Split an Existing Prompt Command Line into Multiple Lines

bashcommand line

I understand I can type \ enter at the end of a bash command line to continue that command in another line. But how can I split a prompt command line — that has already been fully typed — into two?

For example, how can I break this line right before then without having to cut the remainder off and type it all over again?

$ if true; then ls; fi

Best Answer

A newline character is LF (line feed), a.k.a. Control-J. If you press Ctrl+J, this executes the command accept-line, same as the Return key. To insert a literal LF character, press Ctrl+V Ctrl+J. The command Ctrl+V (quoted-insert) inserts the next character literally. Thus, to split a line, you can enter \ Ctrl+V.

If you do this often, you can make it a macro:

bind '"\e\C-j": "\\\C-v\C-j\C-b\C-b"'
Related Question