brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Alt+. pastes the last argument of your previous command

Shell
~/tips/tip-shell-alt-dot-last-argument

cd Alt+.

Pastes the last word of your previous command at the cursor, so you do not retype paths.

Half of my command lines start with a path I just typed one line earlier. Typing it again wastes fingers.

In Bash, Alt+. inserts the last word of the previous command at the cursor. Press it again and it walks further back through your history, one command per press. The Readline name for it is yank-last-arg.

$ mkdir -p ~/projects/blog-drafts
$ cd Alt+.
$ cd ~/projects/blog-drafts

There is a typed version too: !$ expands to the same last word. It works in a one-liner like cd !$, but the key lets you see the result before you press Enter.

The caveat is the keyboard. On a Mac terminal, Alt is the Option key and it only works as Meta when you turn on “Use Option as Meta key” in the Terminal settings. Without that, Option+. types a different character. Zsh has the same widget on the same key, so the habit travels.

And remember it takes the last word. If the previous command ended with a redirect like > out.txt, that is what you get.

Further reading: Bash manual: Commands For Manipulating The History, Bash manual: Word Designators, On this site: sudo !! reruns your last command with sudo, On this site: cp file{,.bak} makes a backup copy without typing the name twice

More Shell tips zmv -n '(*).log' '$1.txt' zoxide query -ls project f=$(mktemp --suffix=.json) shopt globskipdots echo $EPOCHSECONDS All Shell tips (78) All topics

Link