Last Updated: July 25, 2023
·
5.133K
· robincurry

Bang Cash (!$)

This is one I use a lot - so much that I sometimes assume everyone knows it.

Using !$ from the shell will expand to the last argument from the previous command.

So, let's say you are looking at a document

$ cat some/really/long/path/to/readme.md

and then you say to yourself, 'self, I need to edit this file.' Bang Cash to the rescue. Just do:

$ vim !$    # expands to vim some/really/long/path/to/readme.md

Or say you are editing a file

$ vim some/really/long/path/to/index.html

and now you want to see it in the browser

$ open !$

Bang! Cash! You are so money and you don't even know it.

15 Responses
Add your response

I use the alt-. version (esc, . for you Mac people with jacked up alt keys).

Edit: only works in emacs command mode.

over 1 year ago ·

Also, I think $_ is a minor improvement since it doesn't require you to hit return a second time.

over 1 year ago ·

@seven1m - I think hitting return a second time is necessary in zsh (tab works too). Not necessary in bash. Don't know about other shells.

Also, "Cash Underscore!" just doesn't have the same ring to it. ;)

over 1 year ago ·

You can also use !foo where "foo" is a command (i.e. "git") and bash will look through your history and run the last previous instance of "foo".

Example:

> git ls-files
> vim Gemfile
> bundle
> !git

The last command would run git ls-files again.

over 1 year ago ·

Thanks @jakebellacera. Good one. That reminded me to share this related tip: https://coderwall.com/p/wx-cag

over 1 year ago ·

There's a bunch of these "bang arguments":

  • !:0 is the previous command name
  • !^, !:2, !:3, …, !$ are the arguments of the previous command
  • !* is all the arguments
  • !$ is the last argument. You can also use $_ to get the last argument of the previous command.

And more, see this good link

over 1 year ago ·

great tip! ever try fish shell lets you work with history in a snap http://fishshell.com/

over 1 year ago ·

Yeah @davidchase03 - but this won't work in fish

over 1 year ago ·

So, sorta like using !! to execute the previous command.

vim fileWithElevatedPermissions
sudo !!

over 1 year ago ·
over 1 year ago ·

I use this all the time. Voted it up 'cause I loved the “Bang! Cash! You are so money and you don't even know it.” line. Also, it's quite useful!

over 1 year ago ·

With ZSH you can do more, as usual. For instance select range of params ;)

$ echo p1 p2 p3
p1 p2 p3
$ echo !:2-3
$ echo p2 p3
p2 p3

Also you can do searches and other cool stuff read more http://www.cs.elte.hu/zsh-manual/zsh_6.html#SEC29

over 1 year ago ·

Also this tip looks very similar to https://coderwall.com/p/ttvo9g

over 1 year ago ·

So brilliant ! Thank you!

over 1 year ago ·