Chock amok
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
ps
andfile
. - Apple-specific utilities like
open
,pbcopy
, andpbpaste
. - Bash commands and settings like the tab key and the
.profile
file.1 - 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 mdfind
.
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
pbcopy
,pbpaste
, and an old Unix tool callednl
. I copied the relevant lines from the script, switched to the Terminal and typedpbpaste | nl -v 206 -w 7 -s ': ' -v 206 | pbcopy
The
-v 206
set the starting line number to 206. The-w 7
set 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-s ': '
put a colon and two spaces between the line numbers and the code lines. This is the convention I’ve used here for 6–7 years; lines with this format get their line numbers styled by a little JavaScript program.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)
to
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
.profile
file. I keep my bash settings in.bashrc
and have a.bash_profile
file that sources it. Something to do with login and non-login shells, as I recall. ↩