Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to insert newline in a already existing BASH string on the command line

Tags:

linux

bash

Suppose I have typed and executed a long BASH command on the command line. Now I want to split it up. So with the history I have my long command again, but now I cannot give Enter to insert a newline. How do you do that?

like image 926
ericj Avatar asked Oct 22 '13 08:10

ericj


1 Answers

You can use two shortcuts to do that ctrl + k and ctrl + y:

echo "some command" && echo "some other long command"

Now move cursor somewhere (in my example, cursor is marked by >):

echo "some command" && > echo "some other command"

Now press ctrl + k - this will cut everything after a cursor:

echo "some command" && >

Now put \ (backslash) and press enter:

echo "some command" && \
>

And now paste the part you've previously cut by ctrl + y:

echo "some command" && \
echo "some other long command"

Edit: to move more easily around in a long command, you can use shortcuts:

  • alt + b - move one word backwards (on Mac OS X: ESC + b)
  • alt + f - move one word forwards (on Mac OS X: ESC + f)

Ultra-solution

You can also open current line in a editor using Ctrl-x + Ctrl-e (two shortcuts, one after another). Then edit it just as a regular text file, save & quit and voila, edited command will execute.

If you want to choose which editor to use, just set EDITOR environment variable.

like image 69
kamituel Avatar answered Sep 23 '22 21:09

kamituel