(Old news, but in case someone else lands here looking for this, like I just did)
OP seemed to be looking for !#[X][-Y]
zsh syntax
where X and Y are indexes for shell words in history event for current line
X and Y are assumed to refer to the start or end of line -
respectively - when omitted
(keep in mind that end of line in this context is actually the occurrence of the !#[X][-Y]
event call within the line)
this gives us the following variants:
in direct response to example given:
mkdir tmp && cd !#2
or, taking it a step further:
alias mktmpd='mkdir -p tmp /tmp/${PWD:t}; cd ${(@)${~:-${=^:-!#2-3}(N/)}[1]}; pwd; ls'
the path /tmp/${PWD:t}
would be created also and act as an alternative to cd
to if for some reason ./tmp
could not be created (which would have failed silently)
this assumes user can mkdir
in /tmp
and is willing to let some useless paths be generated (and left untouched) when ./tmp
is perfectly usable and in fact chosen
potential problem arises when /tmp/${PWD:t}
already exists and its contents are unrelated and not be disturbed, hence the pwd; ls
aliasing helps with use cases such as somecommand; mktmpd
where normally the !#2-3 would no longer refer to the appropriate parameters tmp
/tmp/${PWD:t}
Note however that I am decidedly not advocating for this use case as an especially helpful one (or even its fitness as an example, really - but I wanted to point out the alias particularity since I have yet to see it mentioned elsewhere)