Mac終端美化(iterm2+oh-my-zsh+vim)

orzs發表於2024-07-19

iterm2設定主題:Agnoster 主題

1. Agnoster 主題安裝

a. 使用 vim 開啟~/.zshrc,然後將 ZSH_THEME="robbyrussell" 改成 ZSH_THEME="agnoster"
b. 安裝Powerline 對應的字型庫
為了展示 Agnoster 主題提示符裡的三角形,需要 Powerline 字型庫的支援,具體步驟如下:

git clone https://github.com/powerline/fonts.git
cd fonts
./install.sh

c. 修改 iterm2 字型配置

進入:iTerm2 > Profiles > Text > Font,修改字型為:Meslo LG S DZ Regular for Powerline。

3. 修改資料夾顯示顏色

個人不太喜歡主題中預設資料夾的顏色,所以使用如下的命令對其進行修改。

export CLICOLOR=1
export LSCOLORS="Fxbxaxdxcxegedabagacad"

vim + oh-my-zsh + git配置開發環境

vim配置

安裝vundle

使用vundle作為外掛管理器,使用前先安裝vundle

mkdir -p ~/.vim/bundle
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim

配置 .vimrc

編輯~/.vimrc檔案,寫入以下內容

set nocompatible
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'

" code complete
Plugin 'davidhalter/jedi-vim'
Plugin 'ervandew/supertab'

" syntastic check
Plugin 'nvie/vim-flake8'
Plugin 'scrooloose/syntastic'

" colorscheme
Plugin 'altercation/vim-colors-solarized'
Plugin 'luochen1990/rainbow' , {'for': 'python'}
Plugin 'morhetz/gruvbox'

" code format
Plugin 'mindriot101/vim-yapf'

" file search
Plugin 'ctrlpvim/ctrlp.vim'

call vundle#end()
filetype plugin indent on

colorscheme gruvbox

" for code complete
let g:jedi#auto_initialization = 1
let g:jedi#completions_enabled = 0
let g:jedi#show_call_signatures = 1


" for <leader>
let mapleader = ","
let g:mapleader = ","

" goto definition
let g:jedi#goto_definitions_command = ""
let g:jedi#goto_assignments_command = "<leader>g"
let g:jedi#goto_command = "<leader>d"

" file search
let g:ctrlp_map = '<c-p>'
let g:ctrlp_cmd = 'CtrlP'
" serach file in MRU
nmap <Leader>f :CtrlPMRUFiles<CR>
" search file in BUffer
nmap <Leader>b :CtrlPBuffer<CR>


set number
set cursorline
set fileencoding=utf-8
set fencs=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
set history=500
let python_highlight_all=1
set background=dark
set t_Co=256
set laststatus=2
set viminfo+=!
set showmatch
set matchtime=5
set ignorecase
set hlsearch
set autoindent
set cindent
set tabstop=4
set expandtab
set softtabstop=4
set shiftwidth=4
set autochdir
set autoread

highlight OverLength ctermbg=red ctermfg=white guibg=#592929
autocmd! FileType python match OverLength /\%89v.\+/

儲存後開啟vim,在normal模式下輸入

:PluginInstall

vundle會自動安裝配置的外掛,之後就可以愉快地使用啦~

使用

配置後的效果如圖

使用比較簡單,主要是flake8和jedi的使用

flake8用於檢查程式碼,編輯完後按快捷鍵F7進行檢查

jedi用於程式碼補全和跳轉,在函式名或者類名處按下, + d會跳轉到定義處

ctrp用於檔案搜尋和跳轉,按下, + f 調出搜尋框,輸入檔名進行搜尋,按下, + b調出緩衝區檔案,此快捷鍵配合

, + d可以在定義處和呼叫處來回跳轉,非常好用

zsh配置

zsh是區別於bash的一種shell,比bash更好用

安裝zsh

oh-my-zsh是基於zsh的一個主題,On My Zsh 官方網站 給出了三種安裝方式,任選一種即可:

a. curl 安裝

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

b. wget安裝

sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

c. fetch 安裝

sh -c "$(fetch -o - https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Oh My Zsh 是一款社群驅動的命令列工具,正如它的主頁上說的,Oh My Zsh 是一種生活方式。它基於 zsh 命令列,提供了主題配置,外掛機制,已經內建的便捷操作。給我們一種全新的方式使用命令列。Oh My Zsh 是基於 zsh 命令列的一個擴充套件工具集,提供了豐富的擴充套件功能。

P.S. 安裝 Oh My Zsh 的前提是需要安裝 Zsh,請自行查閱相關安裝方法。

配置.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="/root/.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/ohmyzsh/ohmyzsh/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.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS=true

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in ~/.oh-my-zsh/plugins/*
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
    git
    history
    zsh-syntax-highlighting
    zsh-completions
    extract
    )

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
bindkey ',' autosuggest-accept
source $ZSH/oh-my-zsh.sh
unsetopt share_history

# User configuration

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

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

export TERM=xterm-256color
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=247'


# for autojump
[[ -s ~/.autojump/etc/profile.d/autojump.sh ]] && . ~/.autojump/etc/profile.d/autojump.sh

安裝外掛

上面我們自己配置了一些外掛,需要手動下載

安裝zsh-autosuggestions

git clone git clone https://gitclone.com/github.com/zsh-users/zsh-autosuggestions.git /Users/username/.zsh/zsh-autosuggestions/

安裝zsh-syntax-highlighting和zsh-completions

cd ~/.oh-my-zsh/plugins
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
git clone https://github.com/zsh-users/zsh-completions.git

重新整理.zshrc檔案

source ~/.zshrc

使用

上面步驟完成後,就可以愉快地使用啦,效果如下圖

提示符會顯示當前的倉庫和分支名,命令列也會自動補全,非常好用


git配置

原始的git沒有顏色區分,看著不舒服,在~/.gitconfig裡可以配置顏色,這樣git statusgit diff就能很容易地看出修改來了

[user]
    email = handsome@gmail.com
    name = handsome

[alias]
    br = branch
    
[color]
    branch = auto
    diff = auto
    status = auto

[color "branch"]
    current = green
    local = white
    remote = green
    
[color "diff"]
    meta = yellow bold
    frag = magenta bold
    old = red bold
    new = green bold

[color "status"]
    added = green
    changed = red
    untracked = red

git的使用效果留給大家自己去體驗

這就是我自己的配置,如果大家有更好的配置可以告知我~

oh-my-zsh隱藏使用者名稱或者主機名(主題去掉git目錄多級顯示)

oh-my-zsh隱藏使用者名稱或者主機名

修改vim ~/.zshrc檔案,在檔案底部增加

隱藏使用者名稱和主機名

prompt_context() {}

只保留使用者名稱,隱藏主機名

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

只保留主機名,隱藏使用者名稱

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

修改後執行 source ~/.zshrc才能生效,注:可以去掉$HOST避免重複顯示路徑

相關文章