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

前端 未结 4 1450
被撕碎了的回忆
被撕碎了的回忆 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:41

    I wasn't happy with the other answers so I spent a bit of time getting something more to my liking. The following will expand the dots when you hit (return) or (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
    

提交回复
热议问题