by Normille · 5y ago · 0 comments
Another neat short hand is !$ which is an alias for the last argument of the previous command. This can be handy if one creates a new directory and wants to change into it without typing the the directory again. So commands would be
mkdir -p make/new/directory
cd !$
You've no idea how many years I've been thinking "This is Linux fer gawd's sake. Surely there must be some easy way to make a new directory and 'cd' to it in a single command!" But was never able to find or guess the right incantation.mkcd() { mkdir -vp "$1" && cd "$1"; }
mkcd() { mkdir -p "$@" && cd "$_"; }
Can someone please detail the differences between $! and $_? (Apologies if I'm missing something obvious)
$ echo "test" > /dev/null && echo !$
/dev/null
$ echo "test" > /dev/null && echo $_
testI would not that !$ is a bash-ism, whereas $_ works at least on zsh and ksh as well as bash.
alias tcd='a=`date +%s`;mkdir "$a" && cd "$a"'Further, holding Alt followed by numeric argument followed by dot, gives you an argument at a specific position. For example, Alt + 1, Alt + . copies the first argument.
!-1:gs/hello/world
which runs the last command with all instances of hello replaced by world. !-1 can be any so called event designator, for instance !n to refer to any specific line number from history or !string for last command containing a particular string.
Seriously people, read the man page for bash. Your shell has so many cool features.
Real programmers use set -o vi
# create a new directory and enter it
function mkd() {
mkdir -p "$@" && cd "$_";
}
And use it like: mkd fooWhat does: "mkd foo bar" do? It creates both directories, and cd's into "bar".
Might be worth checking that only one argument has been passed, or use "$*" which instead would treat all arguments as one parameter, and "mkd foo bar" would create a "foo bar" directory instead (and cd to it)
YMMV.
It's also a good way to "learn by doing", which works for many.
¹ https://github.com/ohmyzsh/ohmyzsh/blob/master/lib/functions...
Wrote about it here: https://henrikwarne.com/2018/08/11/my-favorite-command-line-...
"Insert last argument to the previous command (the last word of the previous history entry). With a numeric argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last word (or the word specified by the argument to the first call) of each line in turn."
https://tiswww.case.edu/php/chet/readline/readline.html#IDX9...
Its sibling is `yank-nth-arg`, bound to M-C-y. So if you want the 2nd argument from the last command, you can press M-1 followed by M-C-y (yeah, arguments are indexed from 0)...
https://tiswww.case.edu/php/chet/readline/readline.html#IDX9...
That's great set of tips BTW.
FWIW, zsh without oh-my-zsh can be configured to do too this with "bindkey ' ' magic-space".
enter ()
{
mkdir -p "$1";
cd "$1"
}And !$ is documented here: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash....
!! -> last whole command
!!:1 -> first argument of last command
!!:$ -> last argument of last command
!$ -> abbreviation for above