Do Cool Things on the Command Line with Five Quick Recipes
My earliest experiences on the Internet (and, for that matter, my earliest experiences with Unix) were by way of a terminal emulator on a dial-up connection. Using nothing more than my keyboard and text commands, I was able to navigate servers, run remote programs, upload and download files, search for information, communicate with people around the world, and even make online purchases. It wasn’t pretty, but I found it satisfying, and it certainly earned me a few geek bragging rights.
Nowadays, command-line interfaces seem quaint if not downright backwards; we click, tap, swipe, and force-touch things to get stuff done. But OS X is a version of Unix, which means that you can still do things the old way if you want to — all it takes is a quick trip to the Terminal app (find it in /Applications/Utilities), which opens the world of the command line to any Mac user.
And the command line is worth knowing about, because even after all these years, there are plenty of activities a Mac user may want to perform that can’t be done in OS X’s graphical user interface. Sure, you can find third-party apps that expose some of these features to people who don’t want to use Terminal, but as I explain in “Take Control of the Mac Command Line with Terminal, Second Edition,” Terminal is nothing to be afraid of. If you can type words into a text editor, you can use Terminal. You just have to learn a few basic principles and memorize a handful of commands. Once you’ve done that, you’ll have much greater power to customize your Mac and solve
unusual problems.
To give you a taste of the cool things you can do in Terminal, here are 5 of the 50 recipes in the newly released version 2.1 of the book, which is designed to help you become comfortable working on the command line, starting with fundamentals and learning more advanced topics as you go. These recipes are particularly easy — anyone can open Terminal and type (or copy and paste) these commands, even without knowing all the ins and outs of how things work behind the scenes. These quick techniques are among my favorites, and I hope you find them useful.
Two hints. First, enter each command as a single, long line, even if it wraps in your browser or email program. Second, after entering a command in Terminal, be sure to press Return or Enter to execute it.
Press the Power Button to Show the Shutdown Dialog — Prior to 10.9 Mavericks, pressing the power button (or key) on your Mac for a second or so displayed a dialog with Restart, Sleep, Cancel, and Shut Down buttons. Starting in Mavericks, Apple changed the behavior so that pressing that button (or key) for a second or so puts your Mac to sleep, while pressing Control-Eject brings up the shutdown dialog.
To restore the old behavior of displaying the shutdown dialog by pressing the power button (or key), enter this:
defaults write com.apple.loginwindow PowerButtonSleepsSystem -bool FALSE
If you change your mind later, you can undo this command by repeating it with TRUE
in place of FALSE
.
Stop the Help Viewer from Floating — This may be my favorite “defaults” recipe of all time. In recent versions of OS X, the Help window (which appears when you choose most commands from any application’s Help menu) floats above all other windows, no matter what you do. You can minimize it to the Dock to get it out of the way, but that makes it awkward for switching back and forth between the Help window and your app.
With this simple command, you can make the Help window act like any other window — it’ll appear in front initially, but you can click another window to bring that window in front of the Help window. Here’s the command:
defaults write com.apple.helpviewer DevMode -bool TRUE
To return the Help window to its irritating always-float behavior, repeat this command with FALSE
instead of TRUE
.
Figure Out Why You Can’t Unmount a Volume — Have you ever tried to eject a CD, disk image, or network volume, only to see an error message saying the volume is in use? If so, the maddening part can be figuring out which process is using it so you can quit that process. So enter the following, substituting for VolumeName the name of the volume you can’t unmount:
lsof | grep /Volumes/VolumeName
This command shows you any processes you own that are currently using this volume; armed with this information, you can quit the program (using the kill
command if necessary, which I discuss in the book). One frequent offender: the bash
shell itself! If that’s the case, you’ll see something like this:
bash 14384 jk cwd DIR 45,8 330 2/Volumes/Data
If you’ve navigated to a directory on this volume in your shell, Mac OS X considers it “in use.” The solution in this case is to exit the shell, or simply cd
to another directory.
If this command doesn’t tell you what you need to know, repeat it, preceded by sudo
.
Reset the Launch Services Database — Mac OS X’s Launch Services database keeps track of which programs are used to open which files, among other things. If you find that the wrong app opens when you double-click a file, or that icons don’t match up with the correct items, you may need to reset your Launch Services database. Do it like this (be sure to enter this command as a single, long line):
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user
Because this resets a lot of default mappings, your Mac may think you’re launching applications for the first time and ask if it’s OK. Agree to the alerts and you should be in good shape.
Disable Your Mac’s Startup Chime — If you’re going to be turning on (or restarting) your Mac in a quiet environment where the startup chime would be distracting, you can turn it off with the following command:
sudo nvram SystemAudioVolume=%80
To re-enable it, you use a somewhat different command:
sudo nvram -d SystemAudioVolume
Take Control of the Mac Command Line with Terminal — If you found any of these recipes helpful, note that there are 45 more in my freshly updated book. But that’s not all. Even if you’re a complete command-line newbie, I take you through everything from the very basics up through fairly advanced procedures that will bring joy to any propeller-head. The book covers navigation, working with programs, customizing Terminal (and the command-line interface itself) to your liking, dealing with permissions, and a long list of other topics.
The command line is dead; long live the command line!
These are great! The first one, "Copy text from Quick Look," seemingly has no effect, though. Running OS X 10.11.3.
Hmmm. It *was* working for me when I tested it while writing the book, and now, sure enough, it's not. I wonder if one of the El Capitan updates broke it? I'll have to investigate further.
Well, folks, sorry to have given anyone false hope. I confirmed that while the (now-deleted) command to enable text selection in Quick Look did work up through 10.10.5, it does not work as of 10.11.3. I don't know why, or whether it might just be a fleeting bug, but until I see evidence to the contrary, I'll have to assume it's jut not gonna work in El Capitan.
What he said! These recipes are all very tasty. I've been wishing for some way to banish the floating Help window for years. Thanks!!!!
Anyone who reads them and finds them useful should (a) buy Joe's book and (b) sign up to be a TidBITS member. Where else do you get useful tidbits like these?!
Thanks, Joe. Just used the one to hide the Help Viewer window.
Just found a problem with hiding the help viewer window command. When it is set to hide the window, the "About this Mac" item in the Apple Menu doesn't work correctly. It takes at least a minute for the AtM window to open. I re-ran the terminal command with the FALSE setting and the AtM item started working correctly.
Interesting. I'm not seeing that on my Mac, and I'm at a bit of a loss to know what might make it happen for you but not for me. Anyone else seeing this?
It may be only in Mac OS 10.8.5, Joe.
OK, I fired up my MBA running Mac OS 10.10.5 and found with the command set to TRUE, it only takes 2-3 seconds for the AtM window to appear. That is way faster than in 10.8.5, but is still slower than without the setting which is 1 second or slightly less. So there IS a very slight penalty in 10.10.5 but not enough to outweigh the usefulness of the new setting.
Interesting. If my experience is any indication, the penalty is even lower on El Capitan. I guess the question for anyone concerned about the delay would be which window you use more frequently. Personally, I use About This Mac maybe once every couple of months, whereas I open the Help window quite often.
Excellent on how to hide help windows. I'm afraid I likely won't be purchasing your book, but I will save that part of your post.
Control-Eject has worked to call up the Shutdown dialog as far back as I can remember—Mac OS 7.2. So has the power button. I can't imagine why Apple changed the power button behavior, unless they determined that too many people were accidentally shutting down their computers via the dialog. Which might be so, given that the power button on new Mac laptops is where the eject key is on most non-extended keyboards rather than on the upper right-hand corner of the keyboard platform where it has lived till recently.