2

Enter a very long command at shell. Now continue to up-arrow into history, tweak it, and make it a lot longer. At some point you're thinking: "I should have made this a multi-line command when I started". Well, now that the command is all just one line, is there a way to break it up?

Example:

foo | grep "aaaaaaaaaaaaaaaaaa" | grep "bbbbbbbbbbbbbb" | more

Now to make that easier to continue editing:

foo |\    << enter some control character here to split the line
grep "aaaaaaaaaaaaaaaaaa" |\
grep "bbbbbbbbbbbbbb" |\
more

In the above example, it wouldn't work to position the cursor after the pipe, enter backslash, then Ctrl+Enter. Nor Shift+Enter, Ctrl+N, etc. Depending on the keystroke a new line does open with >, but it appears to be waiting for text which would follow the last character ( more above, rather than foo |).

The old fashioned way to get out of this situation is to copy the line (using the mouse or from .history), paste it into an editor, break it up and add the \ EOL on each line, then copy/paste it back into the CLI. That's ugly-primitive.

Is there an easier way to break up a long line of multiple commands in the terminal?

1
3

Multiline commands always will be archived in the command history as a single line. On the terminal prompt, you cannot edit a line to appear on multiple lines.

The approach to go about this is, is to open the command in your default editor. With the cursor sowewhere in your command, hit Ctrl+XE. This opens the command in your default editor (nano on Ubuntu), where you can do all your edits, including splitting the lines. In nano, hit Ctrl+SX to save the changes and close the editor: your command will immediately be executed.

2
  • Tried it, works perfectly. Re-editing a multi-line command also works. TYVM – TonyG May 3 at 16:45
  • Thanks, this is useful :-) – sudodus Jun 8 at 9:21

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.