2

I type history then hit Enter to view the latest commands, and I can print a command using something like !123:p, but this prints it after the command prompt then shows an empty command prompt.

I want to run a command to actually put a certain line from history onto the command prompt so that I can edit it or hit Enter to run it.

  • The terminal is largely irrelevant, You need to tell people which shell you are using. And be specific about the operating system that the shell is running on, because that tells people what version of some particular shells you are likely to be using. So you need to fix the question tags, too. – JdeBP Jun 27 '18 at 20:25
2

I think you're looking for the histverify option:

I don't remember which files come out of the box on the Mac, but I have:

$ cat ~/.bash_profile
[[ -f ~/.bashrc ]] && source ~/.bashrc

$ cat ~/.bashrc
...
shopt -s histverify
...

With that option enabled, when you use history substitution, you'll get the option to edit the command before it is executed. From the bash man page:

histverify
If set, and readline is being used, the results of history substitution are not immediately passed to the shell parser. Instead, the resulting line is loaded into the readline editing buffer, allowing further modification.

  • Is the default shell on OS X bash ? I'd check, but it is my work machine... and I installed Linux on it anyway... (just couldn't get used to OS X) – ivanivan Jun 29 '18 at 22:08
1

The up arrow will go through the commands starting with the last one. I recommend you use this with commands that have been run rather recently so that you don't have to go crazy with the up arrow.

You can also use:

CTRL +r

Start typing the command afterwards and it will appear according to your input. You can then either hit ESCAPE to get it at the prompt without executing it or you can execute it with the Enter key if you want to.

  • It does not have to be recently. Can be accros sessions as well. Bash history is written to disk (can't recall the exact filename, and I'm on mobile) – Tim Jun 29 '18 at 20:08
  • 1
    @Tim I know. I'm not saying that it needs to be but that it's more convenient if it has so that he doesn't have to press the up arrow without relent. It's for this reason that I included CTRL +r. I'll update my answer to account for this. – Nasir Riley Jun 29 '18 at 20:48
0

If you type history on the command line a list of past commands will be listed preceded by a number. if you type an exclamation point followed by the number of a command, that command will be repeated. For example !12 will list the 12th command from the history list and run that command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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