What is a fish shell equivalent of mkdir -p foo/bar/baz/quux && cd $_
?
I know about $history[1]
, but here I need only last argument of the previous command.
As suggested in another thread Alt+. cycles arguments from previous commands at cursor input:
mkdir -p ~/fish/previous/arg/demo
cd
# ^ hit Alt + .
cd ~/fish/previous/arg/demo
fish doesn't support a last argument variable unfortunately.
An efficient interactive way to do this is to make the directory:
> mkdir -p foo/bar/baz/quux
Then type cd and the first character of the path.
> cd f
At this point fish will probably autosuggest the whole path. If not you can press alt-up to do a history token search and it will certainly find it.
A scripting way to do it would be:
set path foo/bar/baz/quux && mkdir -p $path && cd $path
Fish has no shortcut for this, you'll have to repeat that argument.
Or if it's for interactive use, press alt-up to cycle through older arguments.
I also found the lack of $_
annoying. A (somewhat hacky) solution is to create a function like this:
function cdl
echo "$history[1]" | read --array --tokenize result
cd "$result[-1]"
end
So now you can type cdl
instead of cd $_
to change to the directory you just created.
EDIT: Added correct quoting and --tokenize
which was added in this PR in version 3.1. Now it works correctly with directory names that contain spaces.