Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Configuring macOS command line
Ramo Mujagic
Ramo Mujagic

Posted on • Edited on

     

Configuring macOS command line

If you are serious about doing any kind of development work, it is really important to have an easy to use command line interface. Out of the box, macOS comes with the Terminal app. We will start from there and try to improve overall experience.

Before taking a deeper dive, take a look at the image below to get a glimpse of what is going to be covered here.

Terminal app - configured terminal window

There will be plenty of things that can be changed and you can end up with completely different interface, if you choose so.

Shell setup

Before going any further, you need to choose which shell to use. There are many out there and macOS usually comes with two of them pre-installed,bash andzsh.

My choice here iszsh. It is highly customisable and comes with manypowerful features. Even if you currently usebash, switching tozsh is quite simple. More so,zsh is an extended version ofbash. They basically share a lot of common functionality and features.

Install Zsh

Majority of macOS versions already come withzsh pre-installed. To check ifzsh is installed on your system, run the following command.

zsh--version
Enter fullscreen modeExit fullscreen mode

If you havezsh installed, it's version should be printed out. In this case, you do not need to do any additional changes.

However, ifzsh is not installed, you can easily install it usinghomebrew. Homebrew is a macOS package manager that makes it easy to install missing software. It usually comes pre-installed on many macOS versions. To installzsh usinghomebrew, run the following command.

brewinstallzsh
Enter fullscreen modeExit fullscreen mode

You can confirm if it's installed by running thezsh --version command again.

Zsh as a default shell

Depending on the macOS version, your default shell might be set tobash. You can easily check it by running the following command.

echo$0
Enter fullscreen modeExit fullscreen mode

If you see that the output iszsh, then your default shell is already set properly. In case you need to setzsh as a default shell, do so by running the following command.

chsh-s$(which zsh)
Enter fullscreen modeExit fullscreen mode

Finally, start new shell session (close the current and open a new Terminal window), for the changes to take effect.

Migrate from bash

If you are coming frombash, you might have some custom configuration saved in your.bash_profile or.bashrc files. Since those files arebash specific, they will not be used byzsh and all configuration saved in them must be migrated intozsh specific files.

  • .zshenv is sourced on all shell invocations. Here you should save all exports that are needed for other programs.
  • .zshrc is sourced when running in interactive shells. Here you can put all configs that make the shell experience more enjoyable, like:aliases,functions,options, etc.

Make sure that all of your$PATH exports are saved in the.zshenv file, since it will be sourced on every shell invocation.

Oh My Zsh framework

When you switch tozsh, installingOh My Zsh is one single thing that you must do. This will make your shell really awesome!

Oh My Zsh is a community driven framework for managingzsh configuration. Installing it could not be simpler, just run the following command.

sh-c"$(curl-fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Enter fullscreen modeExit fullscreen mode

Oh My Zsh install command assumes that you already havegit installed on your machine. In case you don't, followthis guide to install it.

Oh My Zsh will save it's configuration into the~/.zshrc file. In case you already had.zshrc created before, it's content will be copied and saved in~/.zshrc.pre-oh-my-zsh file.

You can move your previous configuration back to the~/.zshrc by copying it from~/.zshrc.pre-oh-my-zsh.

It is usually a good idea to create~/env.sh where you can keep your custom configuration. Then in~/.zshrc you can use thesource command to load it.

...# Load evn.sh filesource ~/env.sh
Enter fullscreen modeExit fullscreen mode

This ensures that your custom configuration will always be sourced when running shell in interactive mode. When you need to add or change custom configuration, just do it in theenv.sh file.

Themes

Your shell might be powerfull now, but it is not good enough if it looks ugly. Luckily, setting a theme with Oh My Zsh is a quite simple.

Open~/.zshrc file in your favorite editor,vim of course. In there, you will find a variable with the nameZSH_THEME. By default, it will be set torobbyrussel. To change the theme, just provide a different theme name.

There are tons of already pre-madethemes available to pick from.

Personally, I use thearrow theme since it provides simple, minimal and clean interface. To enable thearrow theme just setZSH_THEME="arrow" in your~/.zshrc file. Make sure to restart your shell session after.

...# Set name of the theme to load --- if set to "random", it will# load a random theme each time oh-my-zsh is loaded, in which case,# to know which specific one was loaded, run: echo $RANDOM_THEME# See https://github.com/ohmyzsh/ohmyzsh/wiki/ThemesZSH_THEME="arrow"...
Enter fullscreen modeExit fullscreen mode

You can also useexternal themes which are created by the community. Using external themes requires more work since they need to be added to the custom theme directory. You can followthis guide for more information about this topic.

Plugins

Great thing about Oh My Zsh is that itcomes bundled with allkinds of plugins. This allows you to easily extend the functionality of your shell, just by enabling them. Plugins can be easily enabled in the~/.zshrc file by changing theplugins variable.

One such bundled plugin is calledweb-search. It allows you to query popular services, like google, from your terminal window. To enable this plugin, just addweb-search to theplugins variable.

...# Which plugins would you like to load?# Standard plugins can be found in $ZSH/plugins/# Custom plugins may be added to $ZSH_CUSTOM/plugins/# Example format: plugins=(rails git textmate ruby lighthouse)# Add wisely, as too many plugins slow down shell startup.plugins=(web-search)...
Enter fullscreen modeExit fullscreen mode

Keep in mind thatplugins variable is actually an array. If you have multiple values inside, they must be separated with empty space.

Now, let's switch tothird party plugins, since there are so many good ones. To use third party plugins you need to install them into the$ZSH_CUSTOM/plugins directory. After that, the only thing that needs to be done is to add the plugin, by it' name, into theplugins variable.

I would recommend installing following plugins since they can increase your shell productivity significantly.

  • zsh-autosuggestions - suggests commands as you type based on history and completions.
  • zsh-syntax-highlighting - highlights commands while you type them in the interactive terminal.

Installzsh-autosuggestions running following command.

git clone https://github.com/zsh-users/zsh-autosuggestions${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
Enter fullscreen modeExit fullscreen mode

Installzsh-syntax-highlighting running following command.

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
Enter fullscreen modeExit fullscreen mode

When plugins are installed, enable them viaplugins variable and restart your shell for effects to take place.

...# Which plugins would you like to load?# Standard plugins can be found in $ZSH/plugins/# Custom plugins may be added to $ZSH_CUSTOM/plugins/# Example format: plugins=(rails git textmate ruby lighthouse)# Add wisely, as too many plugins slow down shell startup.plugins=(web-search zsh-autosuggestions zsh-syntax-highlighting)...
Enter fullscreen modeExit fullscreen mode

Plugins can provide a lot of value and extend your shell in so many ways, but there is also one downside. Every plugin you add willimpact shell performance andstartup time. So, choose plugins wisely and keep only the ones you are actually using.

Terminal app

This section is all about configuring, default, Terminal app that come preinstalled on every macOS.

You might wonder, why not use more feature rich apps likeiTerm2?
The reason for me is really simple, I just do not use much of the provided features and the benefits for me are not that big.

With that out of the way, let's configure the Terminal app.

Install Fira Code font

I have tried a lot of fonts in the terminal environment, but this one just feels the best. It is a completely free, monospaced font, containingligatures for common programming multi-character combinations.

Fastest way to install the font is to go to thereleases page of the Fira Code repository and download the latest version. Once downloaded, you can just install all fonts provided in thettf directory. With that done, Fira Code is installed onto your system.

Custom profile

In the Terminal app, profiles can define custom configuration and behavior. To modify customization to our liking, a custom profile needs to be created.

It is totally fine to customize it manually, but there are also a lot of third party themes that can be used in the Terminal app. One example is theDracula theme. Usually, third party themes will be provided via.terminal file which can be imported in the Terminal app.

I personally use the Dracula theme myself. You can download.zip fromofficial website. It should come with theDracula.terminal file.

To install it, open the Terminal app and go toPreferences > Profiles. From there, click on theMore icon and a dropdown will be shown where you need to selectImport. When the theme is imported, it will be saved as a new profile with the nameDracula.

Make sure thatDracula profile is selected. On the bottom, you should see theDefault button. Click on the button and it will set, currently selected, profile as default. With this, when the new Terminal window is open, it will automatically use selected profile.

On the right side, you can see all the settings that can be changed. Since we use the Dracula theme, most of the settings does not need to be changed, but I like to tweak some of the options to make it more nice.

Let's start withBackground settings. Click on theBackground icon and a new window should open. Here you can configure Terminal background settings. I like to keep it simple, so for me, settings are set to following.

Background color   black (#000000)Opacity            90%Blur               5%
Enter fullscreen modeExit fullscreen mode

This will give you a Terminal window that is slightly transparent with a blurred background. I like it since it looks clean and opacity is high enough so underlying windows are not visible that much.

Next, theFont needs to be changed. Click on theChange button under theFont section and the new window should open. SelectFira Code as your font and adjust settings to your linking.

You can also tweak other settings as well, but I do not change them since they are defined by the Dracula theme anyways.

Conclusion

Some people find working with terminal and command line tools very hard and often are trying to avoid it completely. But the truth is,it is inevitable.

When things are like that, why don't we make sure that the command line environment looks nice and actually help us to increase our productivity?

By choosing the right terminal app, changing shell and tweaking theme configuration it is possible to enhance command line experience drastically. I really hope that this post has given you a glimpse into what is possible and, at the end, you are able to customize your command line environment to your liking.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer mainly working with JavaScript on Web and Mobile based applications
  • Location
    Linz, Austria
  • Work
    Software Engineer
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp