LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-12-2013, 12:36 PM   #1
rodusa
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Rep: Reputation: Disabled
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
 
Old 04-12-2013, 12:47 PM   #2
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,652
Blog Entries: 10

Rep: Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075
Ctrl+R
start typing previous command and press the <end> key
edit c-li command
Press <enter>
 
Old 04-12-2013, 12:48 PM   #3
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,652
Blog Entries: 10

Rep: Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075
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
 
Old 04-12-2013, 01:31 PM   #4
rodusa
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-12-2013, 02:17 PM   #5
archShade
Member
 
Registered: Mar 2006
Location: Delft NL
Distribution: Debian; Slackware; windows 7
Posts: 218

Rep: Reputation: 53
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.

Last edited by archShade; 04-12-2013 at 02:20 PM. Reason: added bit about :p
 
1 members found this post helpful.
Old 04-13-2013, 09:48 AM   #6
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,652
Blog Entries: 10

Rep: Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075
Quote:
Originally Posted by rodusa View Post
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.

Last edited by Habitual; 04-13-2013 at 09:50 AM.
 
Old 04-15-2013, 08:40 AM   #7
rodusa
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
This is what I needed:
!22

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
 
Old 04-15-2013, 09:51 AM   #8
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,652
Blog Entries: 10

Rep: Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075
Quote:
Originally Posted by rodusa View Post
This is what I needed:
!22

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/

Last edited by Habitual; 04-15-2013 at 11:44 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Is there any command to delete bash command history? cola Linux - Newbie 4 06-01-2010 08:32 AM
URGENT! Is there any command to get a history command lines and time in SUSE Linux.? igsoper Linux - Software 5 06-25-2009 03:14 AM
Inject/modify files in iso image on the fly brianmcgee Linux - Software 10 08-01-2008 03:08 AM
The command to burn dvd on-the-fly scng Linux - Software 3 10-04-2005 11:19 AM


All times are GMT -5. The time now is 12:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration