aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/fzhist
blob: ca1db630b5a8bef9a7402287b34880e618e83d7e (plain) (blame)
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
#!/usr/bin/env sh

# Description: Fuzzy find a command from history, edit in $EDITOR and run as a command
#              Currently supports only bash and fish history
#
# Shell: POSIX compliant
# Author: Arun Prakash Jana

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

if [ "$shellname" = "bash" ]; then
    hist_file="$HOME/.bash_history"
    entry="$(fzy < "$hist_file")"
elif [ "$shellname" = "fish" ]; then
    hist_file="$HOME/.config/fish/fish_history"
    entry="$(grep "\- cmd: " "$hist_file" | cut -c 8- | fzy)"
fi

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

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

    rm $tmpfile

    echo -n "Press any key to exit"
    read input
fi