By Ryan Veazey
Oct 27, 2017
This is the second part in a series about configuring and using a Mac. Whereas the first post focused on more generally applicable advice, this one will focus more on the command-line environment.
Note: I will be using the same keyboard shortcut conventions I introduced in the previous blog, such as
⌃ to mean the control (or ctrl) key.
While the Mac terminal is very good, its default settings profile is not. Go to settings (
⌘,). Under that, select Pro for New window with profile and, under the
Profiles tab, select Pro and click Default. From now on, new terminal windows should use the Pro theme which has a more traditional light-on-dark theme with a slight translucency. All of these settings can be configured, but Pro is a much better place to start, as most command-line tools assume a similar theme.
Under Profiles/Window and Tab, you can configure titles to help you better identify them at a glance. Settings include values like working directory, process name and profile name.
The Shell section has an option for When the shell exits: which I would suggest setting to Close if the shell exited cleanly. This generally means that if you caused the process to quit, the tab or window will simply disappear, but if some failure caused it to end, it will remain so you can diagnose the problem. The default behavior leaves all windows open. You can also specify a list of processes to ignore when deciding if it should warn you about closing an active terminal session.
The Advanced section includes configuration for the bell, which has a fair number of settings, and the terminal type (which should usually be set to xterm-256color).
If you find that your key presses are not sending what you’d expect to the terminal, check the Keyboard settings under the Profiles tab. The actual configuration you’ll need may depend on which shell and/or program you’re using, as well as configuration in your shell, but here you can “fix” issues like the Home and End keys not working as you’d expect. If you need to customize this, it’s probably best to do a web search for the specific function you’re trying to configure, as there are quite a few variations.
There is also a setting under General to open new tabs in the same directory as the current terminal, which I would suggest enabling.
The system-wide shortcuts (mentioned in the previous post) still work in the Terminal app, such as
⌃N for new window,
⌃Tab for new tab, and
⌃} to switch left or right through open tabs.
The following aren’t Mac-specific, but good to know nonetheless. These assume a Bash shell, but many may work for other shells as well.
⌃L- Clear the screen. Should work in the shell and in most programs which use the read line library, including many REPLs (interactive shells for programming languages).
⌃C- Send SIGINT (interrupt) to the current program. This usually (but not always) kills the process.
⌃D- Send an end of file signal. This can be useful to trigger the end of input into a program such as cat.
⌃E- Move to the beginning or end of the line, respectively.
⌃R- Search backward through previously typed commands in the history
⌃Z- put currently running process in the background. This does not kill the process. Use the
fgcommand to restore the process to the foreground.
⌃K- Kill (delete) to the end of the line. Deletes all text to the right of the current cursor position.
⌃W- Similar to above, but deletes everything from the beginning of the line until the cursor.
Tab- attempt to complete the current word. Depending on your shell/program, this can suggest completions for filenames, programs, arguments, branches, etc. It’s worth a quick search on how to add tab completion to your shell for programs you frequently use.
Depending on your shell (and your login method), several files are sourced when you start a terminal session. These are used to configure all kinds of things about your session. If you’re using Bash (the default), you’ll probably want to be editing
My suggested additions to
export EDITOR=vim export CLICOLOR=1 export LSCOLORS=GxFxCxDxBxegedabagaced
This should set the default editor to Vim (if you absolutely don’t want to use Vim, you may want to change this, but I’d suggest learning). It also sets some variables which should give you nicely formatted output for
ls so you can, for example, easily distinguish folders or executable files.
If you’re asking about a shell, you know bash. It’s ubiquitous, and it’s already the default shell. If you’re new to the command line, stick with this, as it’ll be the most commonly assumed environment when looking for help.
Z shell (zsh) is a shell that is similar to Bash but with some additional features. Some very powerful customizations and themes can be added easily via Oh My Zsh. Some of these present very helpful prompts with all kinds of information about. For example, the prompt I use writes the current directory on the right side of the line, with some information about the git branch before the prompt on the left.
A list of plugins can be found here. Many of these include deep tab-completion logic for programs such as git, docker and various tools for different programming languages.
Vim is an improved (hence the name) version of the venerable vi editor. It is a very powerful text editor that I highly suggest becoming at least moderately comfortable using. Many of the conventions are shared with other programs, including
sed, and there are a great number of plugins and configurations which can make it as good or better than an IDE for many use cases.
A lot of developer working in languages like Python, Ruby or Node.js use Vim. You’ll want to make sure and do some basic configuration for things like tab width and syntax highlighting, but there’s a large number of plugins which can provide everything from linting to autocompletion suggestions
That being said, vi (and Vim) has a reputation for being very difficult to learn. This is primarily due to one unusual but very important feature of Vim - it is modal, which is to say that it has different modes. The two main ones are the (default) normal mode, and the insert mode. In insert mode, keystrokes will insert text, but command mode can do things like deleting, appending, repeating, changing, substituting ranges of text all with a few quick keystrokes. One of the tricks to learning normal mode is to think of manipulating text via an action and a direction, such as “delete 4 words”, “yank (copy) 3 lines”, or “change inner quotes”. Macros can be quickly recorded and replayed to rather simply do things as complicated as finding all HTML elements of a certain type and deleting the first two tags inside them, something considerably harder to do in many other editors.
There are many great resources on how to use Vim that can be found with a quick web search, but the most important one is how to quit: Make sure you are in normal mode (Which you’ll know by the absence of a line at the bottom specifying a different mode such as –INSERT–) by pressing
⌃C, and then type
:q for quit. If you have changed the file, it will either require you to first write the file, which can be done at the same time by typing
:wq for “write and quit” or
:q! for “quit and discard any changes.” Help can also be found in-app by typing
:help in normal mode.
Configuring Vim can be a daunting task since there is no normal settings screen. To change config, you’ll want to edit (or create) the file
My suggested starting place for configuring vim is this
set nocompatible set number syntax on set ai set tabstop=2 set shiftwidth=2 set expandtab set backspace=indent,eol,start set wildmenu set showmatch filetype plugin indent on
This will set some basic features such as syntax highlighting and auto-indentation based on filetype, show line numbers on the left, and give a more friendly tab-completion prompt.
There have been several plugin systems pop up in recent years for Vim, but the good news is the plugins themselves aren’t proprietary to them, so you don’t have to worry about not being able to use a specific plugin with your plugin manager. The most popular manager as of this writing appears to be vim-plug. A great list of plugins can be found here.
As I suggested in a previous article, use brew.
Macs come with Python, Ruby, PHP and Perl installed by default, so a lot of functionality can be assumed out of the box. However, there are times when you may want to install packages for these languages without have root access, or to use different versions of them. Many of these languages have projects aimed at easily managing these, such as RVM (Ruby Version Manager), which can help you try out new versions of Ruby without modifying the system version.
The next blog post in this series will give suggestions for which programs (some built-in, some third-party) to use for specific-use cases.