Skip to content

Commit

Permalink
Merge pull request #185 from kang8/feature/support-fzf-for-zsh-tab
Browse files Browse the repository at this point in the history
ZSH: TAB complete using fzf
  • Loading branch information
skywind3000 authored Jul 13, 2023
2 parents 71bae7f + 86cb43a commit 7f2bfcb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
8 changes: 7 additions & 1 deletion README.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,就能把匹配的路径列出来:

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

Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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`:
![](images/complete-2.png)
Expand Down
38 changes: 37 additions & 1 deletion z.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2284,6 +2284,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
Expand Down Expand Up @@ -2321,7 +2345,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 .. ' '
Expand All @@ -2337,6 +2361,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
Expand Down
2 changes: 1 addition & 1 deletion z.lua.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 7f2bfcb

Please sign in to comment.