summaryrefslogtreecommitdiff
path: root/dotfiles/system/.profile.d/fzf.sh
blob: 02f6d8f6c8104daa402c42cea085e6e680d12644 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/sh
# Craig Jennings <c@cjennings.net>
# fuzzy find settings and utilities, sourced by .profile


# Set up fzf key bindings and fuzzy completion
source <(fzf --zsh)


### SETTINGS
export FZF_DEFAULT_OPTS='--height=70%'
export FZF_DEFAULT_COMMAND='rg --files'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
export FZF_DEFAULT_COMMAND='rg --files --no-ignore-vcs --hidden'


### NAVIGATION

# cdff - change directory find file
# change to the directory where the file resides.
cdff() {

    file=$(fzf +m -q "$1")
    dir=$(dirname "$file")
    cd "$dir" || exit
}

# CD to a directory with fzf
cdd () {
    destdir=$(find "${1:-.}" -path '*/\.*' -prune \
                   -o -type d -print 2> /dev/null | fzf +m) &&
        cd "$destdir"
}

### SYSTEM ADMIN

# Kill a process with fzf
kp () {
    pid=$(ps -ef | sed 1d | eval "fzf ${FZF_DEFAULT_OPTS} -m --header='[kill:process]'" | awk '{print $2}')

    if [ "x$pid" != "x" ]
    then
        echo "$pid" | xargs kill -${1:-9}
        kp
    fi
}

# list available packages, show info in preview, and install selection
yinstall() {
    yay -Slq | fzf --multi --preview 'yay -Si {1}' | xargs -ro yay -S --noconfirm
}


yinstall-skipverify() {
    yay -Slq | fzf --multi --preview 'yay -Si {1}' | xargs -ro yay -S --noconfirm --mflags --skipinteg
}

# list installed packages, show info in preview, and remove selection
yrm() {
    yay -Qq | fzf --multi --preview 'yay -Qi {1}' | xargs -ro yay -Rns
}

# find-in-file - usage: fif <searchTerm>
fif() {
    if [ ! "$#" -gt 0 ]; then echo "Need a string to search for!"; return 1; fi
    rg --files-with-matches --no-messages "$1" | fzf --preview "highlight -O ansi -l {} 2> /dev/null | rg --colors 'match:bg:yellow' --ignore-case --pretty --context 10 '$1' || rg --ignore-case --pretty --context 10 '$1' {}"
}

### CONVENIENCE

# Find a book in the calibre library and open it in the default application. 
# previously: find ~/books \( -iname \*.epub -o -iname \*.pdf -o -iname \*.djvu \) | fzf | xargs emacs
bk() {
    bkfile=$(find ~/sync/books/ \( -iname "*.pdf" -o -iname "*.epub" -o -iname "*.djvu" \) -print | fzf)
    if [ -n "$bkfile" ]; then
        xdg-open "$bkfile" &
    fi
}


# play the selected video in the sync/pomodoro directory with mpv
pomo() {
    pomo=$(find ~/sync/pomodoro/ \( -iname "*.webm" -o -iname "*.mkv" \) -print | fzf)
    if [ -n "$pomo" ]; then
        mpv "$pomo" &
    fi
}


# play the selected song in the music directory with mpg123
song() {
    songfile=$(find ~/music/ \( -iname "*.flac" -o -iname "*.mp3" -o -iname "*.ogg"  -o -iname "*.opus" \) -print | fzf)
    if [ -n "$songfile" ]; then
        mpv --no-video "$songfile" 
    fi
}