diff --git a/README.cn.md b/README.cn.md index f092d02..4d38654 100644 --- a/README.cn.md +++ b/README.cn.md @@ -325,7 +325,13 @@ zsh/fish 的补全系统是比较完善的,使用 `z foo` 就能触发补 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) diff --git a/README.md b/README.md index c4e7b6e..301fa76 100644 --- a/README.md +++ b/README.md @@ -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 `` after `z xxx`: ![](images/complete-2.png) diff --git a/z.lua b/z.lua index 00b6073..1897a21 100755 --- a/z.lua +++ b/z.lua @@ -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 @@ -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 .. ' ' @@ -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 diff --git a/z.lua.plugin.zsh b/z.lua.plugin.zsh index 8109dbe..f4bcbc0 100644 --- a/z.lua.plugin.zsh +++ b/z.lua.plugin.zsh @@ -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'