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

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.



sort by: page size:

Just use Ctrl-R in bash to search backwards in the history

> you can do ctrl + r to achieve the same thing.

C-r will (usually?) invoke reverse-search-history (http://www.gnu.org/software/bash/manual/html_node/Commands-F...) in bash (and I'm sure some other shells also).


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

"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


I've noted once within my personal bash cheat sheet this:

Search the command history

* Search as you type: Ctrl + r and type the search term; Repeat Ctrl + r to loop through results.

* Search the last remembered search term: Ctrl + r twice.

* End the search at current history entry: Ctrl + j

* Cancel the search and restore original line: Ctrl + g

Try of course at your own risk. But works for me.


You are probably referring to fish shell which has this feature (https://fishshell.com/).

However, I find that zsh + ohmyzsh works just as well. After typing the beginning of a command you can use the arrow keys to cycle through all commands in the history with the same beginning.

A third method for efficient history search is ctrl+r. Just press ctr+r and start typing part of the command. Then you press enter to execute or ctr+r to cycle through the history.


Example:

Tip: Bash reverse search, because manually scrolling through your history is a nuisance.

How (OS X): CTRL+R, start typing a word from your command history. Press CTRL+R to cycle through matches, ENTER to execute, TAB to edit. If you want to add forward search to bash, you can re-map your key bindings in your bash profile with the following (maps to CTRL+T): bind "\C-t":forward-search-history


I use an eternal history setup for bash, with bash session tags that let me sort history by tag for correct backward-search behavior.

(edit) Not to imply this functionality belongs to a terminal, far from it. But it's nice to have access to 2 years of shell commands :-)


I do the same, except instead of `history` i grep the .bash_history file :-P

(i almost always need it for stuff i typed several sessions ago)


I know that people like to use ctrl-R to search backwards through history, but I am very zealous about using the up-arrow to search through your history instead (in your .bashrc):

bindkey "^[[a" history-beginning-search-backward

bindkey "^[[B" history-beginning-search-forward


Also, another way to find past commands you run `history | grep <whatever-you-are-looking-for>` and works wonders; or as other users have mentioned, Ctrl-R works too.

In case you didn't know, bash has a basic version of this already embedded. You can search your bash history from the command line with Control+R (not Cmd+R even for Macs).

There is a history command, with a way to reload, so this would be possible by writing to bash_history from bashrc, then reloading the history I think. Not tested.

This is a short guide that is an eye opener as to just how much can be done with bash history: https://www.digitalocean.com/community/tutorials/how-to-use-...


Actually, after hitting C-R <keyword>, you can keep pressing C-R to search backwards through the history. Or C-S to search forwards. More info: (bash uses emacs editing mode by default) http://www.gnu.org/software/emacs/manual/html_node/emacs/Bas...

I'm up to about 4 years of history at this point. I use a dumb bash hook that sorts and reloads the history on each command execution, and yes, it's getting a little slow. I wish I could do this through some daemon-based DB-backed system but this works for now. Maybe I'll switch to zsh when it gets too slow.

The reason I have the hook reload and sort on each command is so that I can use a dozen screen sessions at once and:

1) they don't overwrite each other's history

2) each session's history is instantly searchable to the others

3) sorting is useful because each session prepends a session ID to the commands, so that Ctrl-R backward search can always hit the current session's commands first. And I can reboot/restart screen and recover history in each session.

(edit: I looked at the zsh-db project the other poster linked, and this does pretty much the same thing just probably slower since there is no DB involved).


Also bash has a built-in incremental history search.

http://www.gnu.org/software/bash/manual/bashref.html#Searchi...


A thing to do with your environment setup, so a tool I suppose? There are probably different plugins out there for it. In my case it's a few lines in .bashrc that override the history processing to never delete old history, and add tags indicating which session wrote the command. This way I can reboot a computer, restart all 30 screen sessions, and have each access backward searchable history to the start of time (2013 for me).

I'm guessing newer shells may have better support for this kind of thing instead of running shell commands to wrangle text files on every prompt.


Better yet, just set your history to 10k-20k+ items and use reverse-search (ie. Ctrl+r in bash) to recall your old commands. Navigation aside, about 80-90% of commands I use regularly are already in the shell history.

Not just that.

Add this to you inputrc:

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

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

Now up and down arrows will search your history with whatever you already have typed at the prompt. Re-running a command often? Probably just have to type its first letter and hit up arrow once or twice.

next

Legal | privacy