Mac 終端窗口配置 oh-my-zsh


查看當前環境

檢查當前使用的shell

Mac 默認使用 bash,當然也可以使用如下命令查看已安裝的 shell

bogon:~ himonkey$ echo $SHELL
/bin/bash

查看已經安裝的shell

bogon:~ himonkey$ cat /etc/shells 
# 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/ksh
/bin/sh
/bin/tcsh
/bin/zsh

查看當前zsh的版本號

bogon:~ himonkey$ zsh --version
zsh 5.3 (x86_64-apple-darwin18.0)

查看當前最新的zsh版本號,使用homebrew,可以看到當前最新是5.7.1

bogon:~ himonkey$ brew info zsh
zsh: stable 5.7.1 (bottled), HEAD
UNIX shell (command interpreter)
https://www.zsh.org/
Not installed
From: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git/Formula/zsh.rb
==> Dependencies
Required: ncurses ✘, pcre ✘
==> Options
--HEAD
	Install HEAD version
==> Analytics
install: 41,546 (30 days), 122,176 (90 days), 572,107 (365 days)
install_on_request: 39,529 (30 days), 117,505 (90 days), 538,051 (365 days)
build_error: 0 (30 days)

brew 安裝最新 zsh

原系統的 zsh 不動它,使用 brew 安裝最新的 zsh 。

bogon:~ himonkey$ brew install zsh
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/cask-versions and homebrew/cask).

==> Installing dependencies for zsh: ncurses and pcre
==> Installing zsh dependency: ncurses
==> Downloading https://homebrew.bintray.com/bottles/ncurses-6.1.mojave.bottle.t
==> Downloading from https://akamai.bintray.com/01/01e031f6bb1513edcb26d78a15dc5
######################################################################## 100.0%
==> Pouring ncurses-6.1.mojave.bottle.tar.gz
==> Caveats
ncurses is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ncurses first in your PATH run:
  echo 'export PATH="/usr/local/opt/ncurses/bin:$PATH"' >> ~/.bash_profile

For compilers to find ncurses you may need to set:
  export LDFLAGS="-L/usr/local/opt/ncurses/lib"
  export CPPFLAGS="-I/usr/local/opt/ncurses/include"

==> Summary
🍺  /usr/local/Cellar/ncurses/6.1: 3,869 files, 8.3MB
==> Installing zsh dependency: pcre
==> Downloading https://homebrew.bintray.com/bottles/pcre-8.43.mojave.bottle.tar
==> Downloading from https://akamai.bintray.com/08/08e7414a7641d1e184c936537ff67
######################################################################## 100.0%
==> Pouring pcre-8.43.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/pcre/8.43: 204 files, 5.5MB
==> Installing zsh
==> Downloading https://homebrew.bintray.com/bottles/zsh-5.7.1.mojave.bottle.tar
==> Downloading from https://akamai.bintray.com/79/793d87f67e64a5e01dfdea890af21
######################################################################## 100.0%
==> Pouring zsh-5.7.1.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/zsh/5.7.1: 1,515 files, 13.3MB
==> Caveats
==> ncurses
ncurses is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ncurses first in your PATH run:
  echo 'export PATH="/usr/local/opt/ncurses/bin:$PATH"' >> ~/.bash_profile

For compilers to find ncurses you may need to set:
  export LDFLAGS="-L/usr/local/opt/ncurses/lib"
  export CPPFLAGS="-I/usr/local/opt/ncurses/include"

再次運行 zsh --version 查看到的 zsh 版本仍為 5.3 ,我們可以查看一下 /etc/shells 文件中的 zsh 仍為 上述查看的那樣,我們需要把 brew 安裝的 zsh 添加到 shells 文件中。

# 查看 brew 安裝到 zsh 的位置
bogon:~ himonkey$ brew list zsh
/usr/local/Cellar/zsh/5.7.1/bin/zsh
/usr/local/Cellar/zsh/5.7.1/bin/zsh-5.7.1
/usr/local/Cellar/zsh/5.7.1/lib/zsh/ (36 files)
/usr/local/Cellar/zsh/5.7.1/share/info/ (8 files)
/usr/local/Cellar/zsh/5.7.1/share/man/ (17 files)
/usr/local/Cellar/zsh/5.7.1/share/zsh/ (1446 files)

brew 安裝到文件的位置

  1. 通過brew install安裝應用最先是放在/usr/local/Cellar/目錄下。

  2. 有些應用會自動創建軟鏈接放在/usr/bin或者/usr/sbin,同時也會將整個文件夾放在/usr/local

所以在 /etc/shells 文件中添加 /usr/local/Cellar/zsh/5.7.1/bin/zsh-5.7.1 還是 /usr/local/bin/zsh-5.7.1 都是一樣的。下面也驗證了這一點。

bogon:~ himonkey$ ls -al /usr/local/bin
total 0
drwxrwxr-x  19 himonkey  admin  608 11 11 10:36 .
drwxr-xr-x  15 root      wheel  480 11  1 16:24 ..
lrwxr-xr-x   1 himonkey  admin   28 11  1 16:30 brew -> /usr/local/Homebrew/bin/brew
......
lrwxr-xr-x   1 himonkey  admin   27 11 11 10:36 zsh -> ../Cellar/zsh/5.7.1/bin/zsh
lrwxr-xr-x   1 himonkey  admin   33 11 11 10:36 zsh-5.7.1 -> ../Cellar/zsh/5.7.1/bin/zsh-5.7.1

添加最新zsh

使用 vim 這 /bin/zsh 前按 a 鍵在當前光標位置輸入。 注釋掉 /bin/zsh

bogon:~ himonkey$ sudo vim /etc/shells 
Password:

# 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/ksh
/bin/sh
/bin/tcsh
# /bin/zsh
/usr/local/Cellar/zsh/5.7.1/bin/zsh-5.7.1
~                                                                               
~                                                                               
~                                                                               
~                                                                               
:wq                                                                             

重啟終端就可以用 zsh --version 查看到當前 zsh 版本換為 5.7 的。

將當前 bash 終端切換為 zsh, 使用 chsh -s 路徑 然后輸入密碼,重啟就行了。切換回 bash 也一樣操作。

bogon:~ himonkey$ zsh --version
zsh 5.7.1 (x86_64-apple-darwin18.2.0)
bogon:~ himonkey$ chsh -s /usr/local/Cellar/zsh/5.7.1/bin/zsh-5.7.1
Changing shell for himonkey.
Password for himonkey: ****

重啟終端就切換到 zsh 了。顯示信息如下,
它會提醒你這是配置函數 你的主目錄(也就是 ~ 目錄)沒有配置文件。

屏幕快照 2019-11-11 下午1.09.31

This is the Z Shell configuration function for new users,
zsh-newuser-install.
這是新用戶的Z Shell配置函數,zsh-newuser-install。
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~). This function can help you with a few settings that should
make your use of the shell easier.
您看到這條消息是因為您沒有zsh啟動文件(目錄~中的.zshenv、.zprofile、.zshrc、.zlogin文件)。這個函數可以幫助您進行一些設置,使您更容易地使用shell。
You can:
你可以:
(q) Quit and do nothing. The function will be run again next time.
(q)退出,什么都不做。該函數將在下一次再次運行。
(0) Exit, creating the file ~/.zshrc containing just a comment.
That will prevent this function being run again.
(0)退出,創建文件~/。只包含注釋的zshrc。這將阻止該函數再次運行。
(1) Continue to the main menu.
(1)繼續進入主菜單。
--- Type one of the keys in parentheses ---

按 1 進入主菜單

請選擇以下選項之一:
(1)配置歷史設置,即命令行記住和保存的外殼。
(2)配置新的補全系統。
(3)配置鍵在編輯命令行時的行為方式。(推薦)
(4)選擇一些更常見的shell選項。這些是簡單的“開”或“關”開關控制外殼的功能。
(0)退出,創建一個空白~/.zshrc中。
(a)中止所有設置,從零開始。注意,這將覆蓋任何設置從zsh-newuser-install已經在啟動文件。但是,它不會改變您的任何其他設置。
(q)退出不干別的。該函數將在下一次再次運行。
——在括號中鍵入一個鍵——

直接輸入一個 0 會在 ~ 目錄下生成一個空的 .zshrc 配置文件。

安裝 oh-my-zsh 配置主題和插件

安裝 oh-my-zsh

可以看一下 ~ 目錄下是否有 .oh-my-zsh 文件,當然是沒有。開始安裝后

bogon% ls -al
total 56
drwxr-xr-x+ 23 himonkey  staff    736 11 11 17:08 .
drwxr-xr-x   5 root      admin    160 10 22 18:13 ..
-r--------   1 himonkey  staff      9 10 22 18:13 .CFUserTextEncoding
-rw-r--r--@  1 himonkey  staff  10244 11 11 16:42 .DS_Store
drwx------  19 himonkey  staff    608 11 11 16:54 .Trash
drwxr-xr-x   3 himonkey  staff     96 11  3 12:54 .android
-rw-------   1 himonkey  staff    660 11 11 12:48 .bash_history
drwx------  42 himonkey  staff   1344 11 11 12:48 .bash_sessions
drwxr-xr-x   3 himonkey  staff     96 11  1 19:09 .oracle_jre_usage
drwxr-xr-x   3 himonkey  staff     96 11  1 19:09 .vim
-rw-------   1 himonkey  staff   3627 11 11 17:08 .viminfo
-rw-r--r--   1 himonkey  staff     31 11 11 16:55 .zshrc
drwx------@  4 himonkey  staff    128 11  8 21:19 Applications
drwx------+  4 himonkey  staff    128 11 11 16:54 Desktop
drwx------+  4 himonkey  staff    128 10 25 01:11 Documents
drwx------+  5 himonkey  staff    160 11  8 22:31 Downloads
drwx------+ 64 himonkey  staff   2048 11  2 12:42 Library
drwx------+  3 himonkey  staff     96 10 22 18:13 Movies
drwx------+  6 himonkey  staff    192 11  8 21:15 Music
drwx------+  6 himonkey  staff    192 11  8 21:48 Pictures
drwxr-xr-x+  5 himonkey  staff    160 10 23 19:53 Public
drwxr-xr-x   6 himonkey  staff    192 10 23 22:13 tool
drwxr-xr-x   6 himonkey  staff    192 10 23 22:13 work

使用命令安裝
curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh
有安裝當然有卸載

bogon% curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
100  8109  100  8109    0     0   4200      0  0:00:01  0:00:01 --:--:--  4200
Cloning Oh My Zsh...
正克隆到 '/Users/himonkey/.oh-my-zsh'...
remote: Enumerating objects: 1102, done.
remote: Counting objects: 100% (1102/1102), done.
remote: Compressing objects: 100% (1054/1054), done.
remote: Total 1102 (delta 23), reused 882 (delta 19), pack-reused 0
接收對象中: 100% (1102/1102), 721.26 KiB | 435.00 KiB/s, 完成.
處理 delta 中: 100% (23/23), 完成.

Looking for an existing zsh config...
Found ~/.zshrc. Backing up to /Users/himonkey/.zshrc.pre-oh-my-zsh
Using the Oh My Zsh template file and adding it to ~/.zshrc.

         __                                     __
  ____  / /_     ____ ___  __  __   ____  _____/ /_
 / __ \/ __ \   / __ `__ \/ / / /  /_  / / ___/ __ \
/ /_/ / / / /  / / / / / / /_/ /    / /_(__  ) / / /
\____/_/ /_/  /_/ /_/ /_/\__, /    /___/____/_/ /_/
                        /____/                       ....is now installed!


Please look over the ~/.zshrc file to select plugins, themes, and options.

p.s. Follow us on https://twitter.com/ohmyzsh

p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh

Run zsh to try it out.
bogon% ls
Applications	Downloads	Music		tool
Desktop		Library		Pictures	work
Documents	Movies		Public
bogon% ls -al
total 64
drwxr-xr-x+ 25 himonkey  staff    800 11 11 20:25 .
drwxr-xr-x   5 root      admin    160 10 22 18:13 ..
-r--------   1 himonkey  staff      9 10 22 18:13 .CFUserTextEncoding
-rw-r--r--@  1 himonkey  staff  10244 11 11 16:42 .DS_Store
drwx------  19 himonkey  staff    608 11 11 16:54 .Trash
drwxr-xr-x   3 himonkey  staff     96 11  3 12:54 .android
-rw-------   1 himonkey  staff    660 11 11 12:48 .bash_history
drwx------  42 himonkey  staff   1344 11 11 12:48 .bash_sessions
drwxr-xr-x  19 himonkey  staff    608 11 11 20:25 .oh-my-zsh
drwxr-xr-x   3 himonkey  staff     96 11  1 19:09 .oracle_jre_usage
drwxr-xr-x   3 himonkey  staff     96 11  1 19:09 .vim
-rw-------   1 himonkey  staff   3627 11 11 17:08 .viminfo
-rw-r--r--   1 himonkey  staff   3570 11 11 20:25 .zshrc
-rw-r--r--   1 himonkey  staff     31 11 11 16:55 .zshrc.pre-oh-my-zsh
drwx------@  4 himonkey  staff    128 11  8 21:19 Applications
drwx------+  4 himonkey  staff    128 11 11 16:54 Desktop
drwx------+  4 himonkey  staff    128 10 25 01:11 Documents
drwx------+  5 himonkey  staff    160 11  8 22:31 Downloads
drwx------+ 64 himonkey  staff   2048 11  2 12:42 Library
drwx------+  3 himonkey  staff     96 10 22 18:13 Movies
drwx------+  6 himonkey  staff    192 11  8 21:15 Music
drwx------+  6 himonkey  staff    192 11  8 21:48 Pictures
drwxr-xr-x+  5 himonkey  staff    160 10 23 19:53 Public
drwxr-xr-x   6 himonkey  staff    192 10 23 22:13 tool
drwxr-xr-x   6 himonkey  staff    192 10 23 22:13 work

這時候重啟終端再看 .zshrc 文件中有了如下內容。其實就是上面安裝了 oh-my-zsh 的時候,腳本幫我們把原來存在的 .zshrc 文件備份成 .zshrc.pre-oh-my-zsh 將自己的配置文件更改成 .zshrc.
剛好我原來的 .zshrc 文件是空的,那就把備份的 .zshrc.pre-oh-my-zsh 給刪掉。
bogon% vi .zshrc

➜  ~ vi .zshrc 
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="/Users/himonkey/.oh-my-zsh"

# 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/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="robbyrussell"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in ~/.oh-my-zsh/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.

即使上面的不小心誤修改了,不用擔心可以找到 ~/.oh-my-zsh/templates 下的 zshrc.zsh-template 這個文件和上面一致。

開始各種造。

安裝各種插件
我想安裝的:

  • Powerline 狀態行插件
  • 主題 powerlevel10k
  • 語法高亮和語法自動建議插件
  • colorls 是一個 ruby gem,可使用顏色和超棒的字體圖標美化終端

主題 powerlevel10k

設置方法:vim ~/.zshrc, 找到ZSH_THEME=“”,這句話,在雙引號里面寫上cloud就可以啦!

如果你在里面寫的是random,每次開啟終端的主題將是隨機的!

當然你也可以自己找主題,oh-my-zsh里面帶有主題了,主題都在以下這個文件夾里,可以進去找自己喜歡的
~/.oh-my-zsh/themes

當然啦這里面沒有我喜歡的,哈哈。我需要安裝powerlevel10k。

主題的介紹
它的介紹是這樣的,注意關鍵字:兼容 Powerlevel9k,10-100倍的性能提升!具體自己去看
https://github.com/romkatv/powerlevel10k

安裝
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git $ZSH_CUSTOM/themes/powerlevel10k

安裝完成的主題放在 ~/.oh-my-zsh/custom/themes 文件夾下。

➜  themes git:(master) git clone --depth=1 https://github.com/romkatv/powerlevel10k.git $ZSH_CUSTOM/themes/powerlevel10k
正克隆到 '/Users/himonkey/.oh-my-zsh/custom/themes/powerlevel10k'...
remote: Enumerating objects: 38, done.
remote: Counting objects: 100% (38/38), done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 38 (delta 3), reused 13 (delta 0), pack-reused 0
展開對象中: 100% (38/38), 完成.

修改主題的配置
vi ~/.zshrc
修改主題配置如下所示

ZSH_THEME="powerlevel10k/powerlevel10k"

重啟終端,你會看到如下圖
屏幕快照 2019-11-11 下午10.01.14

其實這是Powerlevel10k配置向導。您看到它是因為您沒有定義任何Powerlevel10k配置選項。它將詢問您幾個問題並配置提示符。這看起來像鑽石(旋轉的正方形)嗎?

如果你看到的和我一樣也就是說你的字體不支持這么多符號,這就需要我門安裝字體了。

然后一路設置下去 我們會看到

New config: ~/.p10k.zsh.
Backup of ~/.zshrc: $TMPDIR/.zshrc.eOlXszNdzA.

See ~/.zshrc changes:

  diff $TMPDIR/.zshrc.eOlXszNdzA ~/.zshrc
File feature requests and bug reports at
https://github.com/romkatv/powerlevel10k/issues.
  • 想重新配置該主題, 如果你不喜歡它。你可以隨時直接輸入 p10k configure 命令來運行 powerline10k 向導。即可重新配置並覆蓋掉 ~/.p10k.zsh 文件。
  • 移除插件,這是GitHub上給的方法。可以看到改動還是很少的。實際卻生成了一個 ~/.p10k.zsh 文件.
    This method of installation won't make anything slower or otherwise sub-par.
    比較了一下異同也就能知道, 該主題在 ~/.zshrc 中添加了兩部分內容, 一個是前面 1 到 7 行和后面 107 到109 行。
❯ diff $TMPDIR/.zshrc.eOlXszNdzA ~/.zshrc
0a1,7
> # Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
> # Initialization code that may require console input (password prompts, [y/n]
> # confirmations, etc.) must go above this block, everything else may go below.
> if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
>   source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
> fi
> 
99a107,109
> 
> # To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
> [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

所以 刪除 ~/.zshrc 中添加了兩部分內容、刪除 ~/.p10k.zsh 文件、 刪除 ~/.oh-my-zsh/custom/themes/powerlevel10k 文件夾

安裝字體

安裝完 powerlevel10k 后你可能會發現有亂碼,這是因為你終端字體不支持那么多的字符,這就需要使用擴展字體了。
安裝 powerline 字體有四種方式:
1: Install Powerline Fonts
2: Use a Programmer Font
3: Install Awesome-Powerline Fonts
4: Install Nerd-Fonts
Nerd 字體應該是支持字形最多的,所以不用多想,安裝它就好了!
github 上也有詳細的安裝介紹文檔:https://github.com/ryanoasis/nerd-fonts#font-installation

Nerd字體預覽:https://app.programmingfonts.org/

安裝字體第一種 homebrew

GitHub 上有詳細的安裝教程中可以使用 homebrew 安裝,如果能已經添加過 homebrew/cask-fonts 倉庫,那就下面代碼中第一行就可以省略了,直接執行第二行的語句就行。

$ brew tap homebrew/cask-fonts  # you only have to do this once!
$ brew cask install font-inconsolata

當然 我是直接下載的 ttf 文件雙擊安裝字體的。怎么安裝字體都行,安裝完事兒后 只需在 終端的偏好設置中選擇安裝的字體就行。
設置終端字體的步驟

安裝字體的第二種方式 字體文件安裝

訪問 nerd 網站 https://www.nerdfonts.com/
按照圖中所示 找到 hack 字體下載
字體下載步驟

下載下來的是一個 zip 文件,雙擊解壓 就可以安裝其中的字體,里面字體有重復的 我最終只安裝了四個字體,其中也就 Hack Regular Nerd Font Complete Mono 字體完全適用。

安裝 oh-my-zsh 插件

插件存儲在 ~/.oh-my-zsh/plugins 文件夾中。要激活插件,你需要編輯 ~/.zshrc 文件。安裝插件意味着你創建了一系列執行特定功能的別名或快捷方式。
安裝插件也和安裝 主題一樣,大同小異不做過多解釋。文件最好都放在 ~/.oh-my-zsh/custom/plugings/文件中。
都是使用 git 克隆過來。
然后在 ~/.zshrc 的插件列表中。
plugins=( [plugins...] zsh-syntax-highlighting zsh-autosuggestions )

  • 語法高亮插件 git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  • 和語法自動建議插件 git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

安裝 colorls 彩色的文件夾和圖標

colorls 是一個 ruby gem,可使用顏色和超棒的字體圖標美化終端的 ls 命令。你可以訪問官方網站以獲取更多信息。

sudo gem install colorls

# sudo gem uninstall colorls

安裝的截圖

通過在shell配置文件中輸入以下行 來啟用 colorls 的選項啟用制表符補完功能,只需在 shell 配置末尾(~/.bashrc 或者 ~/.zshrc)輸入以下行:

source $(dirname $(gem which colorls))/tab_complete.sh

反正這個配置我是加不上, 也不知道有什么作用。反正我加了用下面指令

echo $(dirname $(gem which colorls))

會輸出路徑然后拼合起來得到路徑。具體看我的配置文件中 source /Library/Ruby/Gems/2.3.0/gems/colorls-1.2.0/lib/tab_complete.sh
安裝完成后的顯示界面,帶圖標的文件夾

可以看到 ls 指令仍然是原來的樣子,每次都需要輸入 colorls 來完成。所以為防止每次輸入 colorls,你可以在 ~/.bashrc 或 ~/.zshrc 中創建別名。

alias ll='colorls -lA --sd --gs --group-directories-first'
alias ls='colorls --group-directories-first'

tree 命令來查看當前目錄下的目錄樹

沒適應mac的finder, 但是覺得 Linux 的 tree 命令好用。
從網上找到了一個shell 命令打印 tree 結構的方法.

find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'

為了以后不用輸入這么復雜的語句,使用 alias 指定別名的方法,在 ~/.bashrc 或 ~/.zshrc 中創建別名。

alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"

但我更喜歡 brew install tree 安裝的 tree ,因為它支持的指令更多。

最后的最后

附上我的 .zshrc 的內容

# 啟用Powerlevel10k即時提示.應該靠近~/.zshrc的頂部.
# 可能需要控制台輸入的初始化代碼(密碼提示,[y/n]
# 確認書等)必須放在這一塊上面,其他的都可以放在下面.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# 如果你來自bash,你可能不得不改變你的 $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# 你的oh my zsh安裝路徑.
export ZSH="/Users/himonkey/.oh-my-zsh"

# 設置要加載的主題的名稱---如果設置為"random",則將
# 每次加載oh my zsh時加載一個隨機主題,在這種情況下,
# 要知道加載了哪個特定的主題,請運行: echo $RANDOM_THEME
# 參見https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="powerlevel10k/powerlevel10k"

# 設置隨機加載時要從中選取的主題列表
# 在ZSH_THEME=random時設置此變量將導致ZSH加載
# 一個來自這個變量的主題,而不是查看 ~/.oh-my-zsh/themes/
# 如果設置為空數組,則此變量將無效.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )


# 取消對以下行的注釋以使用區分大小寫的完成方式.
# CASE_SENSITIVE="true"

# 取消對以下行的注釋以使用不區分連字符的完成方式.
# 必須關閉區分大小寫的完成. _ 和 - 可以互換.
# HYPHEN_INSENSITIVE="true"

# 取消注釋以下行以禁用每兩周一次的自動更新檢查.
# DISABLE_AUTO_UPDATE="true"

# 取消對以下行的注釋以自動更新而不提示.
# DISABLE_UPDATE_PROMPT="true"


# 取消對以下行的注釋以更改自動更新的頻率(以天為單位).
# export UPDATE_ZSH_DAYS=13

# 如果粘貼URL和其他文本時出錯,請取消對以下行的注釋.
# DISABLE_MAGIC_FUNCTIONS=true

# 取消注釋以下行以禁用ls中的顏色.
# DISABLE_LS_COLORS="true"

# 取消注釋以下行以禁用自動設置終端標題.
# DISABLE_AUTO_TITLE="true"

# 取消對以下行的注釋以啟用命令自動更正.
# ENABLE_CORRECTION="true"

# 取消注釋以下行以在等待完成時顯示紅點.
# COMPLETION_WAITING_DOTS="true"

# 如果要禁用標記未跟蹤的文件,請取消對以下行的注釋
# 在VCS下是骯臟的.這將檢查大型存儲庫的存儲庫狀態
# 非常, 非常快.
# DISABLE_UNTRACKED_FILES_DIRTY="true"


# 如果要更改命令執行時間,請取消對以下行的注釋
# 歷史命令輸出中顯示的戳記.
# 您可以設置三種可選格式之一:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# 或者使用strftime函數格式規范設置自定義格式,
# 詳情請參閱"man strftime".
# HIST_STAMPS="mm/dd/yyyy"

# 是否要使用 $ZSH/custom 之外的其他自定義文件夾?
# ZSH_CUSTOM=/path/to/new-custom-folder

# 您想加載哪些插件?
# 標准插件可以在 ~/.oh-my-zsh/plugins/* 中找到
# 自定義插件可以添加到  ~/.oh-my-zsh/custom/plugins/
# 示例格式: plugins=(rails git textmate ruby lighthouse)
# 明智地添加,因為太多的插件會減慢shell的啟動速度.
plugins=(
 git
 zsh-syntax-highlighting
 zsh-autosuggestions
)

source $ZSH/oh-my-zsh.sh

# 用戶配置

# export MANPATH="/usr/local/man:$MANPATH"

# 您可能需要手動設置語言環境
# export LANG=en_US.UTF-8

# 本地和遠程會話的首選編輯器
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# 編譯標志
# export ARCHFLAGS="-arch x86_64"

# 設置個人別名,覆蓋 oh-my-zsh libs 提供的別名,
# 插件 plugins, 和主題 themes. Aliases 可以放在這里, 通過 oh-my-zsh
# 鼓勵用戶在 ZSH_CUSTOM 文件夾中定義別名.
#
# 要獲得活動別名的完整列表,請運行"alias".
#
# 別名示例
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

# 防止每次輸入 "colorls" 所以為 colorls 的常用操作設置別名
alias ll='colorls -lA --sd --gs --group-directories-first'
alias ls='colorls --group-directories-first'
# 一個shell 命令打印 tree 結構的方法查看當前目錄下的目錄樹結構
alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"

# 若要自定義提示,請運行 `p10k configure` 或編輯 ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

# 為 colorls 的選項啟用制表符補完功能,只需在 shell 配置末尾輸入以下行
# source $(dirname ($gem which colorls))/tab_complete.sh
 source /Library/Ruby/Gems/2.3.0/gems/colorls-1.2.0/lib/tab_complete.sh


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM