To do a good job, he must sharpen his tools

Today, it was raining outside my window, and my uncle was so calm that he wrote an article about how to build a good terminal for our Mac. Sunset and solitary fly together, practical and loaded X and economy!

Note: THE following steps have been done by me in macOS Sierra 10.12, and I believe they can be performed correctly on both higher and lower versions of the system. Please rest assured.

Final effect:

ITerm2 and “Oh My Zsh”

ITerm2 is recognized on the Internet in the Mac the best use of Terminal tools, than the system comes with Terminal intelligence and powerful, detailed operation methods such as uncle accumulated enough again out of an article. The terminal tool is commonly known as CMD under Windows. (Emmmmm… If you haven’t used it, forget it.)

“Oh My Zsh” is awesome, but Shell.

We all know that the Mac operating system is based on Linux/Unix development, in fact, Windows is also, people did not admit at first, the last had to say that borrowed part of Linux/Unix, ha ha. Anyway, a Shell is a Shell for Linux/Unix. It is responsible for the interaction between the outside world and the Linux kernel, receiving commands from users and other applications, translating those commands into a language that the kernel can understand, and passing those commands to the kernel, which does the real work and then returns the results to the user or application.

Linux/Unix offers a wide variety of shells, so why so many? Sh, bash, CSH, etc. To find out how many shells your system has, you can run the following command:

$ cat /etc/shells
Copy the code

The following information is displayed:

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
Copy the code

Here the Mac will have one more copy of ZSH preinstalled than Linux. Bash is the default Shell on most Linux and OS X systems, but the really powerful Shell is the hidden ZSH. However, due to the complex configuration, resulting in a long time of no attention, until one day, a poor and boring foreign programmer developed a ZSH project that can let you quickly start, called “Oh my ZSH”, Github website: github.com/robbyrussel…

Step 1: Download and install iTerm2

The official download website: www.iterm2.com/

Just download it and install it, nothing special to pay attention to.

Note: all of the following commands are run on iTerm2 unless specified.

Step 2: Download and install “Oh My Zsh”

Open iTerm2 and execute the following command on the terminal:

curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh
Copy the code

Download the script and execute it

Step 3: Download and install Powerline

Powerline is a statusline plugin for vim, and provides statuslines and prompts for several other applications, including zsh, bash, tmux, IPython, Awesome, i3 and Qtile.

Powerline is simply a script written in Python that beautifies terminal output and supports multiple terminals.

First check whether PIP is installed on the machine:

$ pip --version
Copy the code

If the version information is not output successfully, install PIP first:

$ sudo easy_install pip
Copy the code

Install Powerline:

$ pip install powerline-status
Copy the code

If there is a message in this step indicating that the installation cannot be successful due to insufficient permissions (generally users above macOS Sierra), it is due to Apple’s official SIP security protection policy, the following steps can be temporarily closed, and we can restart after the installation, so that we can rest assured to operate.

Disabling SIP operations:

  1. Click on the Apple Logo in the upper left corner of the Mac and select Restart
  2. After the computer shuts down, press command+R until it enters “Restore Mode” before restarting.
  3. Select the Use Tools on the top menu bar and click Terminal
  4. Type csrutil disable and press Enter
  5. Restart the computer

This will turn off SIP, restart the computer, and re-run the install powerline command on iTerm2

After the installation, for security reasons, we still turn on SIP to protect our computer!

Open SIP operation:

  1. Click on the Apple Logo in the upper left corner of the Mac and select Restart
  2. After the computer shuts down, press command+R until it enters “Restore Mode” before restarting.
  3. Select the Use Tools on the top menu bar and click Terminal
  4. Type csrutil enable and press Enter
  5. Restart the computer

Step 4: Download and install the font library under Powerline

Github repository address of font library: github.com/powerline/f…

Step 1: Clone the git repository and go to the install.sh directory. Step 2: Execute command to install font library:

$ ./install.sh
Copy the code

When the installation is complete, it will prompt you that all Fonts are installed in the ~/Library/Fonts folder

Step 5: Set Regular Font and non-ASCII Font for iTerm2

Set the Regular Font and non-ASCII Font fonts in the Text TAB of the Profile in iTerm2 Settings to Powerline, as shown below. (Note that the Powerline font names are easily distinguished by Powerline.)

Step 6: Download and install the terminal color scheme

Github repository for color schemes: github.com/altercation…

Step 1: Clone the repository git and go to the folder. To import the configuration file into iTerm2’s list of color schemes, double-click Solarized Dark. Itermcolors and Solarized Light. Itermcolors. Step 3: Set the color scheme for iTerm2;

Step 7: Download and install the agnoster theme

There are many kinds of themes, here choose the Agnoster theme, after you are familiar with the operation, you can find the theme and install

Agnoster-themed Github repository: github.com/fcamblor/oh…

Step 1: Clone the repository git and go to the folder. Step 2: Run install on iTerm2. The themes will be installed in ~/. Oh-my-zsh /themes. Step 3: Set the themes for ZSH

$ vim ~/.zshrc
# select ZSH_THEME and change it to ZSH_THEME="agnoster"
Copy the code

Step 8: Use zsh-syntax-considering to add instruction highlighting

The function of this tool is that when we input the correct or successfully executed command on the terminal, the green prompt will be displayed. On the contrary, a red warning will be displayed if the command is unsuccessful or incorrect.

Zsh-syntax -highlighting Github repository address: github.com/zsh-users/z…

~/. Oh-my-zsh /custom/plugins/ Step 2: Clone repository Git to the current directory. Step 3: Append the following content to the.zshrc file

plugins=(zsh-syntax-highlighting)
source ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
Copy the code

Step 4: Reload the configuration file

$ source ~/.zshrc
Copy the code

This configures both ZSH and OH-my-zSH.

At this point, all configuration operations are complete. Now you can use your iTerm2 in a cool way!

Optimization step: Configure the Finder current directory to open iTerm2

Sometimes we want to open iTerm2 in the current directory of the Finder to avoid having to manually navigate to the corresponding path in iTerm2. The Go2Shell plugin is available online. Here I recommend using the Mac’s Automator to create AppleScript to implement our requirements.

Open Automator in Launchpad:

Select File -> New -> Service:

Select AppleScript and paste it into the script code:

The code is as follows:

on run {input, parameters}

    tell application "Finder"
        set pathList to (quoted form of POSIX path of (folder of the front window as alias))
        set command to "clear; cd " & pathList
    end tell

    tell application "System Events"
        -- some versions might identify as "iTerm2" instead of "iTerm"
        set isRunning to (exists (processes where name is "iTerm")) or (exists (processes where name is "iTerm2"))
    end tell

    tell application "iTerm"
        activate
        set hasNoWindows to ((count of windows) is 0)
        if isRunning and hasNoWindows then
            create window with default profile
        end if
        select first window

        tell the first window
            if isRunning and hasNoWindows is false then
                create tab with default profile
            end if
            tell current session to write text command
        end tell
    end tell

end run
Copy the code

After pasting, press Command + S to save, and enter the name of the service in the dialog box that is displayed.

Next set the keyboard fast key to call the service directly:

Done!

Associated Settings

  1. Start iTerm2 by default using bash instead of ZSH$ chsh -s /bin/zsh
  2. If you want to switch back to bash:$ chsh -s /bin/bash
  3. ⌘+Q To close iTerm2 every popup prompts problem: iTerm2, enterPreference-General-ClosingThe column,Confirm "Quit iTerm2 (⌘ Q)"You can just leave the command off
  4. Path prefix XX@XX is too long, shorten problem: in~/.oh-my-zsh/themesFind under pathagnoster.zsh-themeThe file can be opened using the text tool and will be insidebuild_promptUnder theprompt_contextComment out the field with a #.

conclusion

Work and interest to make their efforts to become a bit cooler, life to make their efforts to become a bit warmer.

By the way, thank you very much for reading this article, I believe you are different.