-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfzf-repl.bash
executable file
·68 lines (54 loc) · 2.29 KB
/
fzf-repl.bash
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
#!/usr/bin/bash -i
# https://github.com/pl643/fzf-repl/fzf-repl.bash
# bash REPL (Read/Evaluate/Print/Loop) using fzf to edit/execute/lazygit files/directories/history
# suggested alias:
# alias fr='~/repo/fzf-repl/fzf-repl.bash'
set +o history
fzf_height=6
# FZF_DEFAULT_COMMAND --prompt doesn't support ansi color, our REPL prompt will be colorless
PS1="\u@\h:\w$ "
# defaults editor to neovim
[ -z "$EDITOR" ] || export EDITOR="nvim"
# display fzf bindings
menu() {
printf "
fzf-repl
fzf bindings:
<Enter> execute/eval fzf query string
C-e edit selected fzf item
C-f fc (fix command) selected fzf item and execute (sourced)
C-g start lazygit in the directory of selected fzf item
C-x execute/eval selected fzf item is file / cd if a directory
alias m='menu/bindings' displays this menu
alias x='exit' exit fzf-repl
fzf_height=%d this shell variable is fzf's height. Minimum is 3.
" $fzf_height
}
alias m=menu
alias x=exit
menu
fzf_repl_history="$HOME/.fzf_repl_history"
fzf_fc="$HOME/.fzf_fc"
# https://learnbyexample.github.io/learn_perl_oneliners/one-liner-introduction.html - for uniq lines without sorting
export FZF_DEFAULT_COMMAND="([ -f $fzf_repl_history ] && tac $fzf_repl_history; tac ~/.bash_history; find . -type f -or -type d) | perl -MList::Util=uniq -e 'print uniq <>'"
while [ true ]; do
eval 'prompt="${PS1@P}";' 2> /dev/null
eval export FZF_DEFAULT_OPTS=\'--height=$fzf_height --info=inline --layout=reverse --prompt \"$prompt\"\'
selected=$(fzf --print-query --bind \
'ctrl-f:execute(echo {} > ~/.fzf_fc; nvim ~/.fzf_fc > /dev/tty)+abort,ctrl-e:execute([ -f {} ] && (echo {} > ~/.fzf_repl_history; nvim {} > /dev/tty))+abort,ctrl-g:execute(echo {} >> ~/.fzf_repl_history; cd $(dirname {}); lazygit > /dev/tty)+abort,ctrl-x:execute(echo {}; eval {})+abort'\
)
if [ -f "$fzf_fc" ] ; then
query=$(cat "$fzf_fc")
rm "$fzf_fc"
else
query=$(echo "$selected" | head -1)
fi
echo "$prompt$query"
# if query is a directory, repl will cd into it
if [ -d "$query" ]; then
cd $query
else
eval "$query"
fi
[ -z "$query" ] || echo "$query" >> $fzf_repl_history
done