Shortcut to delete command but keep arguments
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
When working in terminal I often have sequences of commands like:
cat long/path/to/file/xyz.file
nano long/bath/to/file/xyz.file
bash long/path/to/file/xyz.file
etc
Usually I hit control + A, right arrow a few times, backspace a few times, then write the new command.
But during the ten seconds I am doing this, I always wonder if there is some magic shortcut that will do this for me. So my question is...
Is there a terminal shortcut to delete the command but keep arguments?
If not, is there a way to write your own shortcuts?
The terminals I use most of the time are Ubuntu and OSX if that matters.
terminal keyboard-shortcuts productivity
add a comment |Â
up vote
1
down vote
favorite
When working in terminal I often have sequences of commands like:
cat long/path/to/file/xyz.file
nano long/bath/to/file/xyz.file
bash long/path/to/file/xyz.file
etc
Usually I hit control + A, right arrow a few times, backspace a few times, then write the new command.
But during the ten seconds I am doing this, I always wonder if there is some magic shortcut that will do this for me. So my question is...
Is there a terminal shortcut to delete the command but keep arguments?
If not, is there a way to write your own shortcuts?
The terminals I use most of the time are Ubuntu and OSX if that matters.
terminal keyboard-shortcuts productivity
1
You can save all the right arrow movements after Ctrl+A with one Alt-D
â Tagwint
Oct 2 '17 at 15:27
single or multiple arguments? on bash shell, you can usenano !$
or useEsc+.
shortcut to cycle through last arguments of previous commands
â Sundeep
Oct 2 '17 at 15:29
1
I would use^cat^nano
â Jeff Schaller
Oct 2 '17 at 15:52
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
When working in terminal I often have sequences of commands like:
cat long/path/to/file/xyz.file
nano long/bath/to/file/xyz.file
bash long/path/to/file/xyz.file
etc
Usually I hit control + A, right arrow a few times, backspace a few times, then write the new command.
But during the ten seconds I am doing this, I always wonder if there is some magic shortcut that will do this for me. So my question is...
Is there a terminal shortcut to delete the command but keep arguments?
If not, is there a way to write your own shortcuts?
The terminals I use most of the time are Ubuntu and OSX if that matters.
terminal keyboard-shortcuts productivity
When working in terminal I often have sequences of commands like:
cat long/path/to/file/xyz.file
nano long/bath/to/file/xyz.file
bash long/path/to/file/xyz.file
etc
Usually I hit control + A, right arrow a few times, backspace a few times, then write the new command.
But during the ten seconds I am doing this, I always wonder if there is some magic shortcut that will do this for me. So my question is...
Is there a terminal shortcut to delete the command but keep arguments?
If not, is there a way to write your own shortcuts?
The terminals I use most of the time are Ubuntu and OSX if that matters.
terminal keyboard-shortcuts productivity
terminal keyboard-shortcuts productivity
asked Oct 2 '17 at 15:22
sudo rm -rf slash
1062
1062
1
You can save all the right arrow movements after Ctrl+A with one Alt-D
â Tagwint
Oct 2 '17 at 15:27
single or multiple arguments? on bash shell, you can usenano !$
or useEsc+.
shortcut to cycle through last arguments of previous commands
â Sundeep
Oct 2 '17 at 15:29
1
I would use^cat^nano
â Jeff Schaller
Oct 2 '17 at 15:52
add a comment |Â
1
You can save all the right arrow movements after Ctrl+A with one Alt-D
â Tagwint
Oct 2 '17 at 15:27
single or multiple arguments? on bash shell, you can usenano !$
or useEsc+.
shortcut to cycle through last arguments of previous commands
â Sundeep
Oct 2 '17 at 15:29
1
I would use^cat^nano
â Jeff Schaller
Oct 2 '17 at 15:52
1
1
You can save all the right arrow movements after Ctrl+A with one Alt-D
â Tagwint
Oct 2 '17 at 15:27
You can save all the right arrow movements after Ctrl+A with one Alt-D
â Tagwint
Oct 2 '17 at 15:27
single or multiple arguments? on bash shell, you can use
nano !$
or use Esc+.
shortcut to cycle through last arguments of previous commandsâ Sundeep
Oct 2 '17 at 15:29
single or multiple arguments? on bash shell, you can use
nano !$
or use Esc+.
shortcut to cycle through last arguments of previous commandsâ Sundeep
Oct 2 '17 at 15:29
1
1
I would use
^cat^nano
â Jeff Schaller
Oct 2 '17 at 15:52
I would use
^cat^nano
â Jeff Schaller
Oct 2 '17 at 15:52
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
In many shells, AltD will delete from the cursor to the end of the word under the cursor, so you can do CtrlA followed by AltD to delete the first word.
Alternatively, in shells with history manipulation, !:1-$
will be replaced by all the parameters of the previous command, so you can type your new command followed by that to copy the arguments of the previous command:
$ echo Hello sudo rm -rf slash
Hello sudo rm -rf slash
$ printf "%s " !:1-$
Hello sudo rm -rf slash
If your commands have single arguments, or if youâÂÂre only interested in the last argument, you can shorten this to !$
; so in your case
$ cat long/path/to/file/xyz.file
$ nano !$
$ bash !$
add a comment |Â
up vote
1
down vote
I will provide an alternative way besides using !:1-$
to get all the arguments.
You may use the typo correcting pattern ^strA^strB
to replace strA
with strB
. For example, after
$ cat path/to/file
You can do:
$ ^cat^nano
which will change the first match of cat
in the previous command and execute that command again.
Obviously, this trick could be more useful in other cases, like if you want to change a filename in the middle of the previous very long command.
Hope this help.
add a comment |Â
up vote
0
down vote
Okay. So below is only applicable for bash shell or I am unsure if this works on all the shells.
Lets say you are using your command line argument in every subsequent commands.cat long/path/to/file/xyz.file
So in the next command you want to keep your argument "long/path/to/file/xyz.file" same but want to change your command, this is how you will do it nano !$
and in the next subsequent command you will do the same.bash !$
. So !$
keeps the last argument of last command as it is. For example tail -f /var/log/messages
somecommand !$
will actually become somecommand /var/log/messages
and not somecommand -f /var/log/messages
There is yet another feature that lets you keep the last command as it is. For example you want to run last command with sudo privileges. Lets say you ran tail /var/log/messages
and you don't have permissions to view contents, so instead of writing whole command with sudo you can simply do sudo !!
bash shell will automatically convert this to sudo tail /var/log/messages
Hope this helps :-)
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
In many shells, AltD will delete from the cursor to the end of the word under the cursor, so you can do CtrlA followed by AltD to delete the first word.
Alternatively, in shells with history manipulation, !:1-$
will be replaced by all the parameters of the previous command, so you can type your new command followed by that to copy the arguments of the previous command:
$ echo Hello sudo rm -rf slash
Hello sudo rm -rf slash
$ printf "%s " !:1-$
Hello sudo rm -rf slash
If your commands have single arguments, or if youâÂÂre only interested in the last argument, you can shorten this to !$
; so in your case
$ cat long/path/to/file/xyz.file
$ nano !$
$ bash !$
add a comment |Â
up vote
4
down vote
In many shells, AltD will delete from the cursor to the end of the word under the cursor, so you can do CtrlA followed by AltD to delete the first word.
Alternatively, in shells with history manipulation, !:1-$
will be replaced by all the parameters of the previous command, so you can type your new command followed by that to copy the arguments of the previous command:
$ echo Hello sudo rm -rf slash
Hello sudo rm -rf slash
$ printf "%s " !:1-$
Hello sudo rm -rf slash
If your commands have single arguments, or if youâÂÂre only interested in the last argument, you can shorten this to !$
; so in your case
$ cat long/path/to/file/xyz.file
$ nano !$
$ bash !$
add a comment |Â
up vote
4
down vote
up vote
4
down vote
In many shells, AltD will delete from the cursor to the end of the word under the cursor, so you can do CtrlA followed by AltD to delete the first word.
Alternatively, in shells with history manipulation, !:1-$
will be replaced by all the parameters of the previous command, so you can type your new command followed by that to copy the arguments of the previous command:
$ echo Hello sudo rm -rf slash
Hello sudo rm -rf slash
$ printf "%s " !:1-$
Hello sudo rm -rf slash
If your commands have single arguments, or if youâÂÂre only interested in the last argument, you can shorten this to !$
; so in your case
$ cat long/path/to/file/xyz.file
$ nano !$
$ bash !$
In many shells, AltD will delete from the cursor to the end of the word under the cursor, so you can do CtrlA followed by AltD to delete the first word.
Alternatively, in shells with history manipulation, !:1-$
will be replaced by all the parameters of the previous command, so you can type your new command followed by that to copy the arguments of the previous command:
$ echo Hello sudo rm -rf slash
Hello sudo rm -rf slash
$ printf "%s " !:1-$
Hello sudo rm -rf slash
If your commands have single arguments, or if youâÂÂre only interested in the last argument, you can shorten this to !$
; so in your case
$ cat long/path/to/file/xyz.file
$ nano !$
$ bash !$
edited Oct 2 '17 at 15:52
answered Oct 2 '17 at 15:28
Stephen Kitt
145k22317382
145k22317382
add a comment |Â
add a comment |Â
up vote
1
down vote
I will provide an alternative way besides using !:1-$
to get all the arguments.
You may use the typo correcting pattern ^strA^strB
to replace strA
with strB
. For example, after
$ cat path/to/file
You can do:
$ ^cat^nano
which will change the first match of cat
in the previous command and execute that command again.
Obviously, this trick could be more useful in other cases, like if you want to change a filename in the middle of the previous very long command.
Hope this help.
add a comment |Â
up vote
1
down vote
I will provide an alternative way besides using !:1-$
to get all the arguments.
You may use the typo correcting pattern ^strA^strB
to replace strA
with strB
. For example, after
$ cat path/to/file
You can do:
$ ^cat^nano
which will change the first match of cat
in the previous command and execute that command again.
Obviously, this trick could be more useful in other cases, like if you want to change a filename in the middle of the previous very long command.
Hope this help.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I will provide an alternative way besides using !:1-$
to get all the arguments.
You may use the typo correcting pattern ^strA^strB
to replace strA
with strB
. For example, after
$ cat path/to/file
You can do:
$ ^cat^nano
which will change the first match of cat
in the previous command and execute that command again.
Obviously, this trick could be more useful in other cases, like if you want to change a filename in the middle of the previous very long command.
Hope this help.
I will provide an alternative way besides using !:1-$
to get all the arguments.
You may use the typo correcting pattern ^strA^strB
to replace strA
with strB
. For example, after
$ cat path/to/file
You can do:
$ ^cat^nano
which will change the first match of cat
in the previous command and execute that command again.
Obviously, this trick could be more useful in other cases, like if you want to change a filename in the middle of the previous very long command.
Hope this help.
answered Feb 12 at 19:18
Ray
112
112
add a comment |Â
add a comment |Â
up vote
0
down vote
Okay. So below is only applicable for bash shell or I am unsure if this works on all the shells.
Lets say you are using your command line argument in every subsequent commands.cat long/path/to/file/xyz.file
So in the next command you want to keep your argument "long/path/to/file/xyz.file" same but want to change your command, this is how you will do it nano !$
and in the next subsequent command you will do the same.bash !$
. So !$
keeps the last argument of last command as it is. For example tail -f /var/log/messages
somecommand !$
will actually become somecommand /var/log/messages
and not somecommand -f /var/log/messages
There is yet another feature that lets you keep the last command as it is. For example you want to run last command with sudo privileges. Lets say you ran tail /var/log/messages
and you don't have permissions to view contents, so instead of writing whole command with sudo you can simply do sudo !!
bash shell will automatically convert this to sudo tail /var/log/messages
Hope this helps :-)
add a comment |Â
up vote
0
down vote
Okay. So below is only applicable for bash shell or I am unsure if this works on all the shells.
Lets say you are using your command line argument in every subsequent commands.cat long/path/to/file/xyz.file
So in the next command you want to keep your argument "long/path/to/file/xyz.file" same but want to change your command, this is how you will do it nano !$
and in the next subsequent command you will do the same.bash !$
. So !$
keeps the last argument of last command as it is. For example tail -f /var/log/messages
somecommand !$
will actually become somecommand /var/log/messages
and not somecommand -f /var/log/messages
There is yet another feature that lets you keep the last command as it is. For example you want to run last command with sudo privileges. Lets say you ran tail /var/log/messages
and you don't have permissions to view contents, so instead of writing whole command with sudo you can simply do sudo !!
bash shell will automatically convert this to sudo tail /var/log/messages
Hope this helps :-)
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Okay. So below is only applicable for bash shell or I am unsure if this works on all the shells.
Lets say you are using your command line argument in every subsequent commands.cat long/path/to/file/xyz.file
So in the next command you want to keep your argument "long/path/to/file/xyz.file" same but want to change your command, this is how you will do it nano !$
and in the next subsequent command you will do the same.bash !$
. So !$
keeps the last argument of last command as it is. For example tail -f /var/log/messages
somecommand !$
will actually become somecommand /var/log/messages
and not somecommand -f /var/log/messages
There is yet another feature that lets you keep the last command as it is. For example you want to run last command with sudo privileges. Lets say you ran tail /var/log/messages
and you don't have permissions to view contents, so instead of writing whole command with sudo you can simply do sudo !!
bash shell will automatically convert this to sudo tail /var/log/messages
Hope this helps :-)
Okay. So below is only applicable for bash shell or I am unsure if this works on all the shells.
Lets say you are using your command line argument in every subsequent commands.cat long/path/to/file/xyz.file
So in the next command you want to keep your argument "long/path/to/file/xyz.file" same but want to change your command, this is how you will do it nano !$
and in the next subsequent command you will do the same.bash !$
. So !$
keeps the last argument of last command as it is. For example tail -f /var/log/messages
somecommand !$
will actually become somecommand /var/log/messages
and not somecommand -f /var/log/messages
There is yet another feature that lets you keep the last command as it is. For example you want to run last command with sudo privileges. Lets say you ran tail /var/log/messages
and you don't have permissions to view contents, so instead of writing whole command with sudo you can simply do sudo !!
bash shell will automatically convert this to sudo tail /var/log/messages
Hope this helps :-)
answered Oct 2 '17 at 15:38
Sagar
168110
168110
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395673%2fshortcut-to-delete-command-but-keep-arguments%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
You can save all the right arrow movements after Ctrl+A with one Alt-D
â Tagwint
Oct 2 '17 at 15:27
single or multiple arguments? on bash shell, you can use
nano !$
or useEsc+.
shortcut to cycle through last arguments of previous commandsâ Sundeep
Oct 2 '17 at 15:29
1
I would use
^cat^nano
â Jeff Schaller
Oct 2 '17 at 15:52