Scrollable history?

One thing I've wanted to have in my shell, although I don't know if this is doable, is to be able to scroll through my history..

I know UP and DOWN show me my history buffer, but I like to see a list of the commands in my history.

In misc/mc , I can press Alt-h and it produces a scrollable history but only the hisory under misc/mc. I'd like have such an option straight from the shell.

Is this possible?
 
That doesn't make it scrollable, as in selecting and entry using the up/down keys.

I guess I'm not making it clear what I'm looking for.
 
On shells/zsh, hitting Ctrl-R shows history and Up/Down and hit Enter to choose entry, and it becomes editable.
It's "pageable", but not "scrollable" (means, flip the whole list when cursor is at topmost or bottommost entry). So it could NOT be what you really want, though.
 
So many options. For example: "history > /tmp/foo", then use whatever tool you want on /tmp/foo. That tool might be grep, sed, and such. Or use an editor on /tmp/foo.

If you like to work in an editor: most modern ones (like emacs and vim) have the ability to run a shell inside an editor window. Just type "history" into that shell, and then scroll that buffer.

Another approach is the XY problem: Why do you want to scroll your history? Probably to find a command you entered recently, and re-issue it, perhaps modified. Finding a command is easy with control R. Re-issuing it can be done right from there, perhaps after command-line editing. You can also learn to use the "!" syntax at the shell, which recalls a recent command, and allows you to modify it with the / construct.
 
What balanga wants is reasonable but I don't know of a shell that does it. I'd like a similar facility in less itself, particularly for wading though log files. The idea is, only lines that match the user specified search regex are shown, and one can scroll up/down. Then if you change the regex, other lines may be shown but the cursor remains where you left it last and you can specify a new regex to scroll up/down relative to that line. If you have this feature in a shell, you should be able to pick a range of lines, optionally modify them, and re-execute them. May be you can do this from emacs? [speculating as I don't use emacs]

Note that ! only allows picking one line and strictly in the "ed" model.
 
[…] Is this possible?
Yeah, you can always populate a dialog(1) with the shell history. 📜 With curses support you can also click on menu items. 🤪 Filling the interactive command line edit buffer would require use of the TIOCSTI tty(4) ioctl(2) and it only works on the assumption the shell was in some sort of input mode.​
Bash:
# Displays a menu of recent shell history items.
hist_menu() {
    {
        fc -l -1  # To get the most recent item’s number.
        fc -l
    } | {
        # -E : use extended “modern” regular expressions
        sed -E 's ^[[:space:]]*([[:digit:]]+)\*?[[:space:]]+ \1\t '
    } | {
        # Not all `read` implementations can deal with null bytes.
        read -r number lastcommand
        # xargs(1) treats quotation marks specially.
        # Commands in the history may contain quotation marks.
        # To preserve them, instruct xargs(1) to split on null bytes.
        sed 's \t \x0 ' | tr '\n' '\0' | xargs -t -0 \
            dialog --output-fd 1 --default-item ${number:?} \
            --menu 'Select a command to reexecute.' 0 0 0
    } | {
        read -r command
        # -s : silently execute command, do not spawn an editor
        fc -s ${command:?None selected.}
    }
}

alias h=hist_menu
 
So many options. For example: "history > /tmp/foo", then use whatever tool you want on /tmp/foo. That tool might be grep, sed, and such. Or use an editor on /tmp/foo.

If you like to work in an editor: most modern ones (like emacs and vim) have the ability to run a shell inside an editor window. Just type "history" into that shell, and then scroll that buffer.

Another approach is the XY problem: Why do you want to scroll your history? Probably to find a command you entered recently, and re-issue it, perhaps modified. Finding a command is easy with control R. Re-issuing it can be done right from there, perhaps after command-line editing. You can also learn to use the "!" syntax at the shell, which recalls a recent command, and allows you to modify it with the / construct.

Actually running something like history >history.txt & vim history.txt does the sort of thing I want.

And if 'set cursorline' in vimrc I have highlighted cursor which is close to what I want to achieve.
 
Yeah, you can always populate a dialog(1) with the shell history. 📜 With curses support you can also click on menu items. 🤪 Filling the interactive command line edit buffer would require use of the TIOCSTI tty(4) ioctl(2) and it only works on the assumption the shell was in some sort of input mode.​
Bash:
# Displays a menu of recent shell history items.
hist_menu() {
    {
        fc -l -1  # To get the most recent item’s number.
        fc -l
    } | {
        # -E : use extended “modern” regular expressions
        sed -E 's ^[[:space:]]*([[:digit:]]+)\*?[[:space:]]+ \1\t '
    } | {
        # Not all `read` implementations can deal with null bytes.
        read -r number lastcommand
        # xargs(1) treats quotation marks specially.
        # Commands in the history may contain quotation marks.
        # To preserve them, instruct xargs(1) to split on null bytes.
        sed 's \t \x0 ' | tr '\n' '\0' | xargs -t -0 \
            dialog --output-fd 1 --default-item ${number:?} \
            --menu 'Select a command to reexecute.' 0 0 0
    } | {
        read -r command
        # -s : silently execute command, do not spawn an editor
        fc -s ${command:?None selected.}
    }
}

alias h=hist_menu
To be honest I'm a bit lost with what you are suggesting.

Do I copy this hist_menu function into a file and call it hist_menu? And how to I invoke it?

Apologies if this is a dumb question.
 
That doesn't make it scrollable, as in selecting and entry using the up/down keys.

I guess I'm not making it clear what I'm looking for.
No shell will provide a scrollable list. Shells that implement the csh style of history callback, i.e. bash, zsh, have the ! facility to execute previous commands. Shells that implement the ksh style of history callback, i.e. bash, zsh, sh, allow you to use ctrl-r to search through history.

I suppose if you use emacs as your shell (some did when I started using UNIX) you could write an emacs lisp extension to provide a scollable list that can be mouse selected.
 
I've been using tcsh as my shell for a while and have never wanted more than the "history" command. I typically have an alias "h" as "history 25" so basically screen worth of history. If I need more I type out history.
For shells that allow you to save the history, that winds up in a file, so one could simply look at that.
 
One thing I've wanted to have in my shell, although I don't know if this is doable, is to be able to scroll through my history..

I know UP and DOWN show me my history buffer, but I like to see a list of the commands in my history.

In misc/mc , I can press Alt-h and it produces a scrollable history but only the hisory under misc/mc. I'd like have such an option straight from the shell.

Is this possible?
Yeah, totally doable. Easiest options:
Use history piped into a pager:
history | less
Boom — scrollable history.
Searchable history with Ctrl-R: lets you jump back fast, way better than UP/DOWN.
fzf integration:
HISTFILE=~/.bash_history
bind '"\eh": "fzf-history-widget"\n'
Then press Alt-h for a fuzzy, scrollable picker — basically what you want.
So yeah, you can get scrollable history; just depends how fancy you want it.
 
I have no idea, I am a tcsh user and never used something else, but looking at the sources, it looks like a plugin that should be copied to some directory used by bash/zsh to load extended functionality (/usr/local/share/something). A bash/zsh user could know how to use it.
 
Apart from no standard shell I know supports that natively, if any it's the terminal emulator that provides scrolling:
Just do history, or simply h (is common default alias for 'history 150' (at least in [t]csh), and simply scroll in your xterm window.🤓
Since I am xterm-only user I'm not quite sure, but there may other terminal emulators that provide some 'history management'.

But since I set my history to 4000 entries that wasn't really useful anyway.
I either need one of the last dozen entries, or I do something like history | grep ffmpeg
I find that way more effective than scrolling through a very long list. 😴
 
Back
Top