File: fzhist

package info (click to toggle)
nnn 5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: ansic: 11,902; sh: 3,585; makefile: 512; cpp: 80; python: 31; csh: 2
file content (53 lines) | stat: -rwxr-xr-x 1,250 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env sh

# Description: Fuzzy find a command from history,
#              edit in $EDITOR and run as a command
#
# Note: Supports only bash, zsh and fish history
#
# TODO: For zsh there's also $ZDOTDIR which might need to be checked as well for the -z $HISTFILE && -n $ZDOTDIR case.
#
# Shell: POSIX compliant
# Author: Arun Prakash Jana

if type fzf >/dev/null 2>&1; then
    fuzzy=fzf
else
    exit 1
fi

shellname="$(basename "$SHELL")"

if [ "$shellname" = "bash" ]; then
    if [ -f "$HISTFILE" ]; then
        hist_file="$HISTFILE"
    else
        hist_file="$HOME/.bash_history"
    fi
    entry="$("$fuzzy" < "$hist_file")"
elif [ "$shellname" = "zsh" ]; then
    if [ -f "$HISTFILE" ]; then
        hist_file="$HISTFILE"
    else
        hist_file="$HOME/.zsh_history"
    fi
    entry="$("$fuzzy" < "$hist_file")"
elif [ "$shellname" = "fish" ]; then
    hist_file="$HOME/.local/share/fish/fish_history"
    entry="$(grep "\- cmd: " "$hist_file" | cut -c 8- | "$fuzzy")"
fi

if [ -n "$entry" ]; then
    tmpfile=$(mktemp)
    echo "$entry" >> "$tmpfile"
    $EDITOR "$tmpfile"

    if [ -s "$tmpfile" ]; then
        $SHELL -c "$(cat "$tmpfile")"
    fi

    rm -- "$tmpfile"

    printf "Press any key to exit"
    read -r _
fi