Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am able to use the history command on CentOS to get list of previous inputted commands, however, if I do something like: !372, history will attempt to run the referenced command.

I need the previous run command to appear in at current cursor. Here's an example:

[dev@home ~]$ previous_command_no_execute!372  
share|improve this question
add comment

7 Answers

up vote 24 down vote accepted

How about, put this on your command line:

$ !372

Then press ESC followed by CTRL+E. This will autoexpand on the command line without actually running it. (also expands everything else on the line, including env vars)

This only works on Bash, as far as I'm aware.

share|improve this answer
    
Yes, this is the exact solution. –  centosnoob Mar 30 '12 at 17:25
1  
For a solution in zsh, hit tab after typing !372 and it will expand it for you. –  Reid Mar 31 '12 at 2:17
add comment

You could use Ctrlr and then type some characters from the original command e.g.

(reverse-i-search)`/etc': sudo /etc/init.d/apache2 restart

above I typed Ctrl-r /etc and it pulled up the sudo /etc/init.d/apache2 restart command from the history

share|improve this answer
    
yes this is an appropriate solution. thank you. –  centosnoob Mar 30 '12 at 17:22
add comment

history | grep 372 should work, right?

Edit:

Additionally you could do something like echo !372 > /dev/clip to put the command directory into your clipboard.

share|improve this answer
    
always worked for me... –  voretaq7 Mar 30 '12 at 16:51
    
no that just finds that specific command previously run. –  centosnoob Mar 30 '12 at 16:52
    
Then both myself and @voretaq7 are misunderstanding your question. Could you perhaps be a little more clear? –  MDMarra Mar 30 '12 at 16:53
    
@MDMarra I have updated the question. I hope it sheds some further light. –  centosnoob Mar 30 '12 at 16:56
    
@centosnoob looking at your update, and assuming your console supports a mouse and copy-paste (and it's rare to be directly on the terminal-style console with no copy-paste capability these days) the copy and paste solution is really the least painful. vi-style command editing as I outlined in my answer is another option, but I'm a vi zealot and even I don't use vi-style command line editing... –  voretaq7 Mar 30 '12 at 17:00
show 1 more comment

MDMarra gave the easy way (history|grep and copy/paste using your desktop environment, assuming you have one). I'll provide what I call the hard way, which works if you don't have a desktop environment or a mouse.

Assuming you're running bash as your shell:

$ set -o vi
$ ESC372k

(you'll now have the command you ran 372 commands ago, on the line, and you'll be in vi-style command mode. Edit as you would if you were using ex/vi)

share|improve this answer
    
Interesting. That only seems to work if you have "set -o vi" run early on. Might have to put that in bash profile or something. –  Matt Mar 30 '12 at 17:03
    
That's one of those "I always use vi, why aren't I using vi bindings to edit lines in bash" things that you realize with a shock. It takes a little while to get used to, then the emacs-style bindings that everyone else uses feel weird and useless. –  cjc Mar 30 '12 at 21:13
    
@Matt Yes - you need to set your edit mode to vi-style editing. It should work as soon as you set -o vi, and should work for your entire history (the edit mode is just an interface to the shell's command history and other things). –  voretaq7 Mar 30 '12 at 21:39
    
@cjc I use vi as my editor of choice, but I do not use vi-style bindings to edit my command line (I just happened to know how to do this in vi-mode off the top of my head, but others have posted the equivalent in emacs-mode which may be more friendly :). –  voretaq7 Mar 30 '12 at 21:41
add comment

The simplest and quickest way to do what you want is to run:

!372:p

followed by Enter and

share|improve this answer
add comment

Are you on a desktop system or server?

On my Kubuntu desktop system with the shell open it supports highlighting with the mouse. I usually just highlight the text with the mouse and click the middle mouse button to paste it in. Would that work for you?

So you can grep for the command. Highlight the text. Paste it with the middle mouse button, then edit it.

There might be a better way but this works for me.

share|improve this answer
add comment

there is no command to do auto paste, but you can recall any command into an editor to edit it before executing. this may achieve what you are looking for, but upon exiting the command will execute so if you want to abort, you should clear your edit buffer before exiting your editor.

fc -e vi 372 372

share|improve this answer
add comment

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.