All shells understand these commands:
$ cd .
$ cd ..
And zsh will also understand:
$ cd ...
$ cd ....
Provided you say:
$ alias -g ...='../..'
$ alias -g ....='../../..'
Now, how can I make it do proper tab-completion when I've started typing cd ..../<TAB>
? I recall it was implemented in oh-my-zsh but I've stopped using it now.
It would also be appreciated if it would work not only for cd
, say I want to execute cat ..../a/b/..../c/d | less
.
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
A good option is manydots-magic, which expands ...
into ../..
, etc. but does it intelligently. See the link above for more details, but briefly:
../..
cd a/b/..../y/z
.git log branch...
git diff ...
-> git diff ../..
git diff ...b
-> git diff ...b
(for git diff ...branch
)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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With