13

所有的 shell 都理解这些命令:

$ cd .
$ cd ..

而zsh也会明白:

$ cd ...
$ cd ....

前提是你说:

$ alias -g ...='../..'
$ alias -g ....='../../..'

现在,当我开始输入时,如何让它正确完成制表符cd ..../<TAB>?我记得它是在 oh-my-zsh 中实现的,但我现在已经停止使用它。

如果它不仅适用于cd,比如我想执行,那也将不胜感激cat ..../a/b/..../c/d | less

4

4 回答 4

8

我对其他答案不满意,所以我花了一些时间来获得更多我喜欢的东西。当您点击时,以下内容将扩大点↵</kbd> (return) or ↹</kbd> (tab), not as you type the dots.

function expand-dots() {
    local MATCH
    if [[ $LBUFFER =~ '\.\.\.+' ]]; then
        LBUFFER=$LBUFFER:fs%\.\.\.%../..%
    fi
}

function expand-dots-then-expand-or-complete() {
    zle expand-dots
    zle expand-or-complete
}

function expand-dots-then-accept-line() {
    zle expand-dots
    zle accept-line
}

zle -N expand-dots
zle -N expand-dots-then-expand-or-complete
zle -N expand-dots-then-accept-line
bindkey '^I' expand-dots-then-expand-or-complete
bindkey '^M' expand-dots-then-accept-line
于 2017-01-02T01:13:07.917 回答
6

我为处理同样的问题所做的就是让 zsh 在../..我键入时 填写,...并且以这种方式扩展它是有意义的。它可能适合您(或不适合:-P):

if is-at-least 5.0.0 && [[ ! $UID -eq 0 ]]; then                                                                                                                             
  ## http://www.zsh.org/mla/users/2010/msg00769.html                                                                                                                       
  function rationalise-dot() {                                                                                                                                             
    local MATCH # keep the regex match from leaking to the environment                                                                                                   
    if [[ $LBUFFER =~ '(^|/| |      |'$'\n''|\||;|&)\.\.$' && ! $LBUFFER = p4* ]]; then                                                                                  
        #if [[ ! $LBUFFER = p4* && $LBUFFER = *.. ]]; then                                                                                                               
        LBUFFER+=/..                                                                                                                                                     
    else                                                                                                                                                                 
        zle self-insert                                                                                                                                                  
    fi                                                                                                                                                                   
}                                                                                                                                                                        
zle -N rationalise-dot                                                                                                                                                   
bindkey . rationalise-dot                                                                                                                                                
bindkey -M isearch . self-insert                                                                                                                                         
fi

我也有一个别名...,但它不是全局的。

请注意,我检查命令行是否以p4(Perforce 命令行工具)开头,并且在这种情况下不要乱用它,因为 Perforce 参数通常涉及文字...。如果您不使用p4,您显然可以删除该检查。

于 2014-05-05T12:00:42.190 回答
5

一个不错的选择是manydots-magic,它会扩展...../.., 等等,但它会很智能。有关更多详细信息,请参见上面的链接,但简要说明:

  • 它允许您使用单个Backspace(如果它是您键入的最后一件事)来恢复扩展。
    • 但它不会恢复显式键入../..
  • 您可以内联使用它,例如cd a/b/..../y/z.
  • 然而,当它没有意义时它不会扩展,例如git log branch...
  • 当它可能有意义时它扩展,但当你输入更多时它会恢复。例如
    • git diff ...->git diff ../..
    • git diff ...b-> git diff ...b(对于git diff ...branch
于 2017-02-16T05:28:20.137 回答
2

你必须使用compinit和使用_expand_alias作为完成者。这是一个例子:

zstyle ':completion:*' completer _complete _ignored _expand_alias
autoload -Uz compinit
compinit

_complete _ignored是 的默认设置completer,您可以将其设置为 only_expand_alias但补全仅适用于别名。

如果compinit已在您的 中配置~/.zshrc,那么您只需添加_expand_alias到列表中completer,例如:

zstyle ':completion:*' completer _expand _complete _ignored _approximate _expand_alias

默认情况下_expand_alias扩展全局别名和常规别名,如果您不想扩展常规别名,请设置:

zstyle ':completion:*' regular false

注意:这当然只适用于全局别名适用的情况。因此它们不会被扩展为整个路径的一部分,例如a/b/..../c/d

于 2014-05-04T14:02:51.257 回答