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


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.

  • 1
    You can save all the right arrow movements after Ctrl+A with one Alt-D
    – Tagwint
    Oct 2, 2017 at 15:27
  • 1
    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, 2017 at 15:29
  • 1
    I would use ^cat^nano
    – Jeff Schaller
    Oct 2, 2017 at 15:52

3 Answers 3


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

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.


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .