-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh-select-with-lf.plugin.zsh
38 lines (33 loc) · 1.33 KB
/
zsh-select-with-lf.plugin.zsh
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
(( ! $+commands[lf] )) && return
(( ! ${+ZSH_SELECT_WITH_LF_FILE_BIND} )) && typeset -g ZSH_SELECT_WITH_LF_FILE_BIND='^x^l'
(( ! ${+ZSH_SELECT_WITH_LF_DIR_BIND} )) && typeset -g ZSH_SELECT_WITH_LF_DIR_BIND='^xl'
__select_with_lf_file() {
action=${1:-print-selection}
local current=${LBUFFER##* }
local initial_directory=${PWD}
[[ -n ${current} ]] && {
current=${current%%[[:space:]]#}
[[ ${current} == '~'* ]] && current=${HOME}${current#'~'}
}
if [[ -d ${current} ]]; then
initial_directory=${current}
elif [[ -f ${current} ]]; then
initial_directory=${current%/*}
elif [[ -d ${current%/*} ]]; then
initial_directory=${current%/*}
fi
selection="$(command lf -${action} ${initial_directory})"
[[ -z ${selection} ]] && return 1
[[ ${selection} == $HOME/* && ${selection} != *" "* ]] && selection="~${selection#$HOME}"
[[ ${selection} == *" "* ]] && selection="\"${selection}\""
[[ -n ${current} ]] && LBUFFER="${LBUFFER% *} "
LBUFFER="$LBUFFER$selection"
}
__select_with_lf_dir() {
__select_with_lf_file print-last-dir
}
autoload -U __select_with_lf_file __select_with_lf_dir
zle -N __select_with_lf_file
zle -N __select_with_lf_dir
bindkey $ZSH_SELECT_WITH_LF_FILE_BIND __select_with_lf_file
bindkey $ZSH_SELECT_WITH_LF_DIR_BIND __select_with_lf_dir