8

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
  • 1
    If you are at an interactive command line prompt, is there any particular reason you need to do this besides making your shell history look prettier? Functionally, extra newlines make no difference to the shell. If you are typing a long complex shell snippet, it's best to use an editor. Look into the fc command as well - it is available in nearly all shells. – jw013 Jun 18 '13 at 18:47
  • Not prettier but just to make complex commands readable. – n.r. Jun 19 '13 at 15:31
4

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"'
6

You can use edit-and-execute-command, usually mapped to Ctrl+x+e, to open the current command in an editor. When exiting the editor the command will be run.

  • 1
    Nice - never knew this. You'll need to make sure your $EDITOR environment variable is set to something - I added vim to mine like so: export EDITOR=vim. – slm Jun 17 '13 at 16:06
  • 1
    How can I cancel the execution of the command at exit of the editor? I use :q! or ZQ but of no avail. – Birei Jul 26 '13 at 8:09
  • 1
    @Birei: Delete all the lines then save and exit (e.g. ggdG:wq or :%d|wq, etc.). The command will remain in history (possibly as a duplicate if you recalled it from an earlier entry), but it won't be executed. – Dennis Williamson Apr 5 '16 at 22:28
  • @DennisWilliamson: Wow!! Nearly three years later I yet hadn't solved this issue, so many thanks because the solution was so simple, and simplicity is always better. – Birei Apr 6 '16 at 10:51
2

Note - this answer specific to vi and vim

set -o vi    # edit commands using vi
VISUAL=/usr/bin/vim

To edit the current typed line, hit ESC (to get into vi or vim command mode), then type v or vi.

Note all the vi/vim edit commands work. e.g.

  • ESC + A puts to the end of the current typed line.
  • ESC + 0 puts the cursor at the start of the line.
  • Excellent. This definitely makes editing command lines much easier. But do you know how to insert a new line character without going into visual mode? – n.r. Jun 17 '13 at 16:35
  • 2
    Thanks. CTRL + v then CTRL + j to throw a newline should work. – suspectus Jun 17 '13 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.