4 ms·
One history trick I use a lot that most people don't seem to know about is the !$ variable, which refers to the last argument of the previous command. It's basi
by svachalek 14y ago
One history trick I use a lot that most people don't seem to know about is the !$ variable, which refers to the last argument of the previous command. It's basically a bash pronoun. !! is the entire previous command which is occasionally useful with find:
find . -name foo
vi $(!!)
- spc476 14y agoand !N, where N is a number, is the Nth command in your history.
- mturmon 14y agoAnd !N:$ is the last argument of the N'th command in history. Even more useful, !vi:$ is the last argument of the last "vi" you did.
- zimpenfish 14y agoWoah, clearly I need to spend tomorrow re-reading the BASH manual.
- barrkel 14y agoI find alt-. to be more usable - I know for sure what it will do, as I see the effect of the expansion immediately.
- jacobparker 14y agosudo !! Are you feeling lucky?
- mturmon 14y agoA usage pattern with !$ is: % mkdir foo % chmod g+s !$ % ls -ld !$ # (make sure it's correct) % cd !$
- jdf 14y agoAlternatively, you can add ':p' to the third line. This will print out the command rather than executing it. Additionally, it's also added to your bash history, so you can add it by pressing the up arrow. $ chmod 755 foo $ cd !$:p cd foo $ cd foo There are some similar "tricks" here: https://news.ycombinator.com/item?id=5337558 https://news.ycombinator.com/item?id=5337558
- mturmon 14y agoI have found "magic-space" more useful than ":p". This auto-expands history substitutions (like !$) when you press the space bar. You can bind it to the space key in tcsh or bash. It also works for !!, !vi, and other substitutions. So, when you press ! $ <space>, it expands out to "foo" in place, so you can be sure that's what you wanted, and edit it in place. I think :p predates magic-space. They're both decades old at this point though.
- dllthomas 14y ago$_ is a variable for the same thing. The only difference winds up being the effect on the current line's result in your history: !$ is replaced while $_ stays $_ and so its meaning will change if you run the same command later, which can be what you want or not, depending.
- scott_karana 14y agoYou're right in this specific case, but there are other useful ! arguments, so it's worth learning both. It's the "Event Designators" in the bash manpage for those who are interested.
- dllthomas 14y agoOh, I didn't mean "You should use $_ instead", just "$_ is also useful." As I tried to indicate, you should ideally use whichever will make more sense in your history later. If the command you're typing should always be applied to that file, which happened to be the last argument of the previous command this time, then use !$; if the command you're typing should always be applied to the last argument of the previous command, and it just happened to be that file this time, use $_.
- wildwood 14y agoAgreed, one of my favorites. I do this so many times every day: hg diff -b <filename> hg commit -m "comments" !$
- gknoy 14y agoThank you for pointing out that I can do per-file commits. I didn't know that before! I use 'hg st' so much that I've aliased it as 'st'. It's just about muscle memory now.
- banachtarski 14y agoI would do vi `!!` instead since the backticks are right next to the !'s already.
- oakenshield 14y agoalt+. does the same thing as well, and is slightly easier to type.
- deleted 14y ago[deleted]
- martininmelb 14y ago$ Make me a sandwich What? Make it yourself $ sudo !! Okay
- oneandoneis2 14y agoAgreed - I recently devoted an entire blog post to making the most of !$, it's that useful to me: http://geekblog.oneandoneis2.org/index.php/2013/04/03/protip-enable http://geekblog.oneandoneis2.org/index.php/2013/04/03/protip...