Is there a shortcut for repeat the second proximate command in bash?

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











up vote
7
down vote

favorite












We all know that !! can repeat the last command you do in bash.



But sometimes we need to do some operation like



$ python test.py
$ vim test.py

$ python test.py # here is where I need to repeat the second proximate bash command


I can use up-arrow key to do that, but that requires me to move my right hand away to an uncomfortable position. So I'm wondering if there is a command which like !! would work?










share|improve this question























  • Use Ctrl+P in emacs mode (default), or k in vi-mode if you don't want to reach for the up key.
    – Stéphane Chazelas
    Aug 20 '14 at 9:48










  • I’d actually write both commands in one line like vim test.py && python test.py to avoid fiddling with multiple commands in history, but you can simply bind "ep": previous-history in your ~/.inputrc so you could press Alt+p instead of up key.
    – tijagi
    Aug 20 '14 at 13:00














up vote
7
down vote

favorite












We all know that !! can repeat the last command you do in bash.



But sometimes we need to do some operation like



$ python test.py
$ vim test.py

$ python test.py # here is where I need to repeat the second proximate bash command


I can use up-arrow key to do that, but that requires me to move my right hand away to an uncomfortable position. So I'm wondering if there is a command which like !! would work?










share|improve this question























  • Use Ctrl+P in emacs mode (default), or k in vi-mode if you don't want to reach for the up key.
    – Stéphane Chazelas
    Aug 20 '14 at 9:48










  • I’d actually write both commands in one line like vim test.py && python test.py to avoid fiddling with multiple commands in history, but you can simply bind "ep": previous-history in your ~/.inputrc so you could press Alt+p instead of up key.
    – tijagi
    Aug 20 '14 at 13:00












up vote
7
down vote

favorite









up vote
7
down vote

favorite











We all know that !! can repeat the last command you do in bash.



But sometimes we need to do some operation like



$ python test.py
$ vim test.py

$ python test.py # here is where I need to repeat the second proximate bash command


I can use up-arrow key to do that, but that requires me to move my right hand away to an uncomfortable position. So I'm wondering if there is a command which like !! would work?










share|improve this question















We all know that !! can repeat the last command you do in bash.



But sometimes we need to do some operation like



$ python test.py
$ vim test.py

$ python test.py # here is where I need to repeat the second proximate bash command


I can use up-arrow key to do that, but that requires me to move my right hand away to an uncomfortable position. So I'm wondering if there is a command which like !! would work?







bash command-line command-history






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:28









Rui F Ribeiro

38.2k1475125




38.2k1475125










asked Aug 20 '14 at 8:29









Zen

2,21793054




2,21793054











  • Use Ctrl+P in emacs mode (default), or k in vi-mode if you don't want to reach for the up key.
    – Stéphane Chazelas
    Aug 20 '14 at 9:48










  • I’d actually write both commands in one line like vim test.py && python test.py to avoid fiddling with multiple commands in history, but you can simply bind "ep": previous-history in your ~/.inputrc so you could press Alt+p instead of up key.
    – tijagi
    Aug 20 '14 at 13:00
















  • Use Ctrl+P in emacs mode (default), or k in vi-mode if you don't want to reach for the up key.
    – Stéphane Chazelas
    Aug 20 '14 at 9:48










  • I’d actually write both commands in one line like vim test.py && python test.py to avoid fiddling with multiple commands in history, but you can simply bind "ep": previous-history in your ~/.inputrc so you could press Alt+p instead of up key.
    – tijagi
    Aug 20 '14 at 13:00















Use Ctrl+P in emacs mode (default), or k in vi-mode if you don't want to reach for the up key.
– Stéphane Chazelas
Aug 20 '14 at 9:48




Use Ctrl+P in emacs mode (default), or k in vi-mode if you don't want to reach for the up key.
– Stéphane Chazelas
Aug 20 '14 at 9:48












I’d actually write both commands in one line like vim test.py && python test.py to avoid fiddling with multiple commands in history, but you can simply bind "ep": previous-history in your ~/.inputrc so you could press Alt+p instead of up key.
– tijagi
Aug 20 '14 at 13:00




