Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!)

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
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 May 13 at 16:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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