Using the bash history to get the previous command, copy it and then run it, but with the command comment

Just a question to improve your bash skills. I always do this:

 $ history | grep some_long_command ... ... 123 some_long_command1......... 124 some_long_command2......... ... 

Then I can run the command I found by doing:

!123

However, I often want to do this:

 some_long_command1foobar 

those. change the command before running it. Can you use bash to run this command:

#some_long_command1

for him to comment.

Then I do not need to use my mouse to select a command, edit it, and then run (I can just use the keyboard - faster).

I suppose I could write a script, but maybe there is already functionality built in somewhere ....?

Thank.

+77
command-line linux bash
Jun 12 '12 at 16:01
source share
10 answers

Instead of using the history command, I would suggest using ctrl+r and start typing this command. When you press the arrow key, as if to change it, it will not be recognized by autocomplete and will allow you to edit before starting.

UPDATE: also, if you want to iterate over different commands containing the string just entered, keep pressing ctrl+r

+134
Jun 12 2018-12-12T00:
source share
β€” -

In fact, you can simply add :p to the command to print it without executing it. For example:

 $ ls -la $ !!:p 

Print ls -la as the previous command without running it, and you can simply press ↑ (up) to find it and edit it.

You can also do

 !123:p 

to print the 123rd command as the previous command.

+54
Jun 20 '12 at 17:12
source share

You can also try the fc command to edit the command in history.

WIKI says

fc is a standard Unix program that lists or edits and re-executes commands previously entered into an interactive shell. fc is a built-in command in the bash shell ; help fc will show usage information.




In addition to the reverse incremental search ( Ctrl + R ), we have a few more bash shortcuts:

From man bash :

  previous-history (Cp)
     Fetch the previous command from the history list, moving back in the list. 
 next-history (Cn)
     Fetch the next command from the history list, moving forward in the list. 
 beginning-of-history (M- & lt)
     Move to the first line in the history. 
 end-of-history (M->)
     Move to the end of the input history, ie, the line currently being entered. 
 reverse-search-history (Cr)
     Search backward starting at the current line and moving 'up' through the history as necessary.  This is an incremental search. 
 forward-search-history (Cs)
     Search forward starting at the current line and moving 'down' through the history as necessary.  This is an incremental search. 
 non-incremental-reverse-search-history (Mp)
     Search backward through the history starting at the current line using a non-incremental search for a string supplied by the user. 
 non-incremental-forward-search-history (Mn)
     Search forward through the history using a non-incremental search for a string supplied by the user.
 yank-nth-arg (MCy)
     Insert the first argument to the previous command (usually the second word on the previous line) at point.  With an argument n, insert the nth word from the previous command (the words in the previous command begin with word 0).  A negative argument inserts the nth word from the end of the previous command.  Once the argument n is computed, the argument is extracted as if the "! N" history expansion had been specified.
 yank-last-arg (M-., M-_)
     Insert the last argument to the previous command (the last word of the previous history entry).  With an argument, behave exactly like yank-nth-arg.  Successive calls to yank-last-arg move back through the history list, inserting the last argument of each line in turn.  The history expansion facilities are used to extract the last argument, as if the "! $" History expansion had been specified. 
 shell-expand-line (MCe)
     Expand the line as the shell does.  This performs alias and history expansion as well as all of the shell word expansions.  See HISTORY EXPANSION below for a description of history expansion. 
 history-expand-line (M- ^)
     Perform history expansion on the current line.  See HISTORY EXPANSION below for a description of history expansion.
 insert-last-argument (M-., M-_)
     A synonym for yank-last-arg. 
 operate-and-get-next (Co)
     Accept the current line for execution and fetch the next line relative to the current line from the history for editing.  Any argument is ignored. 
 edit-and-execute-command (C-xC-e)
     Invoke an editor on the current command line, and execute the result as shell commands.
+17
Jun 12 '12 at 16:32
source share
 !123:gs/old/new/ 

Runs 123, replacing the string "old" with the string "new".

+10
Jun 12 2018-12-12T00:
source share

You can go into edit mode by pressing M- ^ (option-shift-6 on mac).

Enter this:

123M - ^

And you will edit command # 123. This is similar to using ctrl-r, but starting with the exclamation point syntax.

+5
Feb 11 '13 at 22:44
source share

You can also install

 shopt -s histverify 

in your .bash_profile , which makes any history extension appear on the command line without starting it, which allows you to edit before that.

+3
Jun 12 '12 at 16:15
source share

Instead of using the history command, bind history-search-backward / history-search-forward to key shortcuts that can be easily remembered (I prefer PgUp / PgDown). To do this, put this in your .inputrc file:

 "<key code>": history-search-backward "<key code>": history-search-forward 

To get <key code> , enter Ctrl-V <key> in the shell and replace the initial ^[ with \e with everything that was printed.

After that, you can simply type some and press PgUp to get some_long_command . If you need some_long_command with_some_arg , but there is a similar command some_long_command with_some_other_arg in the history, you can scroll it until you reach it by typing some and then pressing PgUp several times, or you can type some , press PgUp, move the cursor there , where the two commands begin to differ, enter a few characters and press PgUp again. This ability to quickly scroll through / distinguish between similar commands makes it, in my opinion, a much more convenient tool than Ctrl-R .

+3
Feb 20 '13 at 15:17
source share

You might want to try β€œsuggest a box” similar to the https://github.com/dvorka/hstr story - it reads the Bash story and allows you to move around quickly.

hh

To get the last command, just type hh, go to the command and use the right arrow to get it on the command line (where you can edit it and / or add a comment).

+1
Jan 10 '14 at 0:18
source share

^ p to get the latest typed command in unix / solaris

0
Feb 11 '14 at 14:15
source share

Placed

alias r = 'fc -s'

in .bashrc (home directory) then you can simply type

 r <whatever> 

on the command line and you will run a copy of the last <whatever> command (same parameters) as in your story. just press the up arrow to see what you have done if you feel the need.

0
Nov 27 '16 at 19:47
source share



All Articles