How do I repeat the last command without using the arrow keys?











up vote
96
down vote

favorite
47












I know I can use Up to iterate through previous commands. Running the last command simply involves Up + Enter. However, I was thinking of buying the Happy Hacking Keyboard as I spend a lot of time in vim.



This keyboard has no arrow keys, and the only way I know how to get this kind of behaviour is by pressing Ctrl + R and beginning to repeat my previous command.



Is there an easy way to emulate Up + Enter in an UNIX terminal without the arrow keys?










share|improve this question




















  • 1




    You should replace the terminal tag with the shell that you use.
    – Cristian Ciupitu
    Jul 31 '14 at 7:40






  • 2




    @illuminÉ no mouse...
    – quant
    Aug 2 '14 at 0:15










  • Wasn't Happy Hacking Keyboard discontinued in 2006?
    – Peter Mortensen
    Aug 19 at 17:41










  • @PeterMortensen no it's still available. You might be referring to the first version, but the newer models continue to be available.
    – quant
    Aug 19 at 17:55















up vote
96
down vote

favorite
47












I know I can use Up to iterate through previous commands. Running the last command simply involves Up + Enter. However, I was thinking of buying the Happy Hacking Keyboard as I spend a lot of time in vim.



This keyboard has no arrow keys, and the only way I know how to get this kind of behaviour is by pressing Ctrl + R and beginning to repeat my previous command.



Is there an easy way to emulate Up + Enter in an UNIX terminal without the arrow keys?










share|improve this question




















  • 1




    You should replace the terminal tag with the shell that you use.
    – Cristian Ciupitu
    Jul 31 '14 at 7:40






  • 2




    @illuminÉ no mouse...
    – quant
    Aug 2 '14 at 0:15










  • Wasn't Happy Hacking Keyboard discontinued in 2006?
    – Peter Mortensen
    Aug 19 at 17:41










  • @PeterMortensen no it's still available. You might be referring to the first version, but the newer models continue to be available.
    – quant
    Aug 19 at 17:55













up vote
96
down vote

favorite
47









up vote
96
down vote

favorite
47






47





I know I can use Up to iterate through previous commands. Running the last command simply involves Up + Enter. However, I was thinking of buying the Happy Hacking Keyboard as I spend a lot of time in vim.



This keyboard has no arrow keys, and the only way I know how to get this kind of behaviour is by pressing Ctrl + R and beginning to repeat my previous command.



Is there an easy way to emulate Up + Enter in an UNIX terminal without the arrow keys?










share|improve this question















I know I can use Up to iterate through previous commands. Running the last command simply involves Up + Enter. However, I was thinking of buying the Happy Hacking Keyboard as I spend a lot of time in vim.



This keyboard has no arrow keys, and the only way I know how to get this kind of behaviour is by pressing Ctrl + R and beginning to repeat my previous command.



Is there an easy way to emulate Up + Enter in an UNIX terminal without the arrow keys?







shell keyboard-shortcuts command-history






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 1 '14 at 1:40









Sparhawk

9,03563789




9,03563789










asked Jul 31 '14 at 3:12









quant

1,49852040




1,49852040








  • 1




    You should replace the terminal tag with the shell that you use.
    – Cristian Ciupitu
    Jul 31 '14 at 7:40






  • 2




    @illuminÉ no mouse...
    – quant
    Aug 2 '14 at 0:15










  • Wasn't Happy Hacking Keyboard discontinued in 2006?
    – Peter Mortensen
    Aug 19 at 17:41










  • @PeterMortensen no it's still available. You might be referring to the first version, but the newer models continue to be available.
    – quant
    Aug 19 at 17:55














  • 1




    You should replace the terminal tag with the shell that you use.
    – Cristian Ciupitu
    Jul 31 '14 at 7:40






  • 2




    @illuminÉ no mouse...
    – quant
    Aug 2 '14 at 0:15










  • Wasn't Happy Hacking Keyboard discontinued in 2006?
    – Peter Mortensen
    Aug 19 at 17:41










  • @PeterMortensen no it's still available. You might be referring to the first version, but the newer models continue to be available.
    – quant
    Aug 19 at 17:55








1




1




You should replace the terminal tag with the shell that you use.
– Cristian Ciupitu
Jul 31 '14 at 7:40




You should replace the terminal tag with the shell that you use.
– Cristian Ciupitu
Jul 31 '14 at 7:40




2




2




@illuminÉ no mouse...
– quant
Aug 2 '14 at 0:15




@illuminÉ no mouse...
– quant
Aug 2 '14 at 0:15












Wasn't Happy Hacking Keyboard discontinued in 2006?
– Peter Mortensen
Aug 19 at 17:41




Wasn't Happy Hacking Keyboard discontinued in 2006?
– Peter Mortensen
Aug 19 at 17:41












@PeterMortensen no it's still available. You might be referring to the first version, but the newer models continue to be available.
– quant
Aug 19 at 17:55




@PeterMortensen no it's still available. You might be referring to the first version, but the newer models continue to be available.
– quant
Aug 19 at 17:55










13 Answers
13






active

oldest

votes

















up vote
143
down vote



accepted










With csh or any shell implementing csh-like history substitution (tcsh, bash, zsh):



!!


Then Enter.





Or alternatively:



!-1


Then Enter.





Or Ctrl+P, Enter





Magic space



Also, note that !! and !-1 will not auto-expand for you, until you execute them (when it might be too late).



If using bash, you can put bind Space:magic-space into ~/.bashrc, then pressing Space after the command will auto-expand them inline, allowing you to inspect them before execution. This is particularly useful for history expansion from a command run a while ago, e.g. !echo will pull the last command run starting with echo. With magic space, you get to preview the command before it's run.



That's the equivalent of doing bindkey ' ' magic-space in tcsh or zsh.






share|improve this answer



















  • 1




    @Arman Glad to help. There's a whole lot of history expansion commands; the link lists some of them. I've edited in some information about magic space that might help with usage of some of these.
    – Sparhawk
    Jul 31 '14 at 4:01






  • 3




    magic-space that is one sweet option!
    – fduff
    Jul 31 '14 at 7:10






  • 1




    There's also the histverify option to shopt which will cause readline to perform the history expansion but not execute the command on the first press of the enter key. Which will let you evaluate the result and decide if that's what you want to run.
    – Etan Reisner
    Jul 31 '14 at 13:33






  • 2




    Never understood why !! is used so much more than CTRL-P. CTRL-P lets you see the command before executing it -- and you can even make modifications to it. I always feel like I'm rolling the dice with !!.
    – abonet
    Aug 5 '14 at 21:30






  • 2




    This post is pure gold xD
    – George Kastrinis
    Jan 25 '16 at 10:52


















up vote
42
down vote













Most shells that have a command line editing feature
support Emacs key bindings. (a tiny subset)



Up     Ctrl+P
Down Ctrl+N
Left Ctrl+B
Right Ctrl+F
Home Ctrl+A
End Ctrl+E
Delete Ctrl+D


Alternatively, you could set up your shell to use vi command editing mode, by adding set -o vi to your shell startup file (e.g., ~/.bashrc). 
Then, for example, you can




  • Use EsckEnter
    to re-execute the previous command (same as !!). 
    The minus key - also works as a "move up" command.

  • Use EsckkkkkkkkkkkkEnter
    or Esc12kEnter
    to re-execute the 12th previous command (same as !-12).

  • Use Esc and a motion command
    (i.e., k, suitably repeated),
    and then edit the bash command line you find there. 
    Remember, you will still be in vi command mode,
    so you will need to use a vi editing command
    (e.g., I, A, C, R,
    or one of their lowercase counterparts)
    to actually add to the command from history.

    So, for example, EsckisudoSpaceEnter is equivalent to sudo !!.

  • For advanced users: you can even copy (yank) text from one line
    and paste (put) it on another,
    so you can accomplish results comparable to !-2:- !$
    (Unfortunately, it does not seem to support named buffers.)






share|improve this answer























  • I knew about the emacs relation with the terminal... but the vi trick was great!!!
    – Dox
    Aug 6 '14 at 20:06










  • I got for delete Ctl+u in Zsh
    – Timo
    Jan 11 at 21:08






  • 1




    This is implemented by the readline library. Further shortcuts may be found on its documentation.
    – Spidey
    Feb 18 at 18:38


















up vote
22
down vote













My favorite one is CTRL + P then CTRL + O



This works by default, no extra configuration needed. ^P will let you switch to the last command and ^O will let you execute current line



Note that CTRL + O can be used for as many times as you want






share|improve this answer























  • ^O does not do anything on my Mac.
    – Snowcrash
    Jul 14 at 22:59












  • @Snowcrash Try ^J
    – daisy
    Jul 20 at 5:48










  • that just echoes a blank line even if I've done an ls or pwd previously.
    – Snowcrash
    Jul 20 at 8:54










  • @Snowcrash It's ^P then ^J ...
    – daisy
    Jul 20 at 9:05










  • Ctrl + P does not work on HP-UX (I don't know which shell it is using). The workaround is to invoke `zsh'.
    – Peter Mortensen
    Jul 31 at 11:35




















up vote
14
down vote













Sure! Since you're used to vi keybindings, why not configure your shell to respond to them? For bash, put this in your ~/.inputrc:



set editing-mode vi


Running instances of bash will not re-read the file, so log out and back in.



zsh will even detect this for you: if none of your startup scripts force the editing mode one way or the other using bindkey and if your $EDITOR environment variable is detected to contain vi, then it will automatically enable vi keybindings. If you need to force it, put this in your ~/.zshrc:



bindkey -v


Thereafter, use ESC as usual to enter command line and k and j to move up and down.



ALSO: The default shell bindings in most shells are the emacs bindings, so actually Crtl-P and Ctrl-N should already work without you having to change anything.






share|improve this answer




























    up vote
    7
    down vote













    With any POSIX shell implementing the User Portability option (includes ksh, zsh, bash, yash), you can also use the fc command:



    fc -e : -1


    See an example:



    $ echo "hello"
    hello

    $ fc -e : -1
    echo "hello"
    hello


    More info in Execute a range of commands from history's answer by Jonathan Leffler.






    share|improve this answer






























      up vote
      5
      down vote













      I find I need to redo commands as super user often, so



      sudo !!


      redoes the previous command as if I had remembered to type sudo in the first place.






      share|improve this answer



















      • 2




        I really like this, also looks like a good response to "authorization denied". "I meant sudo ffs!"
        – Viktor Mellgren
        Aug 1 '14 at 11:20


















      up vote
      4
      down vote













      For all you Mac iterm2 users:



      You can bind +R to 0x0C 0x10 0x0d. This will clear the terminal and run the last command.



      iterm 2 key preferences






      share|improve this answer























      • Is there any way to repeat the last command without clearing the terminal?
        – Luke Davis
        Jan 16 at 1:41










      • Ah, nevermind: for anyone reading this, to repeat without clearing the terminal, use 0x10 0x0d (i.e. omit the 0x0C).
        – Luke Davis
        Jan 16 at 1:42


















      up vote
      2
      down vote













      With csh or any shell implementing csh-like history substitution (tcsh, bash, zsh), you can also use the !<beginning of command> to call the last command beginning with <beginning of command>.



      for example if you ran



      $ tail file.txt
      $ less otherfile.txt
      $ !ta


      !ta would execute tail file.txt






      share|improve this answer






























        up vote
        1
        down vote













        The ksh shell has a default alias r that repeats the most recent command. It is bound to fc -s:



        alias r='fc -s'


        Interestingly, the bash documentation for the fc builtin (help fc, and also in the manual itself) even mentions this, but it's not a default alias in that shell:




        A useful alias to use with this is r='fc -s', so that typing r cc
        runs the last command beginning with cc and typing r re-executes
        the last command.




        In bash, this would also work even if you have turned history expansions off with set +H.



        In the zsh shell, there is a r builtin that is documented to be the same as fc -e -.



        The fc utility is a POSIX standard utility.






        share|improve this answer






























          up vote
          0
          down vote













          When I am using a good xterm emulator, e.g. putty, my favorite way to proceed is to select the command including line feed, copy it to clipboard and then use right click on the mouse to paste it. This repeats the command in a single click.
          This also works fine for a sequence of commands separated by semi-colons.






          share|improve this answer




























            up vote
            0
            down vote













            tl;dr !! just appends the last command to your current command. <backtick>!!<backtick> executes the last command and appends the output to your current command.



            There are actually 2 variants:




            1. The last command itself

            2. The result of the last command


            Let me show you 2 examples:



            Example 1



            $ mkdir /test
            mkdir: /test: Permission denied

            $ sudo !!
            sudo mkdir /test
            Password:


            As you see above, I'm executing a command and essentially just appending the last command.



            Another variation



            Example 2



            $ find ~/Documents "test.txt"
            /Users/<user>/Documents/test.txt

            $ vi `!!`
            # Opens test.txt. But if you do
            $ vi !!
            vi find ~/Documents -name "test.txt"
            VIM - Vi IMproved 8.1 (2018 May 18, compiled Oct 29 2018 06:55:58)
            Unknown option argument: "-name"
            More info with: "vim -h"





            share|improve this answer








            New contributor




            Pradyumna Shembekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

























              up vote
              0
              down vote














              • Type History and note the number in front of the command that you to
                execute and use !number


              • You can also use !-1 (1 being the last command you execute, replace
                it with the count number that you get when you do it from below
                keeping the last executed as 1 )



              • !-2 (second last and so on)






              share|improve this answer










              New contributor




              Lovish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

























                up vote
                -1
                down vote













                you can use !n to execute nth command in terminal.
                where 'n' is line no of history command.






                share|improve this answer























                • Which shell expands !? to the last command? Are you sure you're not mixing it up with !! (which has already been covered in other answers)?
                  – Anthony Geoghegan
                  Feb 18 at 16:53










                • sorry, actually i was experimenting this command at that time. please check edited answer. @AnthonyGeoghegan
                  – Ravi Sevta
                  Feb 18 at 17:45













                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: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                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%2f147563%2fhow-do-i-repeat-the-last-command-without-using-the-arrow-keys%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                13 Answers
                13






                active

                oldest

                votes








                13 Answers
                13






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                143
                down vote



                accepted










                With csh or any shell implementing csh-like history substitution (tcsh, bash, zsh):



                !!


                Then Enter.





                Or alternatively:



                !-1


                Then Enter.





                Or Ctrl+P, Enter





                Magic space



                Also, note that !! and !-1 will not auto-expand for you, until you execute them (when it might be too late).



                If using bash, you can put bind Space:magic-space into ~/.bashrc, then pressing Space after the command will auto-expand them inline, allowing you to inspect them before execution. This is particularly useful for history expansion from a command run a while ago, e.g. !echo will pull the last command run starting with echo. With magic space, you get to preview the command before it's run.



                That's the equivalent of doing bindkey ' ' magic-space in tcsh or zsh.






                share|improve this answer



















                • 1




                  @Arman Glad to help. There's a whole lot of history expansion commands; the link lists some of them. I've edited in some information about magic space that might help with usage of some of these.
                  – Sparhawk
                  Jul 31 '14 at 4:01






                • 3




                  magic-space that is one sweet option!
                  – fduff
                  Jul 31 '14 at 7:10






                • 1




                  There's also the histverify option to shopt which will cause readline to perform the history expansion but not execute the command on the first press of the enter key. Which will let you evaluate the result and decide if that's what you want to run.
                  – Etan Reisner
                  Jul 31 '14 at 13:33






                • 2




                  Never understood why !! is used so much more than CTRL-P. CTRL-P lets you see the command before executing it -- and you can even make modifications to it. I always feel like I'm rolling the dice with !!.
                  – abonet
                  Aug 5 '14 at 21:30






                • 2




                  This post is pure gold xD
                  – George Kastrinis
                  Jan 25 '16 at 10:52















                up vote
                143
                down vote



                accepted










                With csh or any shell implementing csh-like history substitution (tcsh, bash, zsh):



                !!


                Then Enter.





                Or alternatively:



                !-1


                Then Enter.





                Or Ctrl+P, Enter





                Magic space



                Also, note that !! and !-1 will not auto-expand for you, until you execute them (when it might be too late).



                If using bash, you can put bind Space:magic-space into ~/.bashrc, then pressing Space after the command will auto-expand them inline, allowing you to inspect them before execution. This is particularly useful for history expansion from a command run a while ago, e.g. !echo will pull the last command run starting with echo. With magic space, you get to preview the command before it's run.



                That's the equivalent of doing bindkey ' ' magic-space in tcsh or zsh.






                share|improve this answer



















                • 1




                  @Arman Glad to help. There's a whole lot of history expansion commands; the link lists some of them. I've edited in some information about magic space that might help with usage of some of these.
                  – Sparhawk
                  Jul 31 '14 at 4:01






                • 3




                  magic-space that is one sweet option!
                  – fduff
                  Jul 31 '14 at 7:10






                • 1




                  There's also the histverify option to shopt which will cause readline to perform the history expansion but not execute the command on the first press of the enter key. Which will let you evaluate the result and decide if that's what you want to run.
                  – Etan Reisner
                  Jul 31 '14 at 13:33






                • 2




                  Never understood why !! is used so much more than CTRL-P. CTRL-P lets you see the command before executing it -- and you can even make modifications to it. I always feel like I'm rolling the dice with !!.
                  – abonet
                  Aug 5 '14 at 21:30






                • 2




                  This post is pure gold xD
                  – George Kastrinis
                  Jan 25 '16 at 10:52













                up vote
                143
                down vote



                accepted







                up vote
                143
                down vote



                accepted






                With csh or any shell implementing csh-like history substitution (tcsh, bash, zsh):



                !!


                Then Enter.





                Or alternatively:



                !-1


                Then Enter.





                Or Ctrl+P, Enter





                Magic space



                Also, note that !! and !-1 will not auto-expand for you, until you execute them (when it might be too late).



                If using bash, you can put bind Space:magic-space into ~/.bashrc, then pressing Space after the command will auto-expand them inline, allowing you to inspect them before execution. This is particularly useful for history expansion from a command run a while ago, e.g. !echo will pull the last command run starting with echo. With magic space, you get to preview the command before it's run.



                That's the equivalent of doing bindkey ' ' magic-space in tcsh or zsh.






                share|improve this answer














                With csh or any shell implementing csh-like history substitution (tcsh, bash, zsh):



                !!


                Then Enter.





                Or alternatively:



                !-1


                Then Enter.





                Or Ctrl+P, Enter





                Magic space



                Also, note that !! and !-1 will not auto-expand for you, until you execute them (when it might be too late).



                If using bash, you can put bind Space:magic-space into ~/.bashrc, then pressing Space after the command will auto-expand them inline, allowing you to inspect them before execution. This is particularly useful for history expansion from a command run a while ago, e.g. !echo will pull the last command run starting with echo. With magic space, you get to preview the command before it's run.



                That's the equivalent of doing bindkey ' ' magic-space in tcsh or zsh.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 1 '14 at 1:34

























                answered Jul 31 '14 at 3:53









                Sparhawk

                9,03563789




                9,03563789








                • 1




                  @Arman Glad to help. There's a whole lot of history expansion commands; the link lists some of them. I've edited in some information about magic space that might help with usage of some of these.
                  – Sparhawk
                  Jul 31 '14 at 4:01






                • 3




                  magic-space that is one sweet option!
                  – fduff
                  Jul 31 '14 at 7:10






                • 1




                  There's also the histverify option to shopt which will cause readline to perform the history expansion but not execute the command on the first press of the enter key. Which will let you evaluate the result and decide if that's what you want to run.
                  – Etan Reisner
                  Jul 31 '14 at 13:33






                • 2




                  Never understood why !! is used so much more than CTRL-P. CTRL-P lets you see the command before executing it -- and you can even make modifications to it. I always feel like I'm rolling the dice with !!.
                  – abonet
                  Aug 5 '14 at 21:30






                • 2




                  This post is pure gold xD
                  – George Kastrinis
                  Jan 25 '16 at 10:52














                • 1




                  @Arman Glad to help. There's a whole lot of history expansion commands; the link lists some of them. I've edited in some information about magic space that might help with usage of some of these.
                  – Sparhawk
                  Jul 31 '14 at 4:01






                • 3




                  magic-space that is one sweet option!
                  – fduff
                  Jul 31 '14 at 7:10






                • 1




                  There's also the histverify option to shopt which will cause readline to perform the history expansion but not execute the command on the first press of the enter key. Which will let you evaluate the result and decide if that's what you want to run.
                  – Etan Reisner
                  Jul 31 '14 at 13:33






                • 2




                  Never understood why !! is used so much more than CTRL-P. CTRL-P lets you see the command before executing it -- and you can even make modifications to it. I always feel like I'm rolling the dice with !!.
                  – abonet
                  Aug 5 '14 at 21:30






                • 2




                  This post is pure gold xD
                  – George Kastrinis
                  Jan 25 '16 at 10:52








                1




                1




                @Arman Glad to help. There's a whole lot of history expansion commands; the link lists some of them. I've edited in some information about magic space that might help with usage of some of these.
                – Sparhawk
                Jul 31 '14 at 4:01




                @Arman Glad to help. There's a whole lot of history expansion commands; the link lists some of them. I've edited in some information about magic space that might help with usage of some of these.
                – Sparhawk
                Jul 31 '14 at 4:01




                3




                3




                magic-space that is one sweet option!
                – fduff
                Jul 31 '14 at 7:10




                magic-space that is one sweet option!
                – fduff
                Jul 31 '14 at 7:10




                1




                1




                There's also the histverify option to shopt which will cause readline to perform the history expansion but not execute the command on the first press of the enter key. Which will let you evaluate the result and decide if that's what you want to run.
                – Etan Reisner
                Jul 31 '14 at 13:33




                There's also the histverify option to shopt which will cause readline to perform the history expansion but not execute the command on the first press of the enter key. Which will let you evaluate the result and decide if that's what you want to run.
                – Etan Reisner
                Jul 31 '14 at 13:33




                2




                2




                Never understood why !! is used so much more than CTRL-P. CTRL-P lets you see the command before executing it -- and you can even make modifications to it. I always feel like I'm rolling the dice with !!.
                – abonet
                Aug 5 '14 at 21:30




                Never understood why !! is used so much more than CTRL-P. CTRL-P lets you see the command before executing it -- and you can even make modifications to it. I always feel like I'm rolling the dice with !!.
                – abonet
                Aug 5 '14 at 21:30




                2




                2




                This post is pure gold xD
                – George Kastrinis
                Jan 25 '16 at 10:52




                This post is pure gold xD
                – George Kastrinis
                Jan 25 '16 at 10:52












                up vote
                42
                down vote













                Most shells that have a command line editing feature
                support Emacs key bindings. (a tiny subset)



                Up     Ctrl+P
                Down Ctrl+N
                Left Ctrl+B
                Right Ctrl+F
                Home Ctrl+A
                End Ctrl+E
                Delete Ctrl+D


                Alternatively, you could set up your shell to use vi command editing mode, by adding set -o vi to your shell startup file (e.g., ~/.bashrc). 
                Then, for example, you can




                • Use EsckEnter
                  to re-execute the previous command (same as !!). 
                  The minus key - also works as a "move up" command.

                • Use EsckkkkkkkkkkkkEnter
                  or Esc12kEnter
                  to re-execute the 12th previous command (same as !-12).

                • Use Esc and a motion command
                  (i.e., k, suitably repeated),
                  and then edit the bash command line you find there. 
                  Remember, you will still be in vi command mode,
                  so you will need to use a vi editing command
                  (e.g., I, A, C, R,
                  or one of their lowercase counterparts)
                  to actually add to the command from history.

                  So, for example, EsckisudoSpaceEnter is equivalent to sudo !!.

                • For advanced users: you can even copy (yank) text from one line
                  and paste (put) it on another,
                  so you can accomplish results comparable to !-2:- !$
                  (Unfortunately, it does not seem to support named buffers.)






                share|improve this answer























                • I knew about the emacs relation with the terminal... but the vi trick was great!!!
                  – Dox
                  Aug 6 '14 at 20:06










                • I got for delete Ctl+u in Zsh
                  – Timo
                  Jan 11 at 21:08






                • 1




                  This is implemented by the readline library. Further shortcuts may be found on its documentation.
                  – Spidey
                  Feb 18 at 18:38















                up vote
                42
                down vote













                Most shells that have a command line editing feature
                support Emacs key bindings. (a tiny subset)



                Up     Ctrl+P
                Down Ctrl+N
                Left Ctrl+B
                Right Ctrl+F
                Home Ctrl+A
                End Ctrl+E
                Delete Ctrl+D


                Alternatively, you could set up your shell to use vi command editing mode, by adding set -o vi to your shell startup file (e.g., ~/.bashrc). 
                Then, for example, you can




                • Use EsckEnter
                  to re-execute the previous command (same as !!). 
                  The minus key - also works as a "move up" command.

                • Use EsckkkkkkkkkkkkEnter
                  or Esc12kEnter
                  to re-execute the 12th previous command (same as !-12).

                • Use Esc and a motion command
                  (i.e., k, suitably repeated),
                  and then edit the bash command line you find there. 
                  Remember, you will still be in vi command mode,
                  so you will need to use a vi editing command
                  (e.g., I, A, C, R,
                  or one of their lowercase counterparts)
                  to actually add to the command from history.

                  So, for example, EsckisudoSpaceEnter is equivalent to sudo !!.

                • For advanced users: you can even copy (yank) text from one line
                  and paste (put) it on another,
                  so you can accomplish results comparable to !-2:- !$
                  (Unfortunately, it does not seem to support named buffers.)






                share|improve this answer























                • I knew about the emacs relation with the terminal... but the vi trick was great!!!
                  – Dox
                  Aug 6 '14 at 20:06










                • I got for delete Ctl+u in Zsh
                  – Timo
                  Jan 11 at 21:08






                • 1




                  This is implemented by the readline library. Further shortcuts may be found on its documentation.
                  – Spidey
                  Feb 18 at 18:38













                up vote
                42
                down vote










                up vote
                42
                down vote









                Most shells that have a command line editing feature
                support Emacs key bindings. (a tiny subset)



                Up     Ctrl+P
                Down Ctrl+N
                Left Ctrl+B
                Right Ctrl+F
                Home Ctrl+A
                End Ctrl+E
                Delete Ctrl+D


                Alternatively, you could set up your shell to use vi command editing mode, by adding set -o vi to your shell startup file (e.g., ~/.bashrc). 
                Then, for example, you can




                • Use EsckEnter
                  to re-execute the previous command (same as !!). 
                  The minus key - also works as a "move up" command.

                • Use EsckkkkkkkkkkkkEnter
                  or Esc12kEnter
                  to re-execute the 12th previous command (same as !-12).

                • Use Esc and a motion command
                  (i.e., k, suitably repeated),
                  and then edit the bash command line you find there. 
                  Remember, you will still be in vi command mode,
                  so you will need to use a vi editing command
                  (e.g., I, A, C, R,
                  or one of their lowercase counterparts)
                  to actually add to the command from history.

                  So, for example, EsckisudoSpaceEnter is equivalent to sudo !!.

                • For advanced users: you can even copy (yank) text from one line
                  and paste (put) it on another,
                  so you can accomplish results comparable to !-2:- !$
                  (Unfortunately, it does not seem to support named buffers.)






                share|improve this answer














                Most shells that have a command line editing feature
                support Emacs key bindings. (a tiny subset)



                Up     Ctrl+P
                Down Ctrl+N
                Left Ctrl+B
                Right Ctrl+F
                Home Ctrl+A
                End Ctrl+E
                Delete Ctrl+D


                Alternatively, you could set up your shell to use vi command editing mode, by adding set -o vi to your shell startup file (e.g., ~/.bashrc). 
                Then, for example, you can




                • Use EsckEnter
                  to re-execute the previous command (same as !!). 
                  The minus key - also works as a "move up" command.

                • Use EsckkkkkkkkkkkkEnter
                  or Esc12kEnter
                  to re-execute the 12th previous command (same as !-12).

                • Use Esc and a motion command
                  (i.e., k, suitably repeated),
                  and then edit the bash command line you find there. 
                  Remember, you will still be in vi command mode,
                  so you will need to use a vi editing command
                  (e.g., I, A, C, R,
                  or one of their lowercase counterparts)
                  to actually add to the command from history.

                  So, for example, EsckisudoSpaceEnter is equivalent to sudo !!.

                • For advanced users: you can even copy (yank) text from one line
                  and paste (put) it on another,
                  so you can accomplish results comparable to !-2:- !$
                  (Unfortunately, it does not seem to support named buffers.)







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 30 at 20:02









                G-Man

                12.3k92961




                12.3k92961










                answered Jul 31 '14 at 23:52









                Gilles

                523k12610461577




                523k12610461577












                • I knew about the emacs relation with the terminal... but the vi trick was great!!!
                  – Dox
                  Aug 6 '14 at 20:06










                • I got for delete Ctl+u in Zsh
                  – Timo
                  Jan 11 at 21:08






                • 1




                  This is implemented by the readline library. Further shortcuts may be found on its documentation.
                  – Spidey
                  Feb 18 at 18:38


















                • I knew about the emacs relation with the terminal... but the vi trick was great!!!
                  – Dox
                  Aug 6 '14 at 20:06










                • I got for delete Ctl+u in Zsh
                  – Timo
                  Jan 11 at 21:08






                • 1




                  This is implemented by the readline library. Further shortcuts may be found on its documentation.
                  – Spidey
                  Feb 18 at 18:38
















                I knew about the emacs relation with the terminal... but the vi trick was great!!!
                – Dox
                Aug 6 '14 at 20:06




                I knew about the emacs relation with the terminal... but the vi trick was great!!!
                – Dox
                Aug 6 '14 at 20:06












                I got for delete Ctl+u in Zsh
                – Timo
                Jan 11 at 21:08




                I got for delete Ctl+u in Zsh
                – Timo
                Jan 11 at 21:08




                1




                1




                This is implemented by the readline library. Further shortcuts may be found on its documentation.
                – Spidey
                Feb 18 at 18:38




                This is implemented by the readline library. Further shortcuts may be found on its documentation.
                – Spidey
                Feb 18 at 18:38










                up vote
                22
                down vote













                My favorite one is CTRL + P then CTRL + O



                This works by default, no extra configuration needed. ^P will let you switch to the last command and ^O will let you execute current line



                Note that CTRL + O can be used for as many times as you want






                share|improve this answer























                • ^O does not do anything on my Mac.
                  – Snowcrash
                  Jul 14 at 22:59












                • @Snowcrash Try ^J
                  – daisy
                  Jul 20 at 5:48










                • that just echoes a blank line even if I've done an ls or pwd previously.
                  – Snowcrash
                  Jul 20 at 8:54










                • @Snowcrash It's ^P then ^J ...
                  – daisy
                  Jul 20 at 9:05










                • Ctrl + P does not work on HP-UX (I don't know which shell it is using). The workaround is to invoke `zsh'.
                  – Peter Mortensen
                  Jul 31 at 11:35

















                up vote
                22
                down vote













                My favorite one is CTRL + P then CTRL + O



                This works by default, no extra configuration needed. ^P will let you switch to the last command and ^O will let you execute current line



                Note that CTRL + O can be used for as many times as you want






                share|improve this answer























                • ^O does not do anything on my Mac.
                  – Snowcrash
                  Jul 14 at 22:59












                • @Snowcrash Try ^J
                  – daisy
                  Jul 20 at 5:48










                • that just echoes a blank line even if I've done an ls or pwd previously.
                  – Snowcrash
                  Jul 20 at 8:54










                • @Snowcrash It's ^P then ^J ...
                  – daisy
                  Jul 20 at 9:05










                • Ctrl + P does not work on HP-UX (I don't know which shell it is using). The workaround is to invoke `zsh'.
                  – Peter Mortensen
                  Jul 31 at 11:35















                up vote
                22
                down vote










                up vote
                22
                down vote









                My favorite one is CTRL + P then CTRL + O



                This works by default, no extra configuration needed. ^P will let you switch to the last command and ^O will let you execute current line



                Note that CTRL + O can be used for as many times as you want






                share|improve this answer














                My favorite one is CTRL + P then CTRL + O



                This works by default, no extra configuration needed. ^P will let you switch to the last command and ^O will let you execute current line



                Note that CTRL + O can be used for as many times as you want







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jul 31 '14 at 6:58

























                answered Jul 31 '14 at 4:11









                daisy

                28.2k48167298




                28.2k48167298












                • ^O does not do anything on my Mac.
                  – Snowcrash
                  Jul 14 at 22:59












                • @Snowcrash Try ^J
                  – daisy
                  Jul 20 at 5:48










                • that just echoes a blank line even if I've done an ls or pwd previously.
                  – Snowcrash
                  Jul 20 at 8:54










                • @Snowcrash It's ^P then ^J ...
                  – daisy
                  Jul 20 at 9:05










                • Ctrl + P does not work on HP-UX (I don't know which shell it is using). The workaround is to invoke `zsh'.
                  – Peter Mortensen
                  Jul 31 at 11:35




















                • ^O does not do anything on my Mac.
                  – Snowcrash
                  Jul 14 at 22:59












                • @Snowcrash Try ^J
                  – daisy
                  Jul 20 at 5:48










                • that just echoes a blank line even if I've done an ls or pwd previously.
                  – Snowcrash
                  Jul 20 at 8:54










                • @Snowcrash It's ^P then ^J ...
                  – daisy
                  Jul 20 at 9:05










                • Ctrl + P does not work on HP-UX (I don't know which shell it is using). The workaround is to invoke `zsh'.
                  – Peter Mortensen
                  Jul 31 at 11:35


















                ^O does not do anything on my Mac.
                – Snowcrash
                Jul 14 at 22:59






                ^O does not do anything on my Mac.
                – Snowcrash
                Jul 14 at 22:59














                @Snowcrash Try ^J
                – daisy
                Jul 20 at 5:48




                @Snowcrash Try ^J
                – daisy
                Jul 20 at 5:48












                that just echoes a blank line even if I've done an ls or pwd previously.
                – Snowcrash
                Jul 20 at 8:54




                that just echoes a blank line even if I've done an ls or pwd previously.
                – Snowcrash
                Jul 20 at 8:54












                @Snowcrash It's ^P then ^J ...
                – daisy
                Jul 20 at 9:05




                @Snowcrash It's ^P then ^J ...
                – daisy
                Jul 20 at 9:05












                Ctrl + P does not work on HP-UX (I don't know which shell it is using). The workaround is to invoke `zsh'.
                – Peter Mortensen
                Jul 31 at 11:35






                Ctrl + P does not work on HP-UX (I don't know which shell it is using). The workaround is to invoke `zsh'.
                – Peter Mortensen
                Jul 31 at 11:35












                up vote
                14
                down vote













                Sure! Since you're used to vi keybindings, why not configure your shell to respond to them? For bash, put this in your ~/.inputrc:



                set editing-mode vi


                Running instances of bash will not re-read the file, so log out and back in.



                zsh will even detect this for you: if none of your startup scripts force the editing mode one way or the other using bindkey and if your $EDITOR environment variable is detected to contain vi, then it will automatically enable vi keybindings. If you need to force it, put this in your ~/.zshrc:



                bindkey -v


                Thereafter, use ESC as usual to enter command line and k and j to move up and down.



                ALSO: The default shell bindings in most shells are the emacs bindings, so actually Crtl-P and Ctrl-N should already work without you having to change anything.






                share|improve this answer

























                  up vote
                  14
                  down vote













                  Sure! Since you're used to vi keybindings, why not configure your shell to respond to them? For bash, put this in your ~/.inputrc:



                  set editing-mode vi


                  Running instances of bash will not re-read the file, so log out and back in.



                  zsh will even detect this for you: if none of your startup scripts force the editing mode one way or the other using bindkey and if your $EDITOR environment variable is detected to contain vi, then it will automatically enable vi keybindings. If you need to force it, put this in your ~/.zshrc:



                  bindkey -v


                  Thereafter, use ESC as usual to enter command line and k and j to move up and down.



                  ALSO: The default shell bindings in most shells are the emacs bindings, so actually Crtl-P and Ctrl-N should already work without you having to change anything.






                  share|improve this answer























                    up vote
                    14
                    down vote










                    up vote
                    14
                    down vote









                    Sure! Since you're used to vi keybindings, why not configure your shell to respond to them? For bash, put this in your ~/.inputrc:



                    set editing-mode vi


                    Running instances of bash will not re-read the file, so log out and back in.



                    zsh will even detect this for you: if none of your startup scripts force the editing mode one way or the other using bindkey and if your $EDITOR environment variable is detected to contain vi, then it will automatically enable vi keybindings. If you need to force it, put this in your ~/.zshrc:



                    bindkey -v


                    Thereafter, use ESC as usual to enter command line and k and j to move up and down.



                    ALSO: The default shell bindings in most shells are the emacs bindings, so actually Crtl-P and Ctrl-N should already work without you having to change anything.






                    share|improve this answer












                    Sure! Since you're used to vi keybindings, why not configure your shell to respond to them? For bash, put this in your ~/.inputrc:



                    set editing-mode vi


                    Running instances of bash will not re-read the file, so log out and back in.



                    zsh will even detect this for you: if none of your startup scripts force the editing mode one way or the other using bindkey and if your $EDITOR environment variable is detected to contain vi, then it will automatically enable vi keybindings. If you need to force it, put this in your ~/.zshrc:



                    bindkey -v


                    Thereafter, use ESC as usual to enter command line and k and j to move up and down.



                    ALSO: The default shell bindings in most shells are the emacs bindings, so actually Crtl-P and Ctrl-N should already work without you having to change anything.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 31 '14 at 3:19









                    Celada

                    30.3k46383




                    30.3k46383






















                        up vote
                        7
                        down vote













                        With any POSIX shell implementing the User Portability option (includes ksh, zsh, bash, yash), you can also use the fc command:



                        fc -e : -1


                        See an example:



                        $ echo "hello"
                        hello

                        $ fc -e : -1
                        echo "hello"
                        hello


                        More info in Execute a range of commands from history's answer by Jonathan Leffler.






                        share|improve this answer



























                          up vote
                          7
                          down vote













                          With any POSIX shell implementing the User Portability option (includes ksh, zsh, bash, yash), you can also use the fc command:



                          fc -e : -1


                          See an example:



                          $ echo "hello"
                          hello

                          $ fc -e : -1
                          echo "hello"
                          hello


                          More info in Execute a range of commands from history's answer by Jonathan Leffler.






                          share|improve this answer

























                            up vote
                            7
                            down vote










                            up vote
                            7
                            down vote









                            With any POSIX shell implementing the User Portability option (includes ksh, zsh, bash, yash), you can also use the fc command:



                            fc -e : -1


                            See an example:



                            $ echo "hello"
                            hello

                            $ fc -e : -1
                            echo "hello"
                            hello


                            More info in Execute a range of commands from history's answer by Jonathan Leffler.






                            share|improve this answer














                            With any POSIX shell implementing the User Portability option (includes ksh, zsh, bash, yash), you can also use the fc command:



                            fc -e : -1


                            See an example:



                            $ echo "hello"
                            hello

                            $ fc -e : -1
                            echo "hello"
                            hello


                            More info in Execute a range of commands from history's answer by Jonathan Leffler.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited May 23 '17 at 12:40









                            Community

                            1




                            1










                            answered Jul 31 '14 at 7:57









                            fedorqui

                            3,98722055




                            3,98722055






















                                up vote
                                5
                                down vote













                                I find I need to redo commands as super user often, so



                                sudo !!


                                redoes the previous command as if I had remembered to type sudo in the first place.






                                share|improve this answer



















                                • 2




                                  I really like this, also looks like a good response to "authorization denied". "I meant sudo ffs!"
                                  – Viktor Mellgren
                                  Aug 1 '14 at 11:20















                                up vote
                                5
                                down vote













                                I find I need to redo commands as super user often, so



                                sudo !!


                                redoes the previous command as if I had remembered to type sudo in the first place.






                                share|improve this answer



















                                • 2




                                  I really like this, also looks like a good response to "authorization denied". "I meant sudo ffs!"
                                  – Viktor Mellgren
                                  Aug 1 '14 at 11:20













                                up vote
                                5
                                down vote










                                up vote
                                5
                                down vote









                                I find I need to redo commands as super user often, so



                                sudo !!


                                redoes the previous command as if I had remembered to type sudo in the first place.






                                share|improve this answer














                                I find I need to redo commands as super user often, so



                                sudo !!


                                redoes the previous command as if I had remembered to type sudo in the first place.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Jul 31 '14 at 21:35









                                polym

                                6,51643157




                                6,51643157










                                answered Jul 31 '14 at 21:16









                                Jared Chmielecki

                                1513




                                1513








                                • 2




                                  I really like this, also looks like a good response to "authorization denied". "I meant sudo ffs!"
                                  – Viktor Mellgren
                                  Aug 1 '14 at 11:20














                                • 2




                                  I really like this, also looks like a good response to "authorization denied". "I meant sudo ffs!"
                                  – Viktor Mellgren
                                  Aug 1 '14 at 11:20








                                2




                                2




                                I really like this, also looks like a good response to "authorization denied". "I meant sudo ffs!"
                                – Viktor Mellgren
                                Aug 1 '14 at 11:20




                                I really like this, also looks like a good response to "authorization denied". "I meant sudo ffs!"
                                – Viktor Mellgren
                                Aug 1 '14 at 11:20










                                up vote
                                4
                                down vote













                                For all you Mac iterm2 users:



                                You can bind +R to 0x0C 0x10 0x0d. This will clear the terminal and run the last command.



                                iterm 2 key preferences






                                share|improve this answer























                                • Is there any way to repeat the last command without clearing the terminal?
                                  – Luke Davis
                                  Jan 16 at 1:41










                                • Ah, nevermind: for anyone reading this, to repeat without clearing the terminal, use 0x10 0x0d (i.e. omit the 0x0C).
                                  – Luke Davis
                                  Jan 16 at 1:42















                                up vote
                                4
                                down vote













                                For all you Mac iterm2 users:



                                You can bind +R to 0x0C 0x10 0x0d. This will clear the terminal and run the last command.



                                iterm 2 key preferences






                                share|improve this answer























                                • Is there any way to repeat the last command without clearing the terminal?
                                  – Luke Davis
                                  Jan 16 at 1:41










                                • Ah, nevermind: for anyone reading this, to repeat without clearing the terminal, use 0x10 0x0d (i.e. omit the 0x0C).
                                  – Luke Davis
                                  Jan 16 at 1:42













                                up vote
                                4
                                down vote










                                up vote
                                4
                                down vote









                                For all you Mac iterm2 users:



                                You can bind +R to 0x0C 0x10 0x0d. This will clear the terminal and run the last command.



                                iterm 2 key preferences






                                share|improve this answer














                                For all you Mac iterm2 users:



                                You can bind +R to 0x0C 0x10 0x0d. This will clear the terminal and run the last command.



                                iterm 2 key preferences







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Aug 5 '14 at 22:58









                                HalosGhost

                                3,67592135




                                3,67592135










                                answered Aug 5 '14 at 22:43









                                Brian Wigginton

                                1414




                                1414












                                • Is there any way to repeat the last command without clearing the terminal?
                                  – Luke Davis
                                  Jan 16 at 1:41










                                • Ah, nevermind: for anyone reading this, to repeat without clearing the terminal, use 0x10 0x0d (i.e. omit the 0x0C).
                                  – Luke Davis
                                  Jan 16 at 1:42


















                                • Is there any way to repeat the last command without clearing the terminal?
                                  – Luke Davis
                                  Jan 16 at 1:41










                                • Ah, nevermind: for anyone reading this, to repeat without clearing the terminal, use 0x10 0x0d (i.e. omit the 0x0C).
                                  – Luke Davis
                                  Jan 16 at 1:42
















                                Is there any way to repeat the last command without clearing the terminal?
                                – Luke Davis
                                Jan 16 at 1:41




                                Is there any way to repeat the last command without clearing the terminal?
                                – Luke Davis
                                Jan 16 at 1:41












                                Ah, nevermind: for anyone reading this, to repeat without clearing the terminal, use 0x10 0x0d (i.e. omit the 0x0C).
                                – Luke Davis
                                Jan 16 at 1:42




                                Ah, nevermind: for anyone reading this, to repeat without clearing the terminal, use 0x10 0x0d (i.e. omit the 0x0C).
                                – Luke Davis
                                Jan 16 at 1:42










                                up vote
                                2
                                down vote













                                With csh or any shell implementing csh-like history substitution (tcsh, bash, zsh), you can also use the !<beginning of command> to call the last command beginning with <beginning of command>.



                                for example if you ran



                                $ tail file.txt
                                $ less otherfile.txt
                                $ !ta


                                !ta would execute tail file.txt






                                share|improve this answer



























                                  up vote
                                  2
                                  down vote













                                  With csh or any shell implementing csh-like history substitution (tcsh, bash, zsh), you can also use the !<beginning of command> to call the last command beginning with <beginning of command>.



                                  for example if you ran



                                  $ tail file.txt
                                  $ less otherfile.txt
                                  $ !ta


                                  !ta would execute tail file.txt






                                  share|improve this answer

























                                    up vote
                                    2
                                    down vote










                                    up vote
                                    2
                                    down vote









                                    With csh or any shell implementing csh-like history substitution (tcsh, bash, zsh), you can also use the !<beginning of command> to call the last command beginning with <beginning of command>.



                                    for example if you ran



                                    $ tail file.txt
                                    $ less otherfile.txt
                                    $ !ta


                                    !ta would execute tail file.txt






                                    share|improve this answer














                                    With csh or any shell implementing csh-like history substitution (tcsh, bash, zsh), you can also use the !<beginning of command> to call the last command beginning with <beginning of command>.



                                    for example if you ran



                                    $ tail file.txt
                                    $ less otherfile.txt
                                    $ !ta


                                    !ta would execute tail file.txt







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Jul 31 '14 at 20:44









                                    Stéphane Chazelas

                                    296k54559903




                                    296k54559903










                                    answered Jul 31 '14 at 19:09









                                    dan08

                                    1213




                                    1213






















                                        up vote
                                        1
                                        down vote













                                        The ksh shell has a default alias r that repeats the most recent command. It is bound to fc -s:



                                        alias r='fc -s'


                                        Interestingly, the bash documentation for the fc builtin (help fc, and also in the manual itself) even mentions this, but it's not a default alias in that shell:




                                        A useful alias to use with this is r='fc -s', so that typing r cc
                                        runs the last command beginning with cc and typing r re-executes
                                        the last command.




                                        In bash, this would also work even if you have turned history expansions off with set +H.



                                        In the zsh shell, there is a r builtin that is documented to be the same as fc -e -.



                                        The fc utility is a POSIX standard utility.






                                        share|improve this answer



























                                          up vote
                                          1
                                          down vote













                                          The ksh shell has a default alias r that repeats the most recent command. It is bound to fc -s:



                                          alias r='fc -s'


                                          Interestingly, the bash documentation for the fc builtin (help fc, and also in the manual itself) even mentions this, but it's not a default alias in that shell:




                                          A useful alias to use with this is r='fc -s', so that typing r cc
                                          runs the last command beginning with cc and typing r re-executes
                                          the last command.




                                          In bash, this would also work even if you have turned history expansions off with set +H.



                                          In the zsh shell, there is a r builtin that is documented to be the same as fc -e -.



                                          The fc utility is a POSIX standard utility.






                                          share|improve this answer

























                                            up vote
                                            1
                                            down vote










                                            up vote
                                            1
                                            down vote









                                            The ksh shell has a default alias r that repeats the most recent command. It is bound to fc -s:



                                            alias r='fc -s'


                                            Interestingly, the bash documentation for the fc builtin (help fc, and also in the manual itself) even mentions this, but it's not a default alias in that shell:




                                            A useful alias to use with this is r='fc -s', so that typing r cc
                                            runs the last command beginning with cc and typing r re-executes
                                            the last command.




                                            In bash, this would also work even if you have turned history expansions off with set +H.



                                            In the zsh shell, there is a r builtin that is documented to be the same as fc -e -.



                                            The fc utility is a POSIX standard utility.






                                            share|improve this answer














                                            The ksh shell has a default alias r that repeats the most recent command. It is bound to fc -s:



                                            alias r='fc -s'


                                            Interestingly, the bash documentation for the fc builtin (help fc, and also in the manual itself) even mentions this, but it's not a default alias in that shell:




                                            A useful alias to use with this is r='fc -s', so that typing r cc
                                            runs the last command beginning with cc and typing r re-executes
                                            the last command.




                                            In bash, this would also work even if you have turned history expansions off with set +H.



                                            In the zsh shell, there is a r builtin that is documented to be the same as fc -e -.



                                            The fc utility is a POSIX standard utility.







                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Nov 30 at 19:32

























                                            answered Nov 30 at 19:26









                                            Kusalananda

                                            118k16223364




                                            118k16223364






















                                                up vote
                                                0
                                                down vote













                                                When I am using a good xterm emulator, e.g. putty, my favorite way to proceed is to select the command including line feed, copy it to clipboard and then use right click on the mouse to paste it. This repeats the command in a single click.
                                                This also works fine for a sequence of commands separated by semi-colons.






                                                share|improve this answer

























                                                  up vote
                                                  0
                                                  down vote













                                                  When I am using a good xterm emulator, e.g. putty, my favorite way to proceed is to select the command including line feed, copy it to clipboard and then use right click on the mouse to paste it. This repeats the command in a single click.
                                                  This also works fine for a sequence of commands separated by semi-colons.






                                                  share|improve this answer























                                                    up vote
                                                    0
                                                    down vote










                                                    up vote
                                                    0
                                                    down vote









                                                    When I am using a good xterm emulator, e.g. putty, my favorite way to proceed is to select the command including line feed, copy it to clipboard and then use right click on the mouse to paste it. This repeats the command in a single click.
                                                    This also works fine for a sequence of commands separated by semi-colons.






                                                    share|improve this answer












                                                    When I am using a good xterm emulator, e.g. putty, my favorite way to proceed is to select the command including line feed, copy it to clipboard and then use right click on the mouse to paste it. This repeats the command in a single click.
                                                    This also works fine for a sequence of commands separated by semi-colons.







                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Dec 12 '16 at 15:52









                                                    Bernard Perrin

                                                    91




                                                    91






















                                                        up vote
                                                        0
                                                        down vote













                                                        tl;dr !! just appends the last command to your current command. <backtick>!!<backtick> executes the last command and appends the output to your current command.



                                                        There are actually 2 variants:




                                                        1. The last command itself

                                                        2. The result of the last command


                                                        Let me show you 2 examples:



                                                        Example 1



                                                        $ mkdir /test
                                                        mkdir: /test: Permission denied

                                                        $ sudo !!
                                                        sudo mkdir /test
                                                        Password:


                                                        As you see above, I'm executing a command and essentially just appending the last command.



                                                        Another variation



                                                        Example 2



                                                        $ find ~/Documents "test.txt"
                                                        /Users/<user>/Documents/test.txt

                                                        $ vi `!!`
                                                        # Opens test.txt. But if you do
                                                        $ vi !!
                                                        vi find ~/Documents -name "test.txt"
                                                        VIM - Vi IMproved 8.1 (2018 May 18, compiled Oct 29 2018 06:55:58)
                                                        Unknown option argument: "-name"
                                                        More info with: "vim -h"





                                                        share|improve this answer








                                                        New contributor




                                                        Pradyumna Shembekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                        Check out our Code of Conduct.






















                                                          up vote
                                                          0
                                                          down vote













                                                          tl;dr !! just appends the last command to your current command. <backtick>!!<backtick> executes the last command and appends the output to your current command.



                                                          There are actually 2 variants:




                                                          1. The last command itself

                                                          2. The result of the last command


                                                          Let me show you 2 examples:



                                                          Example 1



                                                          $ mkdir /test
                                                          mkdir: /test: Permission denied

                                                          $ sudo !!
                                                          sudo mkdir /test
                                                          Password:


                                                          As you see above, I'm executing a command and essentially just appending the last command.



                                                          Another variation



                                                          Example 2



                                                          $ find ~/Documents "test.txt"
                                                          /Users/<user>/Documents/test.txt

                                                          $ vi `!!`
                                                          # Opens test.txt. But if you do
                                                          $ vi !!
                                                          vi find ~/Documents -name "test.txt"
                                                          VIM - Vi IMproved 8.1 (2018 May 18, compiled Oct 29 2018 06:55:58)
                                                          Unknown option argument: "-name"
                                                          More info with: "vim -h"





                                                          share|improve this answer








                                                          New contributor




                                                          Pradyumna Shembekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                          Check out our Code of Conduct.




















                                                            up vote
                                                            0
                                                            down vote










                                                            up vote
                                                            0
                                                            down vote









                                                            tl;dr !! just appends the last command to your current command. <backtick>!!<backtick> executes the last command and appends the output to your current command.



                                                            There are actually 2 variants:




                                                            1. The last command itself

                                                            2. The result of the last command


                                                            Let me show you 2 examples:



                                                            Example 1



                                                            $ mkdir /test
                                                            mkdir: /test: Permission denied

                                                            $ sudo !!
                                                            sudo mkdir /test
                                                            Password:


                                                            As you see above, I'm executing a command and essentially just appending the last command.



                                                            Another variation



                                                            Example 2



                                                            $ find ~/Documents "test.txt"
                                                            /Users/<user>/Documents/test.txt

                                                            $ vi `!!`
                                                            # Opens test.txt. But if you do
                                                            $ vi !!
                                                            vi find ~/Documents -name "test.txt"
                                                            VIM - Vi IMproved 8.1 (2018 May 18, compiled Oct 29 2018 06:55:58)
                                                            Unknown option argument: "-name"
                                                            More info with: "vim -h"





                                                            share|improve this answer








                                                            New contributor




                                                            Pradyumna Shembekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                            Check out our Code of Conduct.









                                                            tl;dr !! just appends the last command to your current command. <backtick>!!<backtick> executes the last command and appends the output to your current command.



                                                            There are actually 2 variants:




                                                            1. The last command itself

                                                            2. The result of the last command


                                                            Let me show you 2 examples:



                                                            Example 1



                                                            $ mkdir /test
                                                            mkdir: /test: Permission denied

                                                            $ sudo !!
                                                            sudo mkdir /test
                                                            Password:


                                                            As you see above, I'm executing a command and essentially just appending the last command.



                                                            Another variation



                                                            Example 2



                                                            $ find ~/Documents "test.txt"
                                                            /Users/<user>/Documents/test.txt

                                                            $ vi `!!`
                                                            # Opens test.txt. But if you do
                                                            $ vi !!
                                                            vi find ~/Documents -name "test.txt"
                                                            VIM - Vi IMproved 8.1 (2018 May 18, compiled Oct 29 2018 06:55:58)
                                                            Unknown option argument: "-name"
                                                            More info with: "vim -h"






                                                            share|improve this answer








                                                            New contributor




                                                            Pradyumna Shembekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                            Check out our Code of Conduct.









                                                            share|improve this answer



                                                            share|improve this answer






                                                            New contributor




                                                            Pradyumna Shembekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                            Check out our Code of Conduct.









                                                            answered Nov 30 at 18:33









                                                            Pradyumna Shembekar

                                                            1




                                                            1




                                                            New contributor




                                                            Pradyumna Shembekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                            Check out our Code of Conduct.





                                                            New contributor





                                                            Pradyumna Shembekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                            Check out our Code of Conduct.






                                                            Pradyumna Shembekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                            Check out our Code of Conduct.






















                                                                up vote
                                                                0
                                                                down vote














                                                                • Type History and note the number in front of the command that you to
                                                                  execute and use !number


                                                                • You can also use !-1 (1 being the last command you execute, replace
                                                                  it with the count number that you get when you do it from below
                                                                  keeping the last executed as 1 )



                                                                • !-2 (second last and so on)






                                                                share|improve this answer










                                                                New contributor




                                                                Lovish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                Check out our Code of Conduct.






















                                                                  up vote
                                                                  0
                                                                  down vote














                                                                  • Type History and note the number in front of the command that you to
                                                                    execute and use !number


                                                                  • You can also use !-1 (1 being the last command you execute, replace
                                                                    it with the count number that you get when you do it from below
                                                                    keeping the last executed as 1 )



                                                                  • !-2 (second last and so on)






                                                                  share|improve this answer










                                                                  New contributor




                                                                  Lovish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.




















                                                                    up vote
                                                                    0
                                                                    down vote










                                                                    up vote
                                                                    0
                                                                    down vote










                                                                    • Type History and note the number in front of the command that you to
                                                                      execute and use !number


                                                                    • You can also use !-1 (1 being the last command you execute, replace
                                                                      it with the count number that you get when you do it from below
                                                                      keeping the last executed as 1 )



                                                                    • !-2 (second last and so on)






                                                                    share|improve this answer










                                                                    New contributor




                                                                    Lovish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                    Check out our Code of Conduct.










                                                                    • Type History and note the number in front of the command that you to
                                                                      execute and use !number


                                                                    • You can also use !-1 (1 being the last command you execute, replace
                                                                      it with the count number that you get when you do it from below
                                                                      keeping the last executed as 1 )



                                                                    • !-2 (second last and so on)







                                                                    share|improve this answer










                                                                    New contributor




                                                                    Lovish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                    Check out our Code of Conduct.









                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited Nov 30 at 21:15





















                                                                    New contributor




                                                                    Lovish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                    Check out our Code of Conduct.









                                                                    answered Nov 30 at 20:36









                                                                    Lovish

                                                                    12




                                                                    12




                                                                    New contributor




                                                                    Lovish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                    Check out our Code of Conduct.





                                                                    New contributor





                                                                    Lovish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                    Check out our Code of Conduct.






                                                                    Lovish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                    Check out our Code of Conduct.






















                                                                        up vote
                                                                        -1
                                                                        down vote













                                                                        you can use !n to execute nth command in terminal.
                                                                        where 'n' is line no of history command.






                                                                        share|improve this answer























                                                                        • Which shell expands !? to the last command? Are you sure you're not mixing it up with !! (which has already been covered in other answers)?
                                                                          – Anthony Geoghegan
                                                                          Feb 18 at 16:53










                                                                        • sorry, actually i was experimenting this command at that time. please check edited answer. @AnthonyGeoghegan
                                                                          – Ravi Sevta
                                                                          Feb 18 at 17:45

















                                                                        up vote
                                                                        -1
                                                                        down vote













                                                                        you can use !n to execute nth command in terminal.
                                                                        where 'n' is line no of history command.






                                                                        share|improve this answer























                                                                        • Which shell expands !? to the last command? Are you sure you're not mixing it up with !! (which has already been covered in other answers)?
                                                                          – Anthony Geoghegan
                                                                          Feb 18 at 16:53










                                                                        • sorry, actually i was experimenting this command at that time. please check edited answer. @AnthonyGeoghegan
                                                                          – Ravi Sevta
                                                                          Feb 18 at 17:45















                                                                        up vote
                                                                        -1
                                                                        down vote










                                                                        up vote
                                                                        -1
                                                                        down vote









                                                                        you can use !n to execute nth command in terminal.
                                                                        where 'n' is line no of history command.






                                                                        share|improve this answer














                                                                        you can use !n to execute nth command in terminal.
                                                                        where 'n' is line no of history command.







                                                                        share|improve this answer














                                                                        share|improve this answer



                                                                        share|improve this answer








                                                                        edited Feb 18 at 17:42

























                                                                        answered Feb 18 at 16:24









                                                                        Ravi Sevta

                                                                        17718




                                                                        17718












                                                                        • Which shell expands !? to the last command? Are you sure you're not mixing it up with !! (which has already been covered in other answers)?
                                                                          – Anthony Geoghegan
                                                                          Feb 18 at 16:53










                                                                        • sorry, actually i was experimenting this command at that time. please check edited answer. @AnthonyGeoghegan
                                                                          – Ravi Sevta
                                                                          Feb 18 at 17:45




















                                                                        • Which shell expands !? to the last command? Are you sure you're not mixing it up with !! (which has already been covered in other answers)?
                                                                          – Anthony Geoghegan
                                                                          Feb 18 at 16:53










                                                                        • sorry, actually i was experimenting this command at that time. please check edited answer. @AnthonyGeoghegan
                                                                          – Ravi Sevta
                                                                          Feb 18 at 17:45


















                                                                        Which shell expands !? to the last command? Are you sure you're not mixing it up with !! (which has already been covered in other answers)?
                                                                        – Anthony Geoghegan
                                                                        Feb 18 at 16:53




                                                                        Which shell expands !? to the last command? Are you sure you're not mixing it up with !! (which has already been covered in other answers)?
                                                                        – Anthony Geoghegan
                                                                        Feb 18 at 16:53












                                                                        sorry, actually i was experimenting this command at that time. please check edited answer. @AnthonyGeoghegan
                                                                        – Ravi Sevta
                                                                        Feb 18 at 17:45






                                                                        sorry, actually i was experimenting this command at that time. please check edited answer. @AnthonyGeoghegan
                                                                        – Ravi Sevta
                                                                        Feb 18 at 17:45




















                                                                        draft saved

                                                                        draft discarded




















































                                                                        Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                                        • Please be sure to answer the question. Provide details and share your research!

                                                                        But avoid



                                                                        • Asking for help, clarification, or responding to other answers.

                                                                        • Making statements based on opinion; back them up with references or personal experience.


                                                                        To learn more, see our tips on writing great answers.





                                                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                                        Please pay close attention to the following guidance:


                                                                        • Please be sure to answer the question. Provide details and share your research!

                                                                        But avoid



                                                                        • Asking for help, clarification, or responding to other answers.

                                                                        • Making statements based on opinion; back them up with references or personal experience.


                                                                        To learn more, see our tips on writing great answers.




                                                                        draft saved


                                                                        draft discarded














                                                                        StackExchange.ready(
                                                                        function () {
                                                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f147563%2fhow-do-i-repeat-the-last-command-without-using-the-arrow-keys%23new-answer', 'question_page');
                                                                        }
                                                                        );

                                                                        Post as a guest















                                                                        Required, but never shown





















































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown

































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown







                                                                        Popular posts from this blog

                                                                        Accessing regular linux commands in Huawei's Dopra Linux

                                                                        Can't connect RFCOMM socket: Host is down

                                                                        Kernel panic - not syncing: Fatal Exception in Interrupt