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.

I use !n where (n) is the line number for executing a line in the history file I want executed at the command prompt which I find via history|less.

But there is a command line history event I wish to manually modify. How can I insert into the command line a history events contents without it actually executing so I can modify and then press return?

Best,

share|improve this question

6 Answers 6

up vote 14 down vote accepted

To request that the command be printed rather than executed after history substitution, add the :p modifier, e.g. !42:p. The resulting command will also be entered in the history, so you can press Up to edit it.

If you have the histverify option set (shopt -s histverify), you will always have the opportunity to edit the result of history substitutions.

The fc builtin gives limited access to history expansion (no word designators), and lets you edit a previous command in an external editor.

You can use !prefix to refer to the last command beginning with prefix, and !?substring to refer to the last command beginning with substring. When you know what you're looking for, this can save a lot of time over history | less.

Another way to search through previous history is incremental search: press Ctrl+R and start entering a substring of what you're looking for. Press Ctrl+R to go to the previous occurence of the search string so far and Ctrl+S if you've gone too far. Most keys other than Ctrl+R, Ctrl+S, Backspace and ordinary characters terminate the incremental search and have their usual effect (e.g. arrow keys to move the cursor in the line you've reached, Enter to run the command).

share|improve this answer
    
There is a typo in your first example... –  pbm Nov 15 '10 at 20:21
4  
You will need to do stty -ixon before Ctrl-s will work. –  Dennis Williamson Nov 15 '10 at 23:51
    
@Dennis: Wow, modern terminal emulators don't do this by default? <test> You're right, even gnome-terminal in Ubuntu has flow control! –  Gilles Nov 16 '10 at 0:05
    
I don't know why they still do. It's impossible to catch something while it's scrolling. By the time you press Ctrl-s, you've got a prompt again. And slow serial connections are mostly a thing of the past. –  Dennis Williamson Nov 16 '10 at 0:43
    
@Dennis's point about Ctrl-s is important, otherwise one gets stuck as I did. –  imz -- Ivan Zakharyaschev Apr 27 '11 at 12:35

Using Ctrl+r you can search history:

pbm@tauri ~ $ 
(reverse-i-search)`xran': xrandr -o normal

Any command that you find could be edited...


I think that I found exactly what you need: run shopt -s histverify and next time when you want to use !n command will be not executed but only put to command line...

share|improve this answer
    
You have to execute a command before it can be searched with Ctrl+r, not what Vass is asking for. –  phunehehe Nov 15 '10 at 15:59
    
I don't understand how this doesn't do what he asks. I would add that if you want to first edit the command before running it, you can simply hit TAB once you have found the right command. –  Steven D Nov 15 '10 at 16:04
2  
So the procedure would be, (1) Ctrl+r, (2) type until you find command, (3) TAB, (4) edit command, (5) ENTER. –  Steven D Nov 15 '10 at 16:06
1  
@phuenhehe: "But there is a command line history event I wish to manually modify" –  pbm Nov 15 '10 at 16:08
1  
Won't !n:p work? –  alex Nov 15 '10 at 19:07

You can use magic space to expand history before hitting enter. In your .inputrc, map space to magic space:

$if Bash
     Space: magic-space
$endif

Now, whenever you type a space after a history specification, it'll be immediately expanded - handy if you want to edit it, too!

share|improve this answer

Take a look at the history-expand-line command, bound to Alt+^ by default. It will expand the line in-place which you can then edit.

share|improve this answer

Another small one: Alt+#

comments out the current line and moves it into the history buffer.

So when you're assembling a command line and you need to issue an interim command to e.g. find a file, you just hit alt+#, issue the other command, go up in the history, uncomment and proceed.

share|improve this answer

If you use bash, just edit ~/.bash_history, add whatever command you want into it and close / reopen the console (or logout/login if you are in cli only).

share|improve this answer
1  
is there not something a bit more elegant? –  Vass Nov 15 '10 at 15:25

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.