I’d actually write both commands in one line like vim test.py && python test.py to avoid fiddling with multiple commands in history, but you can simply bind "ep": previous-history in your ~/.inputrc so you could press Alt+p instead of up key.
– tijagi
Aug 20 '14 at 13:00










2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










You can use !-2:



$ echo foo
foo
$ echo bar
bar
$ !-2
echo foo
foo


That may not help with your right-hand situation.



You can also use !string history searching for this sort of case:



$ python test.py
$ vim test.py
$ !py
python test.py # Printed, then run


This may be more convenient to use. It will run:




the most recent command preceding the current position in the history list starting with string.




Even just !p would work. You can use !?string to search the whole command line, rather than just the start.






share|improve this answer




















  • It's much better! since arrow keys needs to move your entire wrist, and !-2 only requires finger movements, excellent, Michael. Wow, just read the second part, it's out of my expectation. You are a magician!
    – Zen
    Aug 20 '14 at 8:38


















up vote
4
down vote













!-2


More fun is available. Say you want to keep operating on a file (as above, where you're using test.py, repeatedly):



cp foo.py thing.py
edit $_
python $_
!-2
^thing^foo


  • Copy an existing file to thing.py

  • Edit (vim, emacs - though why you'd be using a command line if you were running Emacs-OS, I have no idea) thing.py - the last word in the previous command line

  • python thing.py

  • Edit thing.py

  • Edit foo.py

History manipulation is such fun. Try man history. Note that the quest to re-use history can result in typing far more than the command itself would. The final command substitution, where I replace thing.py by foo.py is one such example. Fewer characters to just type in the command and name. :)



The history substitution is also why you get strange messages when you try:



$ echo "This is a disaster!"
-bash: !": event not found


The exclamation mark is being consumed as a history reference, and it can't find a previous command with a double quote.






share|improve this answer






















  • By the way, If I got echo 'xxx', how do I substitute each 'x' into 'y'?
    – Zen
    Aug 20 '14 at 9:32










  • Substituting a string for another string? Just that? !!:1gs/x/y/
    – JezC
    Aug 20 '14 at 9:44











  • Though personally, I'd be looking at $(echo !! | sed -e s/x/y/g)
    – JezC
    Aug 20 '14 at 9:51










  • using sed would be too clumsy, can I add some parameter or symbol in ^x^y to do that job?
    – Zen
    Aug 20 '14 at 9:58











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%2f151100%2fis-there-a-shortcut-for-repeat-the-second-proximate-command-in-bash%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










You can use !-2:



$ echo foo
foo
$ echo bar
bar
$ !-2
echo foo
foo


That may not help with your right-hand situation.



You can also use !string history searching for this sort of case:



$ python test.py
$ vim test.py
$ !py
python test.py # Printed, then run


This may be more convenient to use. It will run:




the most recent command preceding the current position in the history list starting with string.




Even just !p would work. You can use !?string to search the whole command line, rather than just the start.






share|improve this answer




















  • It's much better! since arrow keys needs to move your entire wrist, and !-2 only requires finger movements, excellent, Michael. Wow, just read the second part, it's out of my expectation. You are a magician!
    – Zen
    Aug 20 '14 at 8:38















up vote
4
down vote



accepted










You can use !-2:



$ echo foo
foo
$ echo bar
bar
$ !-2
echo foo
foo


That may not help with your right-hand situation.



You can also use !string history searching for this sort of case:



$ python test.py
$ vim test.py
$ !py
python test.py # Printed, then run


This may be more convenient to use. It will run:




the most recent command preceding the current position in the history list starting with string.




Even just !p would work. You can use !?string to search the whole command line, rather than just the start.






share|improve this answer




















  • It's much better! since arrow keys needs to move your entire wrist, and !-2 only requires finger movements, excellent, Michael. Wow, just read the second part, it's out of my expectation. You are a magician!
    – Zen
    Aug 20 '14 at 8:38













up vote
4
down vote



accepted







up vote
4
down vote



accepted






You can use !-2:



$ echo foo
foo
$ echo bar
bar
$ !-2
echo foo
foo


That may not help with your right-hand situation.



You can also use !string history searching for this sort of case:



$ python test.py
$ vim test.py
$ !py
python test.py # Printed, then run


This may be more convenient to use. It will run:




the most recent command preceding the current position in the history list starting with string.




Even just !p would work. You can use !?string to search the whole command line, rather than just the start.






share|improve this answer












You can use !-2:



$ echo foo
foo
$ echo bar
bar
$ !-2
echo foo
foo


That may not help with your right-hand situation.



You can also use !string history searching for this sort of case:



$ python test.py
$ vim test.py
$ !py
python test.py # Printed, then run


This may be more convenient to use. It will run:




the most recent command preceding the current position in the history list starting with string.




Even just !p would work. You can use !?string to search the whole command line, rather than just the start.







share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 20 '14 at 8:36









Michael Homer

44.8k7117156




44.8k7117156











  • It's much better! since arrow keys needs to move your entire wrist, and !-2 only requires finger movements, excellent, Michael. Wow, just read the second part, it's out of my expectation. You are a magician!
    – Zen
    Aug 20 '14 at 8:38

















  • It's much better! since arrow keys needs to move your entire wrist, and !-2 only requires finger movements, excellent, Michael. Wow, just read the second part, it's out of my expectation. You are a magician!
    – Zen
    Aug 20 '14 at 8:38
















It's much better! since arrow keys needs to move your entire wrist, and !-2 only requires finger movements, excellent, Michael. Wow, just read the second part, it's out of my expectation. You are a magician!
– Zen
Aug 20 '14 at 8:38





It's much better! since arrow keys needs to move your entire wrist, and !-2 only requires finger movements, excellent, Michael. Wow, just read the second part, it's out of my expectation. You are a magician!
– Zen
Aug 20 '14 at 8:38













up vote
4
down vote













!-2


More fun is available. Say you want to keep operating on a file (as above, where you're using test.py, repeatedly):



cp foo.py thing.py
edit $_
python $_
!-2
^thing^foo


  • Copy an existing file to thing.py

  • Edit (vim, emacs - though why you'd be using a command line if you were running Emacs-OS, I have no idea) thing.py - the last word in the previous command line

  • python thing.py

  • Edit thing.py

  • Edit foo.py

History manipulation is such fun. Try man history. Note that the quest to re-use history can result in typing far more than the command itself would. The final command substitution, where I replace thing.py by foo.py is one such example. Fewer characters to just type in the command and name. :)



The history substitution is also why you get strange messages when you try:



$ echo "This is a disaster!"
-bash: !": event not found


The exclamation mark is being consumed as a history reference, and it can't find a previous command with a double quote.






share|improve this answer






















  • By the way, If I got echo 'xxx', how do I substitute each 'x' into 'y'?
    – Zen
    Aug 20 '14 at 9:32










  • Substituting a string for another string? Just that? !!:1gs/x/y/
    – JezC
    Aug 20 '14 at 9:44











  • Though personally, I'd be looking at $(echo !! | sed -e s/x/y/g)
    – JezC
    Aug 20 '14 at 9:51










  • using sed would be too clumsy, can I add some parameter or symbol in ^x^y to do that job?
    – Zen
    Aug 20 '14 at 9:58















up vote
4
down vote













!-2


More fun is available. Say you want to keep operating on a file (as above, where you're using test.py, repeatedly):



cp foo.py thing.py
edit $_
python $_
!-2
^thing^foo


  • Copy an existing file to thing.py

  • Edit (vim, emacs - though why you'd be using a command line if you were running Emacs-OS, I have no idea) thing.py - the last word in the previous command line

  • python thing.py

  • Edit thing.py

  • Edit foo.py

History manipulation is such fun. Try man history. Note that the quest to re-use history can result in typing far more than the command itself would. The final command substitution, where I replace thing.py by foo.py is one such example. Fewer characters to just type in the command and name. :)



The history substitution is also why you get strange messages when you try:



$ echo "This is a disaster!"
-bash: !": event not found


The exclamation mark is being consumed as a history reference, and it can't find a previous command with a double quote.






share|improve this answer






















  • By the way, If I got echo 'xxx', how do I substitute each 'x' into 'y'?
    – Zen
    Aug 20 '14 at 9:32










  • Substituting a string for another string? Just that? !!:1gs/x/y/
    – JezC
    Aug 20 '14 at 9:44











  • Though personally, I'd be looking at $(echo !! | sed -e s/x/y/g)
    – JezC
    Aug 20 '14 at 9:51










  • using sed would be too clumsy, can I add some parameter or symbol in ^x^y to do that job?
    – Zen
    Aug 20 '14 at 9:58













up vote
4
down vote










up vote
4
down vote









!-2


More fun is available. Say you want to keep operating on a file (as above, where you're using test.py, repeatedly):



cp foo.py thing.py
edit $_
python $_
!-2
^thing^foo


  • Copy an existing file to thing.py

  • Edit (vim, emacs - though why you'd be using a command line if you were running Emacs-OS, I have no idea) thing.py - the last word in the previous command line

  • python thing.py

  • Edit thing.py

  • Edit foo.py

History manipulation is such fun. Try man history. Note that the quest to re-use history can result in typing far more than the command itself would. The final command substitution, where I replace thing.py by foo.py is one such example. Fewer characters to just type in the command and name. :)



The history substitution is also why you get strange messages when you try:



$ echo "This is a disaster!"
-bash: !": event not found


The exclamation mark is being consumed as a history reference, and it can't find a previous command with a double quote.






share|improve this answer














!-2


More fun is available. Say you want to keep operating on a file (as above, where you're using test.py, repeatedly):



cp foo.py thing.py
edit $_
python $_
!-2
^thing^foo


  • Copy an existing file to thing.py

  • Edit (vim, emacs - though why you'd be using a command line if you were running Emacs-OS, I have no idea) thing.py - the last word in the previous command line

  • python thing.py

  • Edit thing.py

  • Edit foo.py

History manipulation is such fun. Try man history. Note that the quest to re-use history can result in typing far more than the command itself would. The final command substitution, where I replace thing.py by foo.py is one such example. Fewer characters to just type in the command and name. :)



The history substitution is also why you get strange messages when you try:



$ echo "This is a disaster!"
-bash: !": event not found


The exclamation mark is being consumed as a history reference, and it can't find a previous command with a double quote.







share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 20 '14 at 12:32

























answered Aug 20 '14 at 9:24









JezC

20614




20614











  • By the way, If I got echo 'xxx', how do I substitute each 'x' into 'y'?
    – Zen
    Aug 20 '14 at 9:32










  • Substituting a string for another string? Just that? !!:1gs/x/y/
    – JezC
    Aug 20 '14 at 9:44











  • Though personally, I'd be looking at $(echo !! | sed -e s/x/y/g)
    – JezC
    Aug 20 '14 at 9:51










  • using sed would be too clumsy, can I add some parameter or symbol in ^x^y to do that job?
    – Zen
    Aug 20 '14 at 9:58

















  • By the way, If I got echo 'xxx', how do I substitute each 'x' into 'y'?
    – Zen
    Aug 20 '14 at 9:32










  • Substituting a string for another string? Just that? !!:1gs/x/y/
    – JezC
    Aug 20 '14 at 9:44











  • Though personally, I'd be looking at $(echo !! | sed -e s/x/y/g)
    – JezC
    Aug 20 '14 at 9:51










  • using sed would be too clumsy, can I add some parameter or symbol in ^x^y to do that job?
    – Zen
    Aug 20 '14 at 9:58
















By the way, If I got echo 'xxx', how do I substitute each 'x' into 'y'?
– Zen
Aug 20 '14 at 9:32




By the way, If I got echo 'xxx', how do I substitute each 'x' into 'y'?
– Zen
Aug 20 '14 at 9:32












Substituting a string for another string? Just that? !!:1gs/x/y/
– JezC
Aug 20 '14 at 9:44





Substituting a string for another string? Just that? !!:1gs/x/y/
– JezC
Aug 20 '14 at 9:44













Though personally, I'd be looking at $(echo !! | sed -e s/x/y/g)
– JezC
Aug 20 '14 at 9:51




Though personally, I'd be looking at $(echo !! | sed -e s/x/y/g)
– JezC
Aug 20 '14 at 9:51












using sed would be too clumsy, can I add some parameter or symbol in ^x^y to do that job?
– Zen
Aug 20 '14 at 9:58





using sed would be too clumsy, can I add some parameter or symbol in ^x^y to do that job?
– Zen
Aug 20 '14 at 9:58


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f151100%2fis-there-a-shortcut-for-repeat-the-second-proximate-command-in-bash%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

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

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay