46

When entering a command which spans multiple lines in Bash, how do I edit previous lines? I'm a Linux Mint (Lisa) user using GNOME Terminal.

For example, let's say I type:

$ echo "foo bar
> baz
>

And then when I'm about to type the third line of the command, suddenly realize I want "foo" and "bar" to be on separate lines. How would I reposition my cursor between "foo" and "bar" such that I could press enter and put them on separate lines?

(It's not easy like you might think. Up arrow doesn't work, neither does Ctrl-P. So please, try it out before posting! Thanks!)

1
  • 2
    The accepted answer says there is no solution, but there is. See below
    – cdosborn
    Commented Apr 9, 2015 at 17:40

4 Answers 4

36

That, unfortunately, is up to bash, not to the terminal. Your options are:

  1. Use semicolons instead of newlines, although even then you can't move up a screen line at a time but must use character or word motion commands. (Oddly, zsh at least lets you move within a compound command when editing history, just not within the current command.) Sometimes fc (which tosses you into your editor with the previous command) is the easiest way to handle compound commands.

  2. If you are using Bash, use the following key combination:

    ctrl x e
    

    It will open up the command you are working on using your text editor. Save the file and quit. (I found the command on the Shell Hater's presentation.) Zsh users have this alternative.

4
  • 1
    The bash man page explains how to change which editor is used: edit-and-execute-command (C-xC-e) - Invoke an editor on the current command line, and execute the result as shell commands. Bash attempts to invoke $VISUAL, $EDITOR, and emacs as the editor, in that order.
    – Matthew
    Commented May 13, 2014 at 16:40
  • There is a solution, without opening the editor.
    – cdosborn
    Commented Mar 30, 2015 at 18:32
  • 1
    Technically its not up to bash - but to the "readline" library that bash uses to get inputs. Commented Oct 18, 2018 at 18:04
  • the key combo (that works for me) is "Ctrl+X, Ctrl+E" in contemporary notation Commented Jun 27 at 8:09
21

The solution is to never enter a command until the multi-line is right, just type: CtrlvCtrlj when you want to go to the next line. Metab to go back a word.

solution

credit to @rici's answer

2
  • What is the Meta button?
    – Jas
    Commented Oct 27, 2021 at 7:10
  • 1
    @Jas the Meta Button is pressing Alt and the keystrokes (simultaneously) or pressing Escape then the keystroke (alterlatively)
    – Tinmarino
    Commented Dec 27, 2021 at 13:17
12

Funnily enough, Ctrl+C is what you are looking for.

when you are on

$ echo "foo bar
> baz
>

just press Ctrl+C (edited command line will suspend) and press Up (previous-history). Your prompt will be: (note the absence of >)

$ echo "foo bar
baz

Now you can move around with Left Right even through line jumps.

There is only one quirk, you must be on the last character to add another line, so move around to edit existing lines (go to start with Ctrl+A) an press Enter if that's enough or goto end (Ctrl+E) to add more lines with Enter. Another drawback is that Ctrl+_ (undo) only restores changes from last Ctrl+C

-4

Copy the commands and paste them in notepad then format it from there. After that you can copy your commands back to the terminal.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .