Don’t mind the lame Buzzfeed title… Here are a few handy bash tricks and tips that people either use every day or never knew existed. Hopefully I can help move some of you into the first camp!
A few of these commands involve working with bash history. On the advice of a coworker, I dropped this in my
.bashrc to keep tons of history:
HISTSIZE=100000 # keep 100k commands in a session history (memory)
HISTFILESIZE=200000 # store 200k commands in my history file (on disk)
Disk space is cheap, as is memory. The number of times (prior to this change) that I wanted a command that had aged out of my bash history is much greater than the number of times I’ve found bash cumbersome because my history file is almost 1MB in size (when I have a 500GB SSD and 16GB RAM in my 2-year-old laptop).
A number of bash commands reference a Meta key. In general, on a Mac, the Escape key fills that roll. On Linux, it’s generally the Alt key. You can change that, but if you’ve done so, you don’t need me to tell you about it. My examples will use Esc for these commands, but if you’re on a Linux box, you will likely want to substitute Alt for it.
Esc-. | Insert last argument
Described in the docs as insert-last-argument (M-., M-_), this keyboard shortcut will spit out the last argument to the previous command. On a Mac, the Meta key is Escape; on Linux, it’s often Alt.
$ mkdir -p long/directory/name/that/would_suck_to/type
$ cd Esc .
. will be expanded into
_ is bound to the same function, but is a bit tougher to type.
Ctrl+R | Reverse history search
This one is tough to explain, but magical. Have you ever hit the up arrow a bunch of times to scroll through history, trying to find something you ran recently?
r will open up an interactive search, or
reverse-i-search in bash parlance.
vim on a file with a long filename? Press
r and start typing
vim. The most recent command matching
vim will be showed. Keep typing to make your search more specific, or press
r again to scroll to the next-newest one. When you find what you want, press Enter to run it, or the right arrow to start moving the cursor through the command. (Or something like Ctrl+E to jump to the end of the line.)
If you want to be really nutty, you can start commenting your commands at the end.
vim /etc/X11/xorg.conf # fix video settings will allow
video to match a search, for example. I’ve been known to throw in random keywords I think I might try looking for later on.
cd – | Return to previous directory
popd are awesome and you should use them. But sometimes you forget. bash has got your back.
cd - will return you to the previous directory you were in. (This is stored in the
OLDPWD environment variable.)
git checkout – | Switch back to the previous branch
If you use git, you’ll be delighted to know that it does something similar.
git checkout - will check out the previous branch you were on. I’m often bad at cleaning up topic branches, and will
git checkout master to do some catching up, and then realize I don’t remember what my topic branch was called. Sure, it would probably take me all of 30 seconds to figure it out, but checking out
- is so much easier.
!! | Re-run the previous command
!! will re-run the command you just ran. Why not just hit the up arrow? Because
!! can be combined. The most command usage:
$ cat /root/whatever
$ sudo !!
sudo cat /root/whatever
Hope you learned something useful! What other neat tricks should I know about?