macOS Command Line Tips

As previously mentioned, one of my favorite exercises is to avoid using system transfers when I move to a new MacBook, and instead start over from scratch, as I learn something new every time.

One big difference this time around has been that I am in the command line much more now than I have been in the past. I’ve been working actively on this as a skill, and as a result, it means more time sitting at a prompt.

The default terminal in macOS is fantastic, at least for the reason that it’s a modern OS with UNIX-like command line syntax—but there are some way it falls short, based on either its BSD roots or choices made by Apple.

The good news is that you can make up for a good number of those shortcomings with a bit of work, and find some neat tricks at the same time. I asked on Twitter for some additional tips:

I’ll be sharing any tips that I’m pointed to there for the first time in this post and subsequent edits, so if you have any tips of your own, drop me a reply on that tweet and I’ll check them out.

Optional Step One: Install a Terminal Replacement

This is very greatly a matter of personal preference, but I usually replace the Terminal app with an alternate solution. (This is where we pour one out for TotalTerminal a.k.a. Visor, of blessed memory.)

I prefer iTerm2 and its advanced features such as split panes, shell integration, and more intelligent buffering and options, but you may find something of your own you prefer. iTerm2 is also open source under the GPLv2.

Install Xcode Command Line Tools

Before we can do anything else of note with the command line, we should install some command line stuff that’s left out by default in macOS but very useful for a lot of the other things we’ll want to do. Thankfully, Apple made this bit pretty easy.

Open your command line and run:

xcode-select --install

You’ll be prompted to confirm this installation with a GUI dialogue. Accept it, and macOS will download the developer tools for you. (You will possibly end up upgrading some of these tools, but that’s OK. This will get you started and give you the basics you need.)

Get a Monospaced Font That’s Not Monaco

Monaco as a font is OK, but not great. There are two alternative options I usually recommend.

The first is the monospaced font that comes bundled in with the default Terminal app:SF Mono. Apple doesn’t distribute this font outside of either Terminal or Xcode from what I can tell, but you can extract it from the Terminal app if you would like. Get to the folder containing the font files using this:

open /Applications/Utilities/Terminal.app/Contents/Resources/Fonts/

Select all the files in that folder and open them, which will take you to Font Book to try and install them. Font Book will tell you there are problems with the font files. Font Book is lying. The installation will work, and I haven’t seen any reports of it being problematic.

(I think the warning is because doing this might possibly be against the terms of use for the font.)

If you would rather not pull SF Mono out of Terminal, or if the errors give you pause, another great option is to install Anonymous Pro, which has been my go-to fixed width font for years. It’s a great option and has a free license. You can download it here.

Get Homebrew for Package Management

Modern Linux distributions often use package managers to add and remove installed software with ease.

Now, when you run into a command line tool or other utility someone’s pointed out to you, you can usually install it more or less automatically with Homebrew. Here’s an example for wget, which quite honestly should be included with macOS, but isn’t:

pathfinder:nodecg ryanmarkel$ wget
-bash: wget: command not found
pathfinder:nodecg ryanmarkel$ brew install wget
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Deleted Formulae
kibana@4.1

==> Installing dependencies for wget: openssl@1.1
==> Installing wget dependency: openssl@1.1
==> Downloading https://homebrew.bintray.com/bottles/openssl@1.1-1.1.0f.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl@1.1-1.1.0f.sierra.bottle.tar.gz
==> Using the sandbox
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs

and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/openssl@1.1/lib
    CPPFLAGS: -I/usr/local/opt/openssl@1.1/include

==> Summary
🍺  /usr/local/Cellar/openssl@1.1/1.1.0f: 6,421 files, 15.5MB
==> Installing wget
==> Downloading https://homebrew.bintray.com/bottles/wget-1.19.1_1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring wget-1.19.1_1.sierra.bottle.tar.gz
🍺  /usr/local/Cellar/wget/1.19.1_1: 11 files, 1.6MB

Suggested Homebrew Packages

You can get pretty fancy with Homebrew. My colleague Jeremy Herve has a great script he uses to run it when spinning up a new system, and he posted about that here. I don’t install that many things via Homebrew, but there are a handful of things available through it that I use with some regularity.

If you know of any cool utilities I don’t; feel free to ping me on Twitter and tell me about them.

mas

(source on GitHub; MIT license)

mas is a great utility that helps you with both installing and maintaining apps you have installed through the Mac App Store (which, admittedly, is fewer and fewer apps over time). You can even use it to search for apps and manage your authentication status.

pathfinder:nodecg ryanmarkel$ mas list
409183694 Keynote (7.2)
408981434 iMovie (10.1.6)
485812721 TweetDeck (3.9.889)
443987910 1Password (6.7)
904280696 Things3 (3.0.3)
803453959 Slack (2.6.2)
442007571 AntiRSI (3.3.0)
557168941 Tweetbot (2.5.1)
407963104 Pixelmator (3.6)
409201541 Pages (6.2)
682658836 GarageBand (10.2.0)
409203825 Numbers (4.2)
692867256 Simplenote (1.1.8)

cloc

(source on GitHub; GPLv2 license)

At work, we do a lot of code review and scheduling those code reviews. If I have a bundle of code and I want to size it up quickly to see what it does, I use cloc to do this. It’s a great first-look at how much work a review could end up being.

pathfinder:Development ryanmarkel$ cloc ryanmarkel-v2/
    1933 text files.
    1905 unique files.
     146 files ignored.

github.com/AlDanial/cloc v 1.72  T=8.46 s (211.5 files/s, 32469.1 lines/s)
----------------------------------------------------------------------------------------
Language                              files          blank        comment           code
----------------------------------------------------------------------------------------
PHP                                    1305          18979          44697          79398
CSS                                      79           5329           2056          32684
JavaScript                              149           4631           5865          20148
PO File                                  73           8254          12486          20091
XML                                      74            869            926           5606
Markdown                                 40           1512              0           3896
JSON                                     31             14              0           3282
HTML                                      9             14              0           1444
Velocity Template Language                1             11              4            470
Bourne Shell                              6            114             40            468
Maven                                     1             30             21            305
YAML                                     10             52            111            262
Ant                                       1             19             30            140
INI                                       7             27             89             95
DTD                                       1             25             54             69
XSLT                                      1              5             14             19
make                                      1              1              0              3
----------------------------------------------------------------------------------------
SUM:                                   1789          39886          66393         168380
----------------------------------------------------------------------------------------

streamlink

(source on GitHub; BSDv2 license)

streamlink is a forked successor of livestreamer, which unfortunately became a dead project but is insanely useful. It uses command line instructions combined with (normally) an install of VLC to open streaming video using a method that tends to be much lighter-weight than using a browser. It will help you identify various transcodes as well, and can be used even to load authentication-required video for some services.

pathfinder:Development ryanmarkel$ streamlink https://twitch.tv/gamesdonequick
[cli][info] Found matching plugin twitch for URL https://twitch.tv/gamesdonequick
Available streams: audio_only, 160p (worst), 360p, 480p, 720p, 720p60 (best)

Bring Some Color to bash

Some of the tools that are included with macOS at the command line and its default configuration are slightly altered from defaults you may be used to in other UNIX-like environments. One that tends to bother me is that by default, ls doesn’t have any color indicators for output. By default, it looks like so:

This really isn’t helpful. Let’s add at least some color marking by adding this to our ~/.profile:

export CLICOLOR=1

Now, when I’m in a terminal session in any terminal app, I should see my directories like so:

You may also wish to customize your prompt using the information you can find here, but the number of options there are a bit much for me to get into.

Using pbcopy/pbpaste

Mark Jaquith replied to me with this tip:

https://twitter.com/markjaquith/status/881500386888175618

Straight up: I did not even know about pbcopy and pbpaste, but reading the man pages for them, it’s crazy I went this long without knowing what they were and how to use them.

You can use the commands to move text back-and-forth between your terminal session and the macOS Clipboard. This should be self-explanatory, but for example, I just realized that I could have used it to put large chunks of the output from commands in this very post without having to select it and copy it.

What Am I Forgetting?

If there’s a neat trick or setup tip you think I’m missing, please let me know! Drop a reply to either the tweet for this post or the one I posted earlier and let me know what I can add!