Mac M1 configures iTerm2+oh-my-zsh color matching & completion & highlighting commands

1. Preamble

After I bought a mac in late October 2021, I always wanted to configure it to the effect I wanted, but after I bought it back at the beginning, I configured iTerm2, but the relevant packages were not downloaded successfully, which resulted in the configuration not being the effect I wanted, so I went to I reconfigured it when I was not busy, and finally the configuration was successful, so I hereby record it. The premise of the installation, it is best to have installed git, if not, you can install it on Baidu~

2. Reference blog:

https://blog.csdn.net/gangyin5071/article/details/79601132
https://zhuanlan.zhihu.com/p/110668356
Thank you!

3. Effects before and after configuration

Before the configuration is successful:
insert image description here
After the configuration is successful,
insert image description here
the project path and branch name can be displayed directly, and the name is also omitted.

Fourth, install and configure iterm2

4.1 download

The downloaded iterm2 software is more powerful than the terminal software that comes with Mac.
Directly download from the official website http://iterm2.com/ , and then drag it into the application software.

4.2 configuration

Set iterm2 as the default terminal software:
open iterm2, click on the menu bar in the upper left corner:
iTerm2->Make ITerm2 Default Term
insert image description here
(mine is gray because I have already set it.)

4.3 Setting Hotkeys

Open iterm2–>Preferences–> Keys–>Hotkey–>Show/hide iTerm2 with a system-wide hotkey, set the hotkey to your favorite (such as option + space), so that you can open it through the set global hotkey Or close the iTerm2 window, very convenient.
insert image description here

4.4 iterm2 color scheme

According to the blog, solarized is used, so I also use this,
use the command

git clone git://github.com/altercation/solarized.git
# or
git clone https://github.com/altercation/solarized.git

Or download from https://ethanschoonover.com/solarized/
insert image description here
After downloading, find the file in the picture below
insert image description here
and open the preference setting preference ->profiles–>colors -> Color Presets option under iTerm2, select import, and import the decompressed solarized file There is a Solarized Dark.itermcolors under the folder with iterm2, select import in the options in the figure below, and the solarized color scheme in the figure above will appear. I use Dark.
insert image description here
At this point, the basic configuration of iTerm2 is complete.

5. Install Homebrew

Homebrew, a package manager for Mac, has functions similar to apt-get and yum tools in Linux environment
Installation method: Execute the following command in the newly installed iTerm2 terminal, which can also be obtained from the official website

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

come on happy little train

brew install sl
sl
sl

After executing the SL train, it will slowly drive towards you
insert image description here

6. Install Wget

Install Wget, which means "World Wide Web" and "Get", a tool for automatically downloading files from the Internet. It supports downloading through the three most common TCP/IP protocols: HTTP, HTTPS, and FTP, and can use HTTP proxy. Install the tool with the following command

brew install wget

After the installation process
insert image description here
is executed wget --help, if it displays a large number of ways to use wget, it means that the installation is successful.

Explanation : The installation failed when I installed it before. When I wrote this blog, I re-executed it again, and it was ok again. It should be related to the network. If it fails once, don’t worry, and you can install it again when you are free.

Seven, install the new version of Zsh

Generally, the default shell of the terminal is Bash. Zsh is recommended here. Execute the following command to view the shell type used by the current terminal. Execute the command

echo $SHELL ## 我的输出:/bin/zsh

when executed

cat /etc/shells

output:

# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.
/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

You can see that the system already comes with Zsh by default, but the version is older. You can install the latest version through the following command

brew install zsh

Set Zsh as the default shell, open the /etc/shells file and add the following code in the first line

sudo vim /etc/shells

Add to

/usr/local/bin/zsh

Then execute the following command, if it fails, choose to execute it in the system's own terminal

cd
chsh -s /usr/local/bin/zsh

8. Install oh-my-zsh

Introduction on github: https://github.com/robbyrussell/oh-my-zsh
Use crul to install:

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

If the wget installation is successful, you can use the following command

wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

The screenshot of the successful installation is as follows. If it fails once, try several times. I failed several times after executing wget. Finally, I changed my hotspot to install successfully.
insert image description here

Nine, configure the theme

Everything is ready to start configuring the theme.
Open the zsh configuration file [~/.zshrc], modify the ZSH_THEME field information to agnoster (default value: robbyrussell), and execute the command

cd
ls -al

You can see the .zshrc file
insert image description here
and execute the modified command

sudo vim ~/.zshrc

Find ZSH_THEME and modify it to

ZSH_THEME="agnoster"

Exit iTerm2 and reopen after the modification (after reinstalling the system and reinstalling it except Yao Moth, I have tried for a long time and finally solved the pro-test and effective method ), and there may be such a prompt

You will find garbled characters like "?". The reason is that some themes use the Powerline plugin, so we also need to install and add Powerline fonts.
Download fonts using the git command

git clone https://github.com/powerline/fonts.git

Or turn on powerline to download
insert image description here
. Whether it is to execute the git command or open the git page to download, it is a bit slow, and the page may not be opened. You need to retry patiently and wait for a while.
After the download is complete, you can see a directory of fonts. There is a README.rst file in it. You can open it and have a look. It introduces the installation and uninstallation methods, as well as fonts.

cat ~/fonts/README.rst

Execute the following command to install the font

cd ~/fonts
./install

will output

Powerline fonts installed to /Users/fanhuifang/Library/Fonts

Enter the directory to see the installed fonts

cd /Users/fanhuifang/Library/Fonts

You can see a lot of .ttf files. These are the corresponding fonts. The README.rst mentioned above also said that fonts can be deleted after installation, but I didn’t delete them. It’s okay to keep them. If you think it’s in the way, you can execute them.

cd 
rm -rf fonts

After installation, the garbled characters will be solved. If you need to hide the host name in the terminal prompt in iTerm and only display the user name, add the following script. Among them, the user name can also be hidden by commenting the third line of code

prompt_context() {
    
    
  if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
    prompt_segment black default "%(!.%{%F{yellow}%}.)$USER"
  fi
}

Add the above shell script content to the .zshrc file

cd
sudo vim  .zshrc

Save and execute after pasting

source ~/.zshrc 

The last step is to configure the downloaded fonts into iTerm2


insert image description here
There are many optional fonts in iTerm2 -> Preferences -> Profiles -> Text , just choose the one you like, I also choose blindly, restart iTerm2, you can see the effect.

10. Command Completion

Then set the command completion, copy the following command and press Enter

git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions

insert image description here
It turns out that the default is the following.
insert image description here
Use vim to edit .zshrc and change it to

plugins=(zsh-autosuggestions git)

Restart iTerm2 again to see the effect of command completion.

Eleven, syntax highlighting

Finally, add some easter eggs –> add syntax highlighting.

Use homebrew to install the zsh-syntax-highlighting plugin:

brew install zsh-syntax-highlighting

After the installation is complete, you can see a sentence

To activate the syntax highlighting, add the following at the end of your .zshrc:
  source /opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

insert image description here
Let's do the same and add the word source to the .zshrc file

sudo vim ~/.zshrc

Insert the following line of code at the end:

  source /opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

Finally, enter the command to reload the configuration file:

source ~/.zshrc

So when you type gi, it is found to be colored.

At this point, the iTerm2 configuration is complete, and you can play with the computer happily.

----------The more you know, the more you don't know---------

Guess you like

Origin blog.csdn.net/fhf2424045058/article/details/123372061