Bash cd to last argument with !:h modifier

Two shortcuts to cd into a long path you just typed as an argument:

cp file.txt /long/path/name
cd !$

!$ = the last argument of the previous command — here the target directory itself, so cd !$ lands directly in it.

cp file.txt /long/path/name/foo.txt
cd !$:h

:h is the head modifier: strip off the last component of the path (/long/path/name/foo.txt → /long/path/name), so this lands in the directory containing the file you just copied.

The pattern: any command’s arguments are reusable as !$ (last), !^ (first), !* (all); :h (head), :t (tail — the inverse, keep only the last component), :r (strip extension) modify them.