Reputation: 2301
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?
Upvotes: 8
Views: 3733
Reputation: 379
Press Ctrl+v, Ctrl+j.
Interactive Bash uses the readline library for command line editing. Normally readline accepts the line upon Ctrl+j (newline, NL
, ^J
, \n
, 0x0a
) or Ctrl+m (carriage return, CR
, ^M
, \r
, 0x0d
). By default Ctrl+v makes readline read the next character literally.
Note Ctrl+v, Enter is not a solution. Enter is transmitted as carriage return, it's equivalent to Ctrl+m. In some cases this carriage return gets converted to newline, but not right after Ctrl+v. You need readline to get a newline without accepting the line, so use Ctrl+v, Ctrl+j. Readline will split the line exactly as you expect.
Upvotes: 1
Reputation: 35970
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.
Upvotes: 19
Reputation: 8946
You can create text file for script. For example:
test.sh
#!/bin/bash
echo Hello, world!
So you will need to execute this:
chmod +x test.sh
./test.sh
Upvotes: 0