LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 11:36 AM   #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, 11:47 AM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Ctrl+R
start typing previous command and press the <end> key
edit c-li command
Press <enter>
 
Old 04-12-2013, 11:48 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
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, 12: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, 01: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 01:20 PM. Reason: added bit about :p
 
1 members found this post helpful.
Old 04-13-2013, 08:48 AM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
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 08:50 AM.
 
Old 04-15-2013, 07: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, 08:51 AM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
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 10:44 AM.
 
  


Reply



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 07: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 02:14 AM
Inject/modify files in iso image on the fly brianmcgee Linux - Software 10 08-01-2008 02:08 AM
The command to burn dvd on-the-fly scng Linux - Software 3 10-04-2005 10:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:09 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
Open Source Consulting | Domain Registration