September 7, 2014 at 10:15 PM by Dr. Drang
During this calm before the upcoming Apple storm, we should all take time to read and contemplate the 30+ years of command line wisdom summarized in this recent post by Craig Hockenberry. I started using some of his tips right away; others will have to marinate for a while until I’m ready to chew on them.
Craig’s tips cover a broad range of topics and include discussion of
- General Unix utilities like
- Apple-specific utilities like
- Bash commands and settings like the tab key and the
- Terminal behavior like ⌥-clicking, ⌥→, and ⌥←.
So far, my favorite tip has been the addition of these two lines to my
.bashrc file to search backward and forward through my command history using the up and down arrow keys:
bind '"\e[A":history-search-backward' bind '"\e[B":history-search-forward'
Bash normally uses ↑ and ↓ to move backward and forward in the command history one command at a time. But if you have the beginning of a command typed, these bindings cause ↑ and ↓ to move back and forth through only those commands that start the same way. Very clever and very useful. For years, I’ve been using ⌃R and ⌃S to search backward and forward through the command history, but this is much simpler.
The section of Craig’s post I need to study further covers the
md* commands that leverage Spotlight and the metadata it uses. I’ve used
mdls to get the duration of audio files, but there’s so much more I could do with it and
I think the best way to thank Craig is to write up our own tips. Here are a couple of mine:
Craig talks about integrating the Terminal and the Mac Desktop by dragging the icon of a file or folder from a Finder window into Terminal to add its path to the command line. That works, but there’s more to the story. If, for example, you need the path to the folder of the current Finder window, you can click on the little folder icon in the title bar (called the proxy icon) and drag it to the Terminal.
This trick isn’t limited to Finder windows. The proxy icon for any file open in any application can be dragged into Terminal to insert its path.
In a post I wrote a week ago, I needed to add some numbered lines of code. As you might expect, I have a BBEdit Text Filter defined that adds line numbers to a selection, but it always starts the numbering at one. In last week’s case, I was showing only an excerpt of a script, an excerpt that started on Line 206. My Text Filter was useless. I relied instead on
pbpaste, and an old Unix tool called
nl. I copied the relevant lines from the script, switched to the Terminal and typed
pbpaste | nl -v 206 -w 7 -s ': ' -v 206 | pbcopy
-v 206set the starting line number to 206. The
-w 7set the width of the line number prefix to 4 characters, which meant the 3-digit line numbers were preceded by four spaces (that’s a Markdown thing). Finally, the
The pipeline changed the clipboard contents from
# Send the message through GMail. smtp = smtplib.SMTP_SSL('smtp.gmail.com', 465) smtp.ehlo() smtp.login(gmailUser, gmailPassword) smtp.sendmail(mailFrom, mailTo, msg)
206: # Send the message through GMail. 207: smtp = smtplib.SMTP_SSL('smtp.gmail.com', 465) 208: smtp.ehlo() 209: smtp.login(gmailUser, gmailPassword) 210: smtp.sendmail(mailFrom, mailTo, msg)
which you’ll see with styled line numbers if you’re reading this at my site.
I look forward to seeing Terminal tricks from all of you.
For reasons lost in time, don’t have a
.profilefile. I keep my bash settings in
.bashrcand have a
.bash_profilefile that sources it. Something to do with login and non-login shells, as I recall. ↩︎