Making the Most of Terminal

This is something i wrote for work but figure it could be pretty general purpose for folks getting just comfortable with command line on their Mac.


As a computer type person who uses the computer more than others, you might find yourself working Terminal. It’s a powerful tool to get stuff done. But can we make it more powerful? The answer is yes, yes we can. The purpose of this guide is not to tell you how to set up your Terminal (the correct way) but to show you that you can set up your Terminal and explore on your own.

Theming

Black on white is pretty boring, and probably leads to eye strain. Fortunately Terminal offers theming, accessible from Preferences > Profiles. There are a number of preset color themes for you to choose from that you can also modify. There is also the option to import themes. I use Dracula, which provides a nice dark theme that’s easy on the eyes.

Also of note: You can set your Terminal’s font family and size here. A monospaced font such as Menlo or Monaco is ideal, but knock yourself out. If you want to use Curlz MT, go for it. And down at the bottom there you can even adjust your cursor!

On the Window tab you can set the title of the window, so you can alway see what the active process or path is at the top, and you can also set your default window size so you never have to make it bigger every time you open a new window. On the Shell tab i like to set the window to close when the shell exits cleanly, that way i can close the window just by using the exit command. Finally, on the Advanced tab, you can disable that audible bell. Go with the visual bell instead.

Once you’ve got your profile set up how you like, don’t forget to set it as default.

.bash_profile

Your .bash_profile is a file that lives in your home directory and gives instructions to your Terminal shell to set up the environment in which you work. Using this file, i can set up aliases for commands, set the appearance of the command line, and much much more. Using the alias command i can create shortcuts for longer commands that i wish to run later. And by including a series of aliases in my .bash_profile, i can have those shortcuts available every time i open a new Terminal window. alias ls="ls -alhG" sets my ls command to automatically include a number of useful flags. alias webserver="cd /Library/WebServer/Documents" creates a command that automatically moves me to the webserver’s document root. And alias jekdeploy="sudo cp -R /Users/joshua/Documents/code/illuminati-town/_site/* /Library/WebServer/Documents/" runs a command that automatically deploys the site files for this blog.

The export command sets useful environment variables. For example, export path will allow you to set a list of directories that contain command line programs, so that you do not have to type the full path of the command. Every time you run the command ls, you are actually running the command /bin/ls, but your Terminal’s export path is pre-set to know where to look for the ls command. Another useful change is export ps1 which changes our command prompt. I have mine set to export PS1="\\u@\h >>> " on all of my computers. The \u tells the prompt to show the user name, followed by an @, followed by the hostname of the computer, represented with the \h variable, followed by three angle brackets. This way i always know just from the prompt which machine i am i currently logged into. In the actual shell, it looks like joshua@joshua.lan >>>. This article, despite starting with a weird Angelina Jolie reference, has more things you can do to customize your command prompt.

Finally, if you want to go full bore with your .bash_profile you can even add functions. This is a function i recently discovered online, and have found to be useful.

pwdf ()
{
   currFolderPath=$( /usr/bin/osascript <<"   EOT"
       tell application "Finder"
           try
               set currFolder to (folder of the front window as alias)
           on error
               set currFolder to (path to desktop folder as alias)
           end try
           POSIX path of currFolder
       end tell
   EOT
   )
   echo "$currFolderPath"
}

This function, which incorporates some AppleScript, creates a command called pwdf that gets the full path of the front most Finder window. I can open a deeply buried directory in Finder, and in Terminal use this pwdf command to get the path of that directory. This can be incorporated into other commands, for example, cd `pwdf`to move my Terminal session to that directory.

Why Use bash?

Lately i’ve started using fish. It does a lot of fantastic colorful syntax highlighting, straight out of the box, does wonderful autosuggestions for commands based on your history, has better tab completion than bash, and has a neat web-based configurator. There’s been a few minor things i’ve had to figure out how to do differently from bash (mainly creating aliases, which you do with a nifty built in function editor, rather setting them up in a file), but it’s definitely got some cool features over bash. It even has a expandability for package-managers to install new functions and themes, that i have yet to explore at all. Here is a quick guide to setting it up and getting started.

Homebrew

Homebrew is a super handy program that acts as a package manager for macOS. It’s a command line tool to download command line tools, basically. If i want to try out, say, the programming language Erlang, i could go to their website download the latest source code, and compile it myself. Or, i could use Homebrew, and simply use the command brew install erlang and have it download and build a complete Erlang environment for me. When a new version of Erlang is available, i simply have to run brew upgrade and all of my updates are done for me. There are a plethora of command line tools that can do just about anything you can imagine, all available through Homebrew. It’s a useful tool to have. A searchable database of brew packages is available at Braumeister.

Miscellaneous

You don’t need a caps lock key. Go to System Preferences > Keyboard > Modifier Keys, and remap your caps lock to ctrl. You’re better off and it’s much easier to reach your ctrl key that way.

Need to re-run a command? Bash keeps a history of every command you run. The up arrow will scroll you back through your Bash history. ctrl+r lets you search your history as well.

Want to jump to the beginning or end of a line? ctrl+a automatically jumps you to the beginning of a line, and ctrl+e takes you to the end. These commands work in lots of other macOS programs as well.

The tab key will auto-complete a path for you (or present a list of files and directories if there is more than one that match). You can type /Use and press tab, and it will auto-complete to /Users/.

Finally, don’t forget about manpages. Pretty much every command has a corresponding manpage with details about what the command is for and how it is used. Just enter man commandname for information about what it does.

jwiltshire Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *