Multi-dot paths in zsh, like `cd …`

前端 未结 4 1506
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 23:54

All shells understand these commands:

$ cd .
$ cd ..

And zsh will also understand:

$ cd ...
$ cd ....

Provide

4条回答
  •  花落未央
    2021-02-06 00:34

    What I did to to deal with the same problem is to just let zsh fill in ../.. when I type ... and it makes sense to expand it in that way. It may suit you (or not :-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
    

    I also have an alias for ..., but it is not global.

    Notice I check if the command line starts with p4 (the Perforce command line tool) and do not mess with it in that case, as Perforce arguments often involve literal .... If you do not use p4 you can obviously remove that check.

提交回复
热议问题