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?


up vote 61 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).

  • There is a typo in your first example... – pbm Nov 15 '10 at 20:21
  • 8
    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

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.

  • Also Esc, # works – Steven Penny Jul 8 at 3:57
  • and you can actually type it on a mac (hold shift, press ESC and # together), unlike the other ket combo (fn+alt+shift+#) ;-) – 0xF2 Oct 4 at 3:12

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.

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

$if Bash
     Space: magic-space

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

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...

  • 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
  • 1
    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

I have often wanted this. When I used csh (and tcsh), I had an idiom:

This idiom inserts "word 88 through the last word" of the previous command at the point where you're typing on the current command line, which is typically the end. In csh, it is not an error for such an expression to produce no words. That is, there need not be a word 88, nor any that follow it. If there is no word 88, this adds no words to the end of the command being typed, and then pushes the resulting command line into the history without execution.

As you probably know,
* ! (like !! and !-1) is the previous command;
* :88 is word 88 (the first word -- typically the command -- is zero) and csh would require such a word exist, but ...
* :88* is all the words starting at word 88, and then csh does not require the word to exist;
* :p means print, but do not execute the command line.
With or without the :p, the command line is added to the command history.

"Why 88?" you wonder? Because it is the same key as the * I'll need. If you have commands that long, perhaps !:888*:p is what you need. Sorry this doesn't work with bash AFAIK. Bash will merely say bash: :88*: bad word specifier

Fun fact: your command does not usually need to be first.
> /tmp/foo echo My command is word 3
is valid in bash and csh.

  • Since the question is tagged for bash, it might be good to start with saying that your solution works only in (t)csh. – zagrimsan Jul 31 '17 at 7:34

I normally use Ctrl+r to search and then Ctrl+e to go to the end of the line without executing.

  • already mentioned – Jeff Schaller Jan 26 at 1:08
  • @JeffSchaller I'm not seeing it. I see the CTRL+R reference. No where do I see the CTRL+E which is the missing piece of the puzzle in the above posts. – nikc Jan 26 at 6:25

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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