Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Sometimes I'd like to get some command from history output, copy it to bash prompt, make some changes and run it. Is there a way to copy command from a history output to bash prompt without involving mouse? For example, it would be some script, than I can bind shortcut to.

EDIT: Maybe I do not understand fc enough, but it looks like I cannot select folder or file via fc (like Tab for usual comand prompt)

share|improve this question
    
you should take a look here : unix.stackexchange.com/q/104394/53092 –  Kiwy Feb 10 at 8:45
    
fc 'command begins… ' –  mikeserv Mar 13 at 18:28

3 Answers 3

you need to take a look at the bash fc command.

If you want to edit before rexecuting a history line just do like this

fc 123

where 123 is the history line number you see typing the command history.
It will open your favorite editor and allow you to modifiy the line then quit and save and it will run.

You can also do it a range of command like this:

fc 123 135

To work on command history from 123 to 135.

Edit 1:
if you need to run without edit consider watch the post Re-execute fc command from history

share|improve this answer
    
This is not a bash command, exactly. Probably bash incorporates it but only because it is a POSIX-specified basic command that is guaranteed to exist in all POSIX compliant shells. And to run it without FCEDIT just fc -s –  mikeserv Mar 13 at 18:30

If you want to reexecute a command from your history, you can use ^r (ctrl + r) in your terminal. It's a reverse search which permits to retrieve a command and edit it before you enter it. I don't know if it works with other shell than bash. Though it's like one of the most useful thing I ever seen.

From the bash documentation :

reverse-search-history (C-r)
Search backward starting at the current line and moving ‘up’ through the history as necessary. This is an incremental search.

Here is the manual.

share|improve this answer
    
Yes that's also a possibility –  Kiwy Feb 10 at 9:26
1  
It is useful if you want to execute only one command from your history. Your solution is better in case you want to execute multiple lines. Plus the ^r technique is useful because you don't actually have to know the line number in the history. –  Depado Feb 10 at 9:28
    
fc 'command begins… ' will find only the last line that started that way, invoke FCEDIT on the command, then run it. –  mikeserv Mar 13 at 18:34
up vote 0 down vote accepted

Enter command number (like !1234) and press Alt+Shift+X

copy_line_from_history_to_prompt () {
  READLINE_LINE=$( history -p "$READLINE_LINE" ) ;
}
bind -x '"eX": copy_line_from_history_to_prompt' # Alt+Shift+X
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.