mirror of
https://github.com/skywind3000/z.lua
synced 2026-03-14 11:49:48 +00:00
ZSH: TAB complete using fzf
Tested in Linux(ArchLinux) and MacOS Close: #99, #77
This commit is contained in:
@@ -325,7 +325,13 @@ zsh/fish 的补全系统是比较完善的,使用 `z foo<tab>` 就能触发补
|
||||
eval "$(lua /path/to/z.lua --init bash enhanced once echo fzf)"
|
||||
```
|
||||
|
||||
然后你在 bash 中,输入部分关键字后按 tab,就能把匹配的路径列出来:
|
||||
如果你想在 zsh 中使用 fzf 补全,初始化时在 `--init` 后追加 `fzf` 关键字:
|
||||
|
||||
```zsh
|
||||
eval "$(lua /path/to/z.lua --init zsh enhanced once echo fzf)"
|
||||
```
|
||||
|
||||
然后你在 bash/zsh 中,输入部分关键字后按 tab,就能把匹配的路径列出来:
|
||||
|
||||

|
||||
|
||||
|
||||
@@ -90,7 +90,7 @@ z -b foo bar # replace foo with bar in cwd and cd there
|
||||
|
||||
eval "$(lua /path/to/z.lua --init zsh)"
|
||||
|
||||
Options like "enhanced" and "once" can be used after `--init` too. It can also be initialized from "skywind3000/z.lua" with your zsh plugin managers (antigen / oh-my-zsh).
|
||||
Options like "enhanced", "once" and "fzf" can be used after `--init` too. It can also be initialized from "skywind3000/z.lua" with your zsh plugin managers (antigen / oh-my-zsh).
|
||||
|
||||
**NOTE**: for wsl-1 users, `lua-filesystem` must be installed.
|
||||
|
||||
@@ -380,6 +380,12 @@ Bash is not as powerful as zsh/fish, so we introduced fzf-completion for bash, i
|
||||
eval "$(lua /path/to/z.lua --init bash enhanced once echo fzf)"
|
||||
```
|
||||
|
||||
If you want use fzf completion in zsh, initalize your z.lua and append `fzf` keyword after `--init`:
|
||||
|
||||
```zsh
|
||||
eval "$(lua /path/to/z.lua --init zsh enhanced once echo fzf)"
|
||||
```
|
||||
|
||||
Then press `<tab>` after `z xxx`:
|
||||
|
||||

|
||||
|
||||
38
z.lua
38
z.lua
@@ -2266,6 +2266,30 @@ if [ "${+functions[compdef]}" -ne 0 ]; then
|
||||
fi
|
||||
]]
|
||||
|
||||
local script_fzf_complete_zsh = [[
|
||||
if command -v fzf >/dev/null 2>&1; then
|
||||
# To redraw line after fzf closes (printf '\e[5n')
|
||||
bindkey '\e[0n' kill-whole-line
|
||||
_zlua_zsh_fzf_complete() {
|
||||
local list=$(_zlua -l ${words[2,-1]})
|
||||
|
||||
if [ -n "$list" ]; then
|
||||
local selected=$(print $list | ${=zlua_fzf} | sed 's/^[0-9,.]* *//')
|
||||
|
||||
if [ -n "$selected" ]; then
|
||||
cd ${selected}
|
||||
printf '\e[5n'
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
if [ "${+functions[compdef]}" -ne 0 ]; then
|
||||
compdef _zlua_zsh_fzf_complete _zlua > /dev/null 2>&1
|
||||
compdef ${_ZL_CMD:-z}=_zlua
|
||||
fi
|
||||
fi
|
||||
]]
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- initialize bash/zsh
|
||||
@@ -2303,7 +2327,7 @@ function z_shell_init(opts)
|
||||
end
|
||||
print(script_complete_bash)
|
||||
if opts.fzf ~= nil then
|
||||
fzf_cmd = "fzf --nth 2.. --reverse --info=inline --tac "
|
||||
local fzf_cmd = "fzf --nth 2.. --reverse --info=inline --tac "
|
||||
local height = os.environ('_ZL_FZF_HEIGHT', '35%')
|
||||
if height ~= nil and height ~= '' and height ~= '0' then
|
||||
fzf_cmd = fzf_cmd .. ' --height ' .. height .. ' '
|
||||
@@ -2319,6 +2343,18 @@ function z_shell_init(opts)
|
||||
print(once and script_init_zsh_once or script_init_zsh)
|
||||
end
|
||||
print(script_complete_zsh)
|
||||
if opts.fzf ~= nil then
|
||||
local fzf_cmd = "fzf --nth 2.. --reverse --info=inline --tac "
|
||||
local height = os.environ('_ZL_FZF_HEIGHT', '35%')
|
||||
if height ~= nil and height ~= '' and height ~= '0' then
|
||||
fzf_cmd = fzf_cmd .. ' --height ' .. height .. ' '
|
||||
end
|
||||
local flag = os.environ('_ZL_FZF_FLAG', '')
|
||||
flag = (flag == '' or flag == nil) and '+s -e' or flag
|
||||
fzf_cmd = fzf_cmd .. ' ' .. flag .. ' '
|
||||
print('zlua_fzf="' .. fzf_cmd .. '"')
|
||||
print(script_fzf_complete_zsh)
|
||||
end
|
||||
elseif opts.posix ~= nil then
|
||||
if prompt_hook then
|
||||
local script = script_init_posix
|
||||
|
||||
@@ -21,7 +21,7 @@ fi
|
||||
|
||||
export _ZL_FZF_FLAG=${_ZL_FZF_FLAG:-"-e"}
|
||||
|
||||
eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced)"
|
||||
eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced fzf)"
|
||||
|
||||
if [[ -z "$_ZL_NO_ALIASES" ]]; then
|
||||
alias zz='z -i'
|
||||
|
||||
Reference in New Issue
Block a user