像 Zsh 中的 ls 键绑定这样的鱼

像 Zsh 中的 ls 键绑定这样的鱼

在 Fish shell 中,当您按下时,Alt+l它会很好地显示命令的输出ls。它会保留您在按下之前所写的命令。我想把它放在 zsh 中。

这是我非常古怪的尝试:

function myls {
    awk '{len = length($0); for(i=0;i<len;i++) printf "\b"}' <<< "$LBUFFER"
    zle push-input
    zle accept-line
    print $(ls --color=always --indicator-style=slash)
}
zle -N myls
bindkey -- '^[l' myls

我努力清除现有命令,最终使用了 hacky\b的。它在长行和多行上无法正常工作。

任何人都可以做得更好吗?

答案1

zle -R在 之后使用push-input可重新显示没有缓冲区的行。

在大多数情况下,我发现 zsh 的完成列表不需要小部件来运行 ls。它具有 ls 样式颜色和文件类型后缀。我有以下绑定,可以在任何上下文中完成文件补全,并进行长ls -l样式列表:

zstyle ":completion:file-complete::::" completer _files
zle -C file-complete complete-word _generic
zstyle ':completion:file-complete:*' file-list true
bindkey '^X^F' file-complete

答案2

感谢 @okapi 的输入,我想出了这个。

function myls {
    [[ "$CONTEXT" = cont ]] && return
    zle push-input
    zle -R
    zle accept-line
    print -n $(ls --color=always --indicator-style=slash)
}

zle -N myls
bindkey -- '^[l' myls

它好多了,但它不能像循环“for> ...”和“while> ...”那样使用延续,所以我现在只是将其返回。

相关内容