DEV Community

Discussion on: Bash Shell Tricks

Collapse
 
val_baca profile image
Valentin Baca • Edited

Neat tips! thanks

Here's mine:

We all know !! but it's mostly just used to sudo !!
I find !$ (which is last argument of the last command) incredibly useful:

#make a directory and go to it
mkdir workspace
cd workspace

# OR even better
mkdir workspace
cd !$

# move then edit a file
mv ~/somefile.txt ~/renamed.txt
vim !$

# edited a file, now add it to git
vim awesome_code.c
git add !$
Enter fullscreen mode Exit fullscreen mode

There's a whole slew of ! commands but !$ is one I use all the time.

Collapse
 
slariviere profile image
Sébastien Larivière

You can also use esc + . instead of !$. I find more natural and useful if you need to fix a typo.

Collapse
 
val_baca profile image
Valentin Baca

Even better! Thanks

P.S. For posterity, all of these work just as well in zsh.

Collapse
 
bhaskar_vk profile image
Bhaskar Karambelkar

Funny $_ is the exact same as !$. There's always more to learn 😀.