Skip to content

Commit 7f2bfcb

Browse files
authored
Merge pull request #185 from kang8/feature/support-fzf-for-zsh-tab
ZSH: TAB complete using fzf
2 parents 71bae7f + 86cb43a commit 7f2bfcb

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

README.cn.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,13 @@ zsh/fish 的补全系统是比较完善的,使用 `z foo<tab>` 就能触发补
325325
eval "$(lua /path/to/z.lua --init bash enhanced once echo fzf)"
326326
```
327327

328-
然后你在 bash 中,输入部分关键字后按 tab,就能把匹配的路径列出来:
328+
如果你想在 zsh 中使用 fzf 补全,初始化时在 `--init` 后追加 `fzf` 关键字:
329+
330+
```zsh
331+
eval "$(lua /path/to/z.lua --init zsh enhanced once echo fzf)"
332+
```
333+
334+
然后你在 bash/zsh 中,输入部分关键字后按 tab,就能把匹配的路径列出来:
329335

330336
![](images/complete-2.png)
331337

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ z -b foo bar # replace foo with bar in cwd and cd there
9090

9191
eval "$(lua /path/to/z.lua --init zsh)"
9292

93-
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).
93+
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).
9494

9595
**NOTE**: for wsl-1 users, `lua-filesystem` must be installed.
9696

@@ -380,6 +380,12 @@ Bash is not as powerful as zsh/fish, so we introduced fzf-completion for bash, i
380380
eval "$(lua /path/to/z.lua --init bash enhanced once echo fzf)"
381381
```
382382

383+
If you want use fzf completion in zsh, initalize your z.lua and append `fzf` keyword after `--init`:
384+
385+
```zsh
386+
eval "$(lua /path/to/z.lua --init zsh enhanced once echo fzf)"
387+
```
388+
383389
Then press `<tab>` after `z xxx`:
384390

385391
![](images/complete-2.png)

z.lua

+37-1
Original file line numberDiff line numberDiff line change
@@ -2284,6 +2284,30 @@ if [ "${+functions[compdef]}" -ne 0 ]; then
22842284
fi
22852285
]]
22862286

2287+
local script_fzf_complete_zsh = [[
2288+
if command -v fzf >/dev/null 2>&1; then
2289+
# To redraw line after fzf closes (printf '\e[5n')
2290+
bindkey '\e[0n' kill-whole-line
2291+
_zlua_zsh_fzf_complete() {
2292+
local list=$(_zlua -l ${words[2,-1]})
2293+
2294+
if [ -n "$list" ]; then
2295+
local selected=$(print $list | ${=zlua_fzf} | sed 's/^[0-9,.]* *//')
2296+
2297+
if [ -n "$selected" ]; then
2298+
cd ${selected}
2299+
printf '\e[5n'
2300+
fi
2301+
fi
2302+
}
2303+
2304+
if [ "${+functions[compdef]}" -ne 0 ]; then
2305+
compdef _zlua_zsh_fzf_complete _zlua > /dev/null 2>&1
2306+
compdef ${_ZL_CMD:-z}=_zlua
2307+
fi
2308+
fi
2309+
]]
2310+
22872311

22882312
-----------------------------------------------------------------------
22892313
-- initialize bash/zsh
@@ -2321,7 +2345,7 @@ function z_shell_init(opts)
23212345
end
23222346
print(script_complete_bash)
23232347
if opts.fzf ~= nil then
2324-
fzf_cmd = "fzf --nth 2.. --reverse --info=inline --tac "
2348+
local fzf_cmd = "fzf --nth 2.. --reverse --info=inline --tac "
23252349
local height = os.environ('_ZL_FZF_HEIGHT', '35%')
23262350
if height ~= nil and height ~= '' and height ~= '0' then
23272351
fzf_cmd = fzf_cmd .. ' --height ' .. height .. ' '
@@ -2337,6 +2361,18 @@ function z_shell_init(opts)
23372361
print(once and script_init_zsh_once or script_init_zsh)
23382362
end
23392363
print(script_complete_zsh)
2364+
if opts.fzf ~= nil then
2365+
local fzf_cmd = "fzf --nth 2.. --reverse --info=inline --tac "
2366+
local height = os.environ('_ZL_FZF_HEIGHT', '35%')
2367+
if height ~= nil and height ~= '' and height ~= '0' then
2368+
fzf_cmd = fzf_cmd .. ' --height ' .. height .. ' '
2369+
end
2370+
local flag = os.environ('_ZL_FZF_FLAG', '')
2371+
flag = (flag == '' or flag == nil) and '+s -e' or flag
2372+
fzf_cmd = fzf_cmd .. ' ' .. flag .. ' '
2373+
print('zlua_fzf="' .. fzf_cmd .. '"')
2374+
print(script_fzf_complete_zsh)
2375+
end
23402376
elseif opts.posix ~= nil then
23412377
if prompt_hook then
23422378
local script = script_init_posix

z.lua.plugin.zsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fi
2121

2222
export _ZL_FZF_FLAG=${_ZL_FZF_FLAG:-"-e"}
2323

24-
eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced)"
24+
eval "$($ZLUA_EXEC $ZLUA_SCRIPT --init zsh once enhanced fzf)"
2525

2626
if [[ -z "$_ZL_NO_ALIASES" ]]; then
2727
alias zz='z -i'

0 commit comments

Comments
 (0)