86

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,

Vass
  • 5,371

8 Answers8

111

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

23

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.

fra-san
  • 10,205
  • 2
  • 22
  • 43
Rahul Patil
  • 24,711
9

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!

Cascabel
  • 1,651
8

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.

5

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

Sooth
  • 391
4

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

pbm
  • 25,387
  • 1
    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
  • This answer is ok, just that searching with Ctrl+r through a long history is cumbersome, I know the line number in the file, so can that be used at all? And how do you keep moving backwards/forwards when in the Ctrl+r mode? Best, – Vass Nov 15 '10 at 16:32
  • If you press Ctrl+r while searching next result will be shown. – pbm Nov 15 '10 at 16:45
  • I updated my answer - I think that I found exactly what you need. – pbm Nov 15 '10 at 17:05
  • 1
    Won't !n:p work? – alex Nov 15 '10 at 19:07
  • !n:p works for me after setting shopt -s histverify – pbm Nov 15 '10 at 19:30
  • @alex, that was awesome, where could I have read up on that? I find no documentation on this anywhere – Vass Nov 15 '10 at 19:33
  • @Vass: man bash - section HISTORY and HISTORY EXPANSION – pbm Nov 15 '10 at 19:35
  • @Vass: Repeated pressing of Ctrl-r will step back through matching command lines. If you add stty -ixon to your ~/.bashrc then you will be able to reverse course and step forward through the matches using Ctrl-s. – Dennis Williamson Nov 15 '10 at 23:50
1

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

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 07:34
1

If you're a vi user, you could set vi as the line editor, and use vi commands to move around the shell history and edit the line before executing.

$ set -o vi # add this to .bashrc
$ [Esc]<history line number>[Shift-g]
# edit the line and hit [Enter] to execute

This way you don't need to remember another way to edit the command line. It works for emacs too.

This is how you search the history using regex:

# find commands starting with cd
$ [Esc]/^cd[Enter]
# press [n] to go to next line (up)
# press [Shift-n] to go to previous line (down)
fnds
  • 11
  • Whilst this is not an immediately intuitive answer for most people, it's a good solution for those that put the time in! – Chris Aug 17 '20 at 06:37