Here I collect miscellaneous notes on the Mac—mostly for programmers, true, but it's possible that normal people might get some use out of what's here as well.
Command Line Issues on the Mac
Some Unix commands don't work as expected on the Mac. In my experience, sed seems to be the primary culprit. Here I've begun keeping track of these issues as well as how you can fix them.
sed -i
The command sed -i
replaces the contents of the input file with the result of the change. In the Unix world, the command has an optional parameter which allows you to specify the extension of the backup of the file you're about to replace. The full syntax of the command goes something like this: sed -i[.backupext] <expression> <inputFile>
.
In the Mac world, this parameter is not optional. (At least, it's not optional in El Capitan Version 10.11.6, and going back several years.)
This listing shows: the contents of a file before a call to sed; the error message you get when you try the command without specifying the backup file's extension; a successful use of the sed command; and the result of the change.
$ cat data.txt A man is a man. A man who would hurt a friend is no friend of mine. The cat sat on a hat. One small step for man, one giant leap for mankind. $ sed -i "s/man/woman/g" data.txt sed: 1: "data.txt": extra characters at the end of d command $ sed -i.bak "s/man/woman/g" data.txt $ cat data.txt A woman is a woman. A woman who would hurt a friend is no friend of mine. The cat sat on a hat. One small step for woman, one giant leap for womankind.
The sed command is trouble in other ways on the Mac, especially, I've noticed, when it comes to using regular expressions and replacing tabs. You might be better off with a Perl one-liner. See One-Line Commands on my "Command Line Tricks" page for examples.
Terminal Tricks
Switching between Finder and the Terminal
Finder → Terminal
If you're in Finder and you want to get to a command prompt with the present working directory set to that directory, here's what you do.
In System Preferences choose Keyboard and then Shortcuts. On the left side, choose Services. Then on the right-hand-side, under Files and Folders, choose New Terminal at Folder and/or New Terminal Tab at Folder.
Now your system is ready. In Finder, right-click or control-click on a folder and choose Services > New Terminal at Folder.
Terminal → Finder
On the other hand, sometimes you're at the command line, in a particular directory, and you want to open Finder at that directory. This is absurdly simple—type open .
. (That's the word "open" followed by a period.)
Open Finder to a Path in Your Clipboard
Sometimes you want to open Finder to a location whose path is in your clipboard. No problem. In Finder hit Cmd-Shift-G. Paste the contents of your clipboard into the text field and—voilà!
Open File in Default App
As you know, certain file extensions can be set up to open with certain applications by default. You can take advantage of this at the command line by typing open <fileName>
.
File Diff App for Mac
Mac's Xcode developer tools come with a visual diff tool called FileMerge, and it's pretty darn good. The only thing is, the application is a little hard to find. Assuming you've already installed Xcode to your Applications folder, you can locate FileMerge with Finder by going to Applications and right-clicking (or Command-Clicking) on Xcode and selecting "Show Package Contents." Then navigate to Contents/Applications. If you think you'll make frequent use of FileMerge, as I do, you can drag the icon to the Dock at the bottom of your screen.
TextEdit
The free Mac application TextEdit is very good for a lot of things. Here are a few keystrokes and other tips I find very useful.
Option-Tab (or Alt-Tab)
Inserts a new bullet.
Clean up All Bullets
I use TextEdit for note-taking. As the above tip suggests, when taking notes I like to use bullets. From time to time, however, I find that bullets are automatically applied where I don't want them. To "clean up" a part of your document, simply select it, then click on the "List bullets and numbering" item in the toolbar, and select None.
Option-v (or Alt-v)
Inserts a check-mark at the cursor. (Actually this works generally on the Mac, not just in TextEdit.)
Non-English Characters
Non-English European languages apply a lot of diacritics to what would otherwise be a standard English character. I'm referring to characters like è, é and á. I'm somewhat embarrassed to admit that I was a Mac user for almost four years before I discovered how to access these characters from almost any Mac application: just hold down the keyboard character which the character you want resembles; a list of characters appears; all you have to do is type the number corresponding to the character you want.
Hidden Files
Setting Finder to always display hidden files is a pretty intuitive task: Preferences > Advanced > Show all filename extensions.
But if it ever happens that you are in some other application, and you are trying to navigate the file system—to attach a file to an email in your browser-based email service, for example—and you cannot see or navigate into a hidden folder, then here is how you can reveal it: Command+Shift+. . (That's three keys: Command, Shift, and the period.)