refer
#!/bin/bash
# oh-my-zsh-setup-root.sh - ้็จไบ root ็จๆท็ Oh My Zsh ๅฎ่ฃ
่ๆฌ
set -e # ้ๅฐ้่ฏฏ็ซๅณ้ๅบ
# ้ข่ฒๅฎไน
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# ๆฅๅฟๅฝๆฐ
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# ๆฃๆฅ็ฝ็ป่ฟๆฅ
check_network() {
log_info "ๆฃๆฅ็ฝ็ป่ฟๆฅ..."
// TODO ๆฃๆฅ github ๅ ...
log_success "็ฝ็ป่ฟๆฅๆญฃๅธธ"
}
# ๅฎ่ฃ
ๅฟ
่ฆ็ไพ่ต
install_dependencies() {
log_info "ๅฎ่ฃ
ๅฟ
่ฆ็ไพ่ตๅ
..."
# ๆดๆฐๅ
ๅ่กจ
apt update
# ๅฎ่ฃ
ๅบ็กๅทฅๅ
ท
apt install -y \
zsh \
curl \
wget \
git \
unzip \
software-properties-common \
apt-transport-https \
ca-certificates \
gnupg \
lsb-release \
build-essential \
python3-pip \
nodejs \
npm \
tree \
htop \
neofetch \
bat \
exa \
fd-find \
ripgrep \
fzf \
autojump \
thefuck
log_success "ไพ่ตๅ
ๅฎ่ฃ
ๅฎๆ"
}
# ๅฎ่ฃ
Oh My Zsh
install_oh_my_zsh() {
log_info "ๅฎ่ฃ
Oh My Zsh..."
if [ -d "$HOME/.oh-my-zsh" ]; then
log_warning "Oh My Zsh ๅทฒ็ปๅฎ่ฃ
๏ผ่ทณ่ฟๅฎ่ฃ
ๆญฅ้ชค"
return
fi
# ไธ่ฝฝๅนถๅฎ่ฃ
Oh My Zsh (root ็จๆท้่ฆ็นๆฎๅค็)
export RUNZSH=no
export CHSH=no
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
log_success "Oh My Zsh ๅฎ่ฃ
ๅฎๆ"
}
# ๅฎ่ฃ
Powerlevel10k ไธป้ข
install_powerlevel10k() {
log_info "ๅฎ่ฃ
Powerlevel10k ไธป้ข..."
# ๅ
้ Powerlevel10k
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
log_success "Powerlevel10k ไธป้ขๅฎ่ฃ
ๅฎๆ"
}
# ๅฎ่ฃ
ๆไปถ
install_plugins() {
log_info "ๅฎ่ฃ
Oh My Zsh ๆไปถ..."
ZSH_CUSTOM=${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}
# zsh-autosuggestions (่ชๅจๅปบ่ฎฎ)
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-autosuggestions" ]; then
git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
fi
# zsh-syntax-highlighting (่ฏญๆณ้ซไบฎ)
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" ]; then
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting
fi
# zsh-completions (่ชๅจ่กฅๅ
จๅขๅผบ)
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-completions" ]; then
git clone https://github.com/zsh-users/zsh-completions $ZSH_CUSTOM/plugins/zsh-completions
fi
# fast-syntax-highlighting (ๆดๅฟซ็่ฏญๆณ้ซไบฎ)
if [ ! -d "$ZSH_CUSTOM/plugins/fast-syntax-highlighting" ]; then
git clone https://github.com/zdharma-continuum/fast-syntax-highlighting.git $ZSH_CUSTOM/plugins/fast-syntax-highlighting
fi
# zsh-history-substring-search (ๅๅฒๆ็ดข)
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-history-substring-search" ]; then
git clone https://github.com/zsh-users/zsh-history-substring-search $ZSH_CUSTOM/plugins/zsh-history-substring-search
fi
# you-should-use (ๅซๅๆ้)
if [ ! -d "$ZSH_CUSTOM/plugins/you-should-use" ]; then
git clone https://github.com/MichaelAquilina/zsh-you-should-use.git $ZSH_CUSTOM/plugins/you-should-use
fi
log_success "ๆไปถๅฎ่ฃ
ๅฎๆ"
}
# ๅฎ่ฃ
้ขๅคๅทฅๅ
ท
install_extra_tools() {
log_info "ๅฎ่ฃ
้ขๅค็็ผ็จๅทฅๅ
ท..."
# ๅฎ่ฃ
lazygit
LAZYGIT_VERSION=$(curl -s "https://api.github.com/repos/jesseduffield/lazygit/releases/latest" | grep -Po '"tag_name": "v\K[^"]*')
curl -Lo lazygit.tar.gz "https://github.com/jesseduffield/lazygit/releases/latest/download/lazygit_${LAZYGIT_VERSION}_Linux_x86_64.tar.gz"
tar xf lazygit.tar.gz lazygit
install lazygit /usr/local/bin
rm lazygit lazygit.tar.gz
# ๅฎ่ฃ
delta (git diff ๅขๅผบ)
wget https://github.com/dandavison/delta/releases/download/0.16.5/git-delta_0.16.5_amd64.deb
dpkg -i git-delta_0.16.5_amd64.deb || apt-get install -f -y
rm git-delta_0.16.5_amd64.deb
# ๅฎ่ฃ
lsd (ls ๅขๅผบ็)
wget https://github.com/Peltoche/lsd/releases/download/0.23.1/lsd_0.23.1_amd64.deb
dpkg -i lsd_0.23.1_amd64.deb || apt-get install -f -y
rm lsd_0.23.1_amd64.deb
log_success "้ขๅคๅทฅๅ
ทๅฎ่ฃ
ๅฎๆ"
}
# ้
็ฝฎ .zshrc
configure_zshrc() {
log_info "้
็ฝฎ .zshrc ๆไปถ..."
# ๅคไปฝๅๅง้
็ฝฎ
if [ -f "$HOME/.zshrc" ]; then
cp "$HOME/.zshrc" "$HOME/.zshrc.backup.$(date +%Y%m%d_%H%M%S)"
fi
# ๅๅปบๆฐ็ .zshrc ้
็ฝฎ
cat > "$HOME/.zshrc" << 'EOF'
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
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
# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"
# Set name of the theme to load
ZSH_THEME="powerlevel10k/powerlevel10k"
# Plugins
plugins=(
git
docker
docker-compose
kubectl
python
pip
node
npm
yarn
vscode
extract
z
colored-man-pages
command-not-found
copyfile
copypath
dirhistory
history
jsontools
web-search
autojump
thefuck
fzf
zsh-autosuggestions
zsh-syntax-highlighting
zsh-completions
fast-syntax-highlighting
zsh-history-substring-search
you-should-use
)
source $ZSH/oh-my-zsh.sh
# User configuration
# Export
export EDITOR='vim'
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
# Conda ็ฏๅขๆฏๆ
if [ -f "/opt/conda/etc/profile.d/conda.sh" ]; then
source "/opt/conda/etc/profile.d/conda.sh"
elif [ -f "$HOME/anaconda3/etc/profile.d/conda.sh" ]; then
source "$HOME/anaconda3/etc/profile.d/conda.sh"
elif [ -f "$HOME/miniconda3/etc/profile.d/conda.sh" ]; then
source "$HOME/miniconda3/etc/profile.d/conda.sh"
fi
# Aliases
alias ll='lsd -alF'
alias la='lsd -A'
alias l='lsd -CF'
alias ls='lsd'
alias tree='lsd --tree'
alias cat='batcat'
alias find='fd'
alias grep='rg'
alias top='htop'
alias lg='lazygit'
alias vim='nvim'
alias python='python3'
alias pip='pip3'
# Git aliases
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git pull'
alias gd='git diff'
alias gb='git branch'
alias gco='git checkout'
alias gcb='git checkout -b'
alias gm='git merge'
alias gr='git rebase'
alias glog='git log --oneline --graph --decorate'
# Docker aliases
alias d='docker'
alias dc='docker-compose'
alias dps='docker ps'
alias di='docker images'
alias drm='docker rm'
alias drmi='docker rmi'
alias dexec='docker exec -it'
# Python aliases
alias py='python3'
alias pip='pip3'
alias venv='python3 -m venv'
alias activate='source venv/bin/activate'
# Conda aliases
alias ca='conda activate'
alias cda='conda deactivate'
alias cenv='conda env list'
alias cinfo='conda info'
# Node.js aliases
alias ni='npm install'
alias ns='npm start'
alias nt='npm test'
alias nb='npm run build'
# System aliases
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias ~='cd ~'
alias h='history'
alias c='clear'
alias e='exit'
alias reload='source ~/.zshrc'
# Root ็จๆท็นๅฎๅซๅ
alias reboot='systemctl reboot'
alias shutdown='systemctl poweroff'
alias services='systemctl list-units --type=service'
alias logs='journalctl -f'
# Functions
mkcd() {
mkdir -p "$1" && cd "$1"
}
extract() {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# FZF configuration
export FZF_DEFAULT_COMMAND='fd --type f --hidden --follow --exclude .git'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
export FZF_ALT_C_COMMAND='fd --type d --hidden --follow --exclude .git'
# History configuration
HISTSIZE=10000
SAVEHIST=10000
setopt HIST_VERIFY
setopt SHARE_HISTORY
setopt APPEND_HISTORY
setopt INC_APPEND_HISTORY
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_REDUCE_BLANKS
setopt HIST_IGNORE_SPACE
# Auto completion
autoload -U compinit && compinit
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'
# Key bindings
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
# Load additional configurations
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
# Root ็จๆทๆ็คบ
if [[ $EUID -eq 0 ]]; then
echo "โ ๏ธ ๅฝๅไธบ root ็จๆท๏ผ่ฏท่ฐจๆ
ๆไฝ"
fi
EOF
log_success ".zshrc ้
็ฝฎๅฎๆ"
}
# ้
็ฝฎ Git
configure_git() {
log_info "้
็ฝฎ Git..."
# ้
็ฝฎ delta ไฝไธบ git diff ๅทฅๅ
ท
git config --global core.pager delta
git config --global interactive.diffFilter 'delta --color-only'
git config --global delta.navigate true
git config --global delta.light false
git config --global merge.conflictstyle diff3
git config --global diff.colorMoved default
# Root ็จๆท Git ๅฎๅ
จ้
็ฝฎ
git config --global --add safe.directory '*'
log_success "Git ้
็ฝฎๅฎๆ"
}
# ่ฎพ็ฝฎ Zsh ไธบ้ป่ฎค Shell
set_default_shell() {
log_info "่ฎพ็ฝฎ Zsh ไธบ้ป่ฎค Shell..."
# ๆฃๆฅ /etc/shells ไธญๆฏๅฆๅ
ๅซ zsh
if ! grep -q "$(which zsh)" /etc/shells; then
echo "$(which zsh)" >> /etc/shells
fi
# ่ฎพ็ฝฎ root ็จๆท็้ป่ฎค shell
chsh -s $(which zsh) root
log_success "Zsh ๅทฒ่ฎพ็ฝฎไธบ้ป่ฎค Shell"
}
# ๆพ็คบๅฎ่ฃ
ๅฎๆไฟกๆฏ
show_completion_info() {
log_success "=========================================="
log_success "Oh My Zsh ๅๆไปถๅฎ่ฃ
ๅฎๆ๏ผ"
log_success "=========================================="
echo
log_info "ๅฎ่ฃ
็ๆไปถ๏ผ"
echo " โข zsh-autosuggestions (่ชๅจๅปบ่ฎฎ)"
echo " โข zsh-syntax-highlighting (่ฏญๆณ้ซไบฎ)"
echo " โข zsh-completions (่ชๅจ่กฅๅ
จๅขๅผบ)"
echo " โข fast-syntax-highlighting (ๅฟซ้่ฏญๆณ้ซไบฎ)"
echo " โข zsh-history-substring-search (ๅๅฒๆ็ดข)"
echo " โข you-should-use (ๅซๅๆ้)"
echo
log_info "ๅฎ่ฃ
็ๅทฅๅ
ท๏ผ"
echo " โข Powerlevel10k ไธป้ข"
echo " โข lazygit (Git GUI)"
echo " โข delta (Git diff ๅขๅผบ)"
echo " โข lsd (ls ๅขๅผบ็)"
echo " โข bat (cat ๅขๅผบ็)"
echo " โข exa (ls ๆฟไปฃๅ)"
echo " โข fd (find ๆฟไปฃๅ)"
echo " โข ripgrep (grep ๆฟไปฃๅ)"
echo " โข fzf (ๆจก็ณๆ็ดข)"
echo
log_info "Root ็จๆท็นๆฎ้
็ฝฎ๏ผ"
echo " โข ๅทฒๆทปๅ ็ณป็ป็ฎก็ๅซๅ"
echo " โข ๅทฒ้
็ฝฎ Git ๅฎๅ
จ็ฎๅฝ"
echo " โข ๅทฒๆฏๆ Conda ็ฏๅข"
echo
log_info "ไฝฟ็จ่ฏดๆ๏ผ"
echo " 1. ้ๅฏ็ป็ซฏๆ่ฟ่ก: exec zsh"
echo " 2. ้ฆๆฌกๅฏๅจไผๆ็คบ้
็ฝฎ Powerlevel10k ไธป้ข"
echo " 3. ่ฟ่ก 'p10k configure' ๅฏ้ๆฐ้
็ฝฎไธป้ข"
echo " 4. ๆฅ็ๆๆๅซๅ: alias"
echo
log_warning "ๅฆๆ้ๅฐ้ฎ้ข๏ผ่ฏทๆฅ็ๅคไปฝๆไปถ: ~/.zshrc.backup.*"
}
# ไธปๅฝๆฐ
main() {
echo "=========================================="
echo "Oh My Zsh ไธ้ฎๅฎ่ฃ
่ๆฌ (Root ็ๆฌ)"
echo "=========================================="
echo
log_warning "ๆฃๆตๅฐ root ็จๆท๏ผ็ปง็ปญๅฎ่ฃ
..."
check_network
install_dependencies
install_oh_my_zsh
install_powerlevel10k
install_plugins
install_extra_tools
configure_zshrc
configure_git
set_default_shell
show_completion_info
}
# ่ฟ่กไธปๅฝๆฐ
main "$@"