On a related note, I recommend using histverify in bash. Put this in your ~/.bashrc:
shopt -s histverify
This will cause bash to print out the command after expanding !$ or other history functions, and give you a chance to look at it before hitting enter again to actually run it. For me, the sanity check is worth the occasional extra key press. Want to make sure I'm running the cd foo
command, not the rm -rf foo
one...
I frequently use the Ctrl-R approach, as well as Alt-. (which is a good fit for the scenario you describe). I'll use !$ on occasion.
These are very useful general purpose techniques.
But to address your specific question:
Making a directory and cd'ing directly into it is such a common combination that it is useful to have a function to wrap it up..
function mcd {
local newdir='_mcd_command_failed_'
if [ -d "$1" ]; then # Dir exists, mention that
echo "$1 exists..."
newdir="$1"
else
if [ -n "$2" ]; then # We've specified a mode
command mkdir -p -m $2 "$1" && newdir = "$1"
else # Plain old mkdir
command mkdir -p "$1" && newdir="$1"
fi
fi
builtin cd "$newdir" # No matter what, cd into it
}
Usage: mcd thisismyfolder
cd this<TAB>