Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

Yes, this. For example, `Ctrl+R makedo` would go back in history to the first command that matches this, to use the ` ./scripts/makedocs.sh` example from the article.


sort by: page size:

Good idea, but you shouldn't overwrite ctrl-O, which is a wildly useful function in bash: run current command in history and retrieve the next. If you want to re-run a sequence of several history commands, you can find the first in history (such as via ctrl-R reverse-isearch), then repeatedly hit ctrl-O to run it and retrieve the next command from history.

My favorite bash trick is still Ctrl-O: submit command and advance history by one.

If you ever find yourself counting up-arrows to repeat a sequence of commands (that aren't worth scripting for whatever reason), next time, go to the top of the list and hit Ctrl-O. The result is the the command runs, and the one after it in the history is loaded into you prompt.


I use ctrl-P/N for history-beginning-search-{backward,forward} in Zsh. Type the start of a previous command and cycle through the matches.

Bash can do that, with Ctrl-r. Press Ctrl-r, then type beginning of command, then press Ctrl-r repeatedly to go back through history.

https://www.digitalocean.com/community/tutorials/how-to-use-...

That page also deals with stuff like getting bash not to delete history with each session, so searching history can be more useful.


If this can help with history across many shell instances that would be so great. I've never figured out what to expect, what happens when you have multiple zsh windows across many session, what to expect for how I can navigate that or those histories well.

Random side point, it frustrates me so much I cant get the pre-substitution command taht I actually type, at least in zsh. I really want to see the history of what I type! Not just what the outcome was, but learn & adjust with what I type.

A while back I hacked around writing vim commands that would run my current line (or selection) in a terminal, and then copy the command, paste it after the current line, & go to the next line (and another command to do the same but not re-copy the output). That let me better see the work I had done, and I really liked that, but it was a pretty drastic hack born of desperation.

Shell histories! Yay! Anyone have other good tools they use?


"By way of comparison, on my home PC I used a third-party command shell called 4DOS [...] It had a wonderful command line history mechanism: type part of a command, then press up-arrow."

Want in bash? Add to ~/.inputrc:

# up and down arrows do incremental history search

"\e[A": history-search-backward

"\e[B": history-search-forward


Unsure if it’s a bash feature, but press ctrl+r on the cli

The interactive arg/string you type matches, against your history, to the most recent cmd that contains the string


"In bash, use Ctrl-R to search through command history."

Or, add these lines to your .bashrc to use Ctrl-p and Ctrl-n:

bind "\C-p":history-search-backward

bind "\C-n":history-search-forward


Agreed, but even nicer than control-r are the readline functions `history-search-backward` and `history-search-forward`.

Personally I bind them to up/down which on OS X at least involves:

  # Put this in some file like ~/.readline-bindings
  "\e[A": history-search-backward
  "\e[B": history-search-forward

  # And this in your ~/.bashrc or ~/.zshrc
  bind -f ~/.readline-bindings
That way if you haven't typed any input it behaves like normal up (previous command), but if you've typed some characters it only retrieves matching commands.

http://codeinthehole.com/writing/the-most-important-command-...


I just want to throw in that you can get that nice up-arrow-history in bash too. It's the first thing I put in a new bash environment:

  bind '"\e[A":history-search-backward'
  bind '"\e[B":history-search-forward'

> You can arrow-up to go to previous commands just like in bash. But if you start a command (say, ssh) and then arrow-up you will then be cycling back through the last ways you entered that command.

Or, if you'd like to do this in Bash, and any program that uses readline: In ~/.inputrc

    # Bind the up arrow to history search, instead of history step
    "\e[A": history-search-backward
    # And the reverse (down)
    "\e[B": history-search-forward

Yes, thank you.

Also starting to type a command and using up-arrow to scroll through (I assume) endings of that command line that are in history. That's great too. But I often don't do, say,a do-release-upgrade (I use Kubuntu but prefer command line upgrade) within the time the command stays in history.


I don't know about this. I use Ctrl+R a lot, and I sometimes like to be able to go up the history, then down to the command line I was writing. I probably need to try this (or a variant) out, thougt, as it boils down to muscle memory.

zsh: https://unix.stackexchange.com/questions/16101/zsh-search-hi...


If you put this in your `~/.inputrc` [1]:

    "\e[A": history-search-backward
    "\e[B": history-search-forward
    "\e[C": forward-char
    "\e[D": backward-char
  
you can start typing a previous command and go up through your history for all commands starting with whatever you currently have typed into the bash shell. Reset the scrolling through the history with `ctrl+c`.

Personally, I hate that powershell remembers your position in the shell history. I don't want to maintain a mental model of the shell history rollodex and where I am in it. That's one less part of my brain that I can't use for thinking about the code. If I have a set of commands to type over and over:

    history 20 | head -10 | sed -r 's/^ *[0-9]+ *//g;' > new_script.bash

* [1] http://codeinthehole.com/writing/the-most-important-command-...

Not quite a command, but ctrl+r in bash does a real useful history search

In many bash configurations, ctrl-o is bound to 'operate-and-get-next’. If you hit up 20x, you should then be able to hit ctrl-o to run that command, and your history will automatically go to the next command down. Check it out, it’s great!

EDIT: beaten by pm215!


> [1] eg. putting history -a in PROMPT_COMMAND as the article recommends appends your session's entire history each time, IIRC.

No, only the last command. Very handy because that way you can start a new terminal and have your current history available immediately. I also have an alias r='history -n'. With this you can load the history of another terminal session directly into the current session.


(I have a feeling that) my frequently used commands change depending on what I'm doing, and with time. Instead of trying to alias my current favourite commands, I tend to just use the backwards history search built in to my shell, and access it with Ctrl-R. I can type the beginning of the command and have the rest of it completed for me.

The only thing that would make it better is Ido-style completion.


This looks like a neat idea but the use case seems very specific. I have always found history (which I always alias to “hh”) to be very useful, combined with !<number> to quickly execute previous commands. The README doesn’t do enough to explain exactly how to use it, besides 3 screenshots.

When dealing with bash history, I find that too many people are still unaware of C-r (Ctrl+R or Command+R): press it and start typing unique bits of your past commands. If multiple commands match, cycle through them with repeated C-r presses.

The usual reaction is :mindblown: :)

next

Legal | privacy