9 ms·
I upvoted for this alone: 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 n
by Normille 6y ago
I upvoted for this alone:
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.
- rnestler 6y agoI hava a mkcd alias to create a directory and changing into it.
- barrkel 6y agoI have one called `enter`. enter () { mkdir -p "$1"; cd "$1" }
- lolive 6y agoTyping Esc then . does the trick too.
- cerved 6y agowhat, what?
- gadiyar 6y agoI didn't know this. Really neat!
- teptoria 6y agoWow amazing! Thanks :)
- itsmarsu 6y agoThat's just magical
- thomasahle 6y agoHow does that work?
- alex_smart 6y agoAlt + . does the same thing as well, with the added advantage that you can keep pressing alt and then hit . several times to go through all the "last arguments" in your history one by one. 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.
- seesawtron 6y agoCool one. "Ctrl + R" also does the same thing to search through past commands and then "Tab" to select the command you are looking for.
- alex_smart 6y agoMay favorite bash "trick" is: !-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.
- tikkabhuna 6y agoAnd if you only have a single instance to replace, you can use ^old^new^ $ echo "hello hello hello world" hello hello hello world $ ^hello^bonjour^ echo "bonjour hello hello world" bonjour hello hello world
- alex_smart 6y agoI know that this expansion exists, but never use it in practice. Why? The last command is more general, you can just drop the g from the command I wrote to only replace the first occurrence. Besides, :[g]s/old/new should already be familiar to anyone who has used vim, so that is almost like having to learn zero new things.
- dcassett 6y ago
- nmfisher 6y agoYou just blew my mind.
- madacol 6y agoMore shortcuts like that https://stackoverflow.com/questions/38176514/re-run-previous-command-with-different-arguments/55067306#55067306 https://stackoverflow.com/questions/38176514/re-run-previous...
- mraza007 6y agoI have been looking for this Thanks for sharing :)
- emmelaich 6y agoOnly in emacs editing mode. Real programmers use set -o vi
- stjohnswarts 6y agoI use that on test machines a lot and had put it in bashrc and heard some cussing from other devs so now I just have to type it every time I spend more than acouple seconds in a terminal :) .
- rvoje 6y agoThis is nifty: https://github.com/wting/autojump https://github.com/wting/autojump
- joseluisq 6y agoI got stuck with exactly that use case today and trying to figure out how I can enter directly to my dir without typing it again. Bash is no so neat at completions like Fish for example. However !$ does the thing and keeps my fingers healthy. That's great set of tips BTW.
- I_complete_me 6y agoIn zsh, you use -> % take <directory> which makes the dir and cd's into it.
- JNRowe 6y agoThat isn't standard zsh behaviour. I suspect it comes from oh-my-zsh¹, but wouldn't be surprised if it isn't a bunch of the configuration bundles. ¹ https://github.com/ohmyzsh/ohmyzsh/blob/master/lib/functions.zsh https://github.com/ohmyzsh/ohmyzsh/blob/master/lib/functions...
- Normille 6y agoThe bash version works in zsh too.
- myicgygc843467 6y agoIs !$ the same as $_ ?
- johncoltrane 6y agohttps://stackoverflow.com/questions/41385015/what-is-bang-dollar-in-bash https://stackoverflow.com/questions/41385015/what-is-bang-do...
- tome 6y agoLooks like !$ is a readline feature and $_ is a shell variable?
- yrro 6y agoYes. $_ is documented here: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Bash-Variables https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.... And !$ is documented here: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Word-Designators https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash....
- mturmon 6y agoI don't think !$ is properly understood as a readline feature. It's a sh/bash abbreviation. !! -> last whole command !!:1 -> first argument of last command !!:$ -> last argument of last command !$ -> abbreviation for above
- W0lf 6y agoFor this exact use case I defined the following many years ago in my bash profile: # create a new directory and enter it function mkd() { mkdir -p "$@" && cd "$_"; } And use it like: mkd foo
- mfontani 6y agoBeware that using "$@" there might not be what you want, as it passes space-separated arguments to mkdir. What 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.
- dvfjsdhgfv 6y agoAll Bash manuals and tutorials should start from explaining this issue. And I really mean it. I can't remember how many times I was bitten by it until I finally read it up.
- samus 6y agoI still have to read it up from time to time even though I know it already. The difference is huge.
- mfontani 6y agoI feel one should also install shellcheck, and follow it religiously while looking up every warning they get - to understand what it's about, why it's a problem, and why the solution / "correct" incantation is so. It's also a good way to "learn by doing", which works for many.
- W0lf 6y agoThanks a lot for pointing this out. I will happily fix this now!
- deleted 6y ago[deleted]
- RhysU 6y agoI have an "mkcd" function in my startup files: mkcd() { mkdir -vp "$1" && cd "$1"; }
- cutthegrass2 6y ago+1, I do the same.
- bunsenhoneydew 6y agoMe too
- gpspake 6y agoYep, first thing I add to my dotfiles when I don't have it haha
- alsetmusic 6y agoWait, now I'm confused. I have: mkcd() { mkdir -p "$@" && cd "$_"; } Can someone please detail the differences between $! and $_? (Apologies if I'm missing something obvious)
- tyingq 6y ago$ echo "test" > /dev/null && echo !$ /dev/null $ echo "test" > /dev/null && echo $_ test
- karatinversion 6y agoYou're thinking of $_ and !$, and I think they are equivalent - $_ is explicitly the last argument of the previous command, whereas !$ is a history expansion command. ! starts a new history expansion command, and $ is a word designator for the last word of the selected history entry. To quote man bash, " If a word designator is supplied without an event specification, the previous command is used as the event." I would not that !$ is a bash-ism, whereas $_ works at least on zsh and ksh as well as bash.
- deleted 6y ago
- unvs 6y agoYes, that's a good one! My way of remembering is "bang for my buck" -> !$
- henrik_w 6y agoescpae-dot (.) also works for this: "It also works to use !$ instead of escape-dot, but that is slightly harder to type, so I don’t use that anymore." Wrote about it here: https://henrikwarne.com/2018/08/11/my-favorite-command-line-shortcuts/ https://henrikwarne.com/2018/08/11/my-favorite-command-line-...
- loulouxiv 6y agoWhich you can type as ALT-.
- yrro 6y agoThis is the `yank-last-arg` readline comment. Bound to M-. and M-_ by default. "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#IDX95 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#IDX94 https://tiswww.case.edu/php/chet/readline/readline.html#IDX9...
- mfontani 6y agomkdir -p foo && cd $_
- mlang23 6y agoAlso take a look at M-. (Meta+dot) which does the same thing while interactively editing a command...
- llbeansandrice 6y agoI know this is a bash thread but zsh/oh-my-zsh will replace !$ with the actual argument when you type a space so you can see and edit it before executing the command.
- JNRowe 6y agobash supports the same, also named magic-space. For bash you can chuck something like "Space: magic-space" in your $INPUTRC, although you'd probably want a guard(see Readline Conditional Constructs in the manpage). FWIW, zsh without oh-my-zsh can be configured to do too this with "bindkey ' ' magic-space".