LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Modify history command on the fly (https://www.linuxquestions.org/questions/linux-newbie-8/modify-history-command-on-the-fly-4175457922/)

rodusa 04-12-2013 11:36 AM

Modify history command on the fly
 
For instance:
How can recall the command in line 22 "touch myfile" so that I can modify the name myfile?

22 touch myfile
23 cp myfile ~\docs

I can only run the command such as !22, but that is not what I want.

Thank you
rod

Habitual 04-12-2013 11:47 AM

Ctrl+R
start typing previous command and press the <end> key
edit c-li command
Press <enter>

Habitual 04-12-2013 11:48 AM

Code:

bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'

added to .bash_history is another method.

http://unix.stackexchange.com/questi...t-executing-it

rodusa 04-12-2013 12:31 PM

I don't think you understood. I need to bring the the command, and be able to modify it. The bang command executes it automatically. I just to do something like this !22 edit
the I can go and modify the command and press enter to run it.

archShade 04-12-2013 01:17 PM

Why does interactive history search command (ctrl-R) not work for you - is there a reason you have to identify the command by the number and not by a matched string?

One alternative is to use the s modifier. this allows for substitution. say you have command 22 as touch myfile.

you could use
Code:

!22:s/myfile/yourfile
and this would run
Code:

touch yourfile
.
or
Code:

!22:s/touch/vim
and it would run
Code:

vim myfile

you can also use the print modifier to just print(:p) the command so you can see what your doing.

Habitual 04-13-2013 08:48 AM

Quote:

Originally Posted by rodusa (Post 4930442)
I don't think you understood.

I know Three ways to do this task, but only 1 that produces the behavior you are describing.
Code:

shopt -s histverify
is what you want and I know exactly what you meant and three ways to interact with bash history, and I gave two methods and one link that
has the answer by the way.

Now before you might offend us further wrt: "I don't think you..." style statements,
Code:

history | tail 
  816  2013-04-12 20:26:46 find notebook.zim
  817  2013-04-12 20:27:17 find notebook.zim -exec cat {} \;
  818  2013-04-12 20:27:55 ll zim/
  819  2013-04-12 20:28:00 ll zim/preferences.conf
  820  2013-04-12 20:28:02 cat zim/preferences.conf
  821  2013-04-12 20:28:39 cd ~
  822  2013-04-12 20:28:42 ll
  823  2013-04-13 09:35:41 shopt -s histverify
  824  2013-04-13 09:37:18 history
  825  2013-04-13 09:37:27 history | tail
!420
cat zim/preferences.conf # this is my kung-fu

What's sad that you had the correct Solution in my 2nd reply/link, and I perhaps spent more time on your solution than you did.

Good day, sir.

rodusa 04-15-2013 07:40 AM

This is what I needed:
!22:p

The p modifier displays the command without executing it.

The problem now is that I have to use PgUp command to access the command and be able to modify it.


Thanks

Habitual 04-15-2013 08:51 AM

Quote:

Originally Posted by rodusa (Post 4931805)
This is what I needed:
!22:p

The p modifier displays the command without executing it.

The problem now is that I have to use PgUp command to access the command and be able to modify it.

Thanks

That's a neat trick I haven't seen the :p in recent memory, if at all.
The extra step on PageUp kind of sours the deal b/c more input is required to
fully do the chore.

Naturally, I favor my "muscle-memoried" bind as it removes the extra step.

terminal >
start typing a portion of the previous command and press Up and it fills in the rest.

both bind directives and the histverify options in .bashrc could be utilized.

Found this neat article:
http://www.catonmat.net/blog/the-def...-line-history/

and this:
http://samrowe.com/wordpress/advanci...he-bash-shell/


All times are GMT -5. The time now is 02:13 PM.