Shortcut to delete command but keep arguments

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1












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.










share|improve this question

















  • 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 use Esc+. 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














up vote
1
down vote

favorite
1












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.










share|improve this question

















  • 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 use Esc+. 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












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 use nano !$ or use Esc+. 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




    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






  • 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










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





share|improve this answer





























    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.






    share|improve this answer



























      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 :-)






      share|improve this answer




















        Your Answer







        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        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






























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





        share|improve this answer


























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





          share|improve this answer
























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





            share|improve this answer














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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 2 '17 at 15:52

























            answered Oct 2 '17 at 15:28









            Stephen Kitt

            145k22317382




            145k22317382






















                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.






                share|improve this answer
























                  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.






                  share|improve this answer






















                    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.






                    share|improve this answer












                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 12 at 19:18









                    Ray

                    112




                    112




















                        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 :-)






                        share|improve this answer
























                          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 :-)






                          share|improve this answer






















                            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 :-)






                            share|improve this answer












                            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 :-)







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Oct 2 '17 at 15:38









                            Sagar

                            168110




                            168110



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                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













































































                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Displaying single band from multi-band raster using QGIS

                                How many registers does an x86_64 CPU actually have?