Skip to content

Instantly share code, notes, and snippets.

@breithbarbot
Last active July 17, 2024 12:07
Show Gist options
  • Save breithbarbot/42e9a1b65ecec83b0432331616baf4ee to your computer and use it in GitHub Desktop.
Save breithbarbot/42e9a1b65ecec83b0432331616baf4ee to your computer and use it in GitHub Desktop.
Install Zsh + Oh My Zsh + Powerlevel10k theme (macOS & Linux)
#!/bin/sh
# Install Zsh + Oh My Zsh + Powerlevel10k theme (macOS & Linux)
# To run this script:
# 1. Execute: sh -c "$(curl -fsSL "$(echo "$(curl -s "https://api.github.com/gists/42e9a1b65ecec83b0432331616baf4ee")" | grep -o '"raw_url": *"[^"]*"' | cut -d'"' -f4)")"
# This script installs and configures the following:
# 1. Zsh and Git
# 2. Oh My Zsh
# 3. Powerlevel10k theme for Zsh
# 4. Custom aliases and configurations in .zshrc
# 5. Sets Zsh as the default shell
# Define color codes
GREEN_BOLD='\033[1;32m'
NC='\033[0m' # No Color
# Function to check if a command exists
command_exists() {
command -v "$1" > /dev/null 2>&1
}
# Function to add line to a specified file if it does not already exist
add_line() {
local alias_line=$1
local file=$2
if ! grep -Fxq "$alias_line" "$file"; then
echo "$alias_line" >> "$file"
fi
}
# Function to print a decorated message
print_decorated_message() {
local NC='\033[0m' # No Color
local message="$1"
local message_type="$2"
case $message_type in
"warning")
printf "%b\n" "\033[1;33m==>$NC $message"
;;
"danger")
printf "%b\n" "\033[1;31m==>$NC $message"
;;
"success")
printf "%b\n" "\033[1;32m==>$NC $message"
;;
"skip")
printf "%b\n" "\033[1;36m==>$NC $message"
;;
*)
printf "%b\n" "\033[1;34m==>$NC $message"
;;
esac
}
# Check if the script is running on MacOS or Linux
if [ "$(uname -s)" != "Darwin" ] && [ "$(uname -s)" != "Linux" ]; then
print_decorated_message "This script is designed to run on macOS or Linux. Exiting."
exit 1
fi
# Install Zsh and Git
if [ "$(uname -s)" = "Darwin" ]; then
if ! command_exists zsh; then
print_decorated_message "Installing Zsh..."
brew install zsh
fi
if ! command_exists git; then
print_decorated_message "Installing Git..."
brew install git
fi
else
if ! command_exists zsh; then
print_decorated_message "Installing Zsh..."
sudo apt install -y zsh
fi
if ! command_exists git; then
print_decorated_message "Installing Git..."
sudo apt install -y git
fi
fi
# Install Oh My Zsh
print_decorated_message "Installing Oh My Zsh..."
if [ -d "$HOME/.oh-my-zsh" ]; then
sudo rm -r ~/.oh-my-zsh
fi
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
print_decorated_message "Configuring Oh My Zsh..."
# Add aliases to .zshrc
add_line 'alias az-clean-history-and-exit="history -c && exit"' ~/.zshrc
if [ "$(uname -s)" = "Darwin" ]; then
add_line 'alias az-upgrade-packages="brew update --auto-update && brew upgrade"' ~/.zshrc
sed -i "" "s/# zstyle ':omz:update' mode auto/zstyle ':omz:update' mode auto/" ~/.zshrc
else
add_line 'alias az-upgrade-packages="sudo apt update && sudo apt upgrade -y"' ~/.zshrc
sed -i "s/# zstyle ':omz:update' mode auto/zstyle ':omz:update' mode auto/" ~/.zshrc
fi
# Install Powerlevel10k theme
print_decorated_message "Installing Powerlevel10k theme..."
if [ -d "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k" ]; then
rm -rf "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k"
fi
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
print_decorated_message "Configuring Oh My Zsh..."
# Add aliases to .zshrc
if [ "$(uname -s)" = "Darwin" ]; then
sed -i "" 's/ZSH_THEME=".*"/ZSH_THEME="powerlevel10k\/powerlevel10k"/g' ~/.zshrc
else
sed -i 's/ZSH_THEME=".*"/ZSH_THEME="powerlevel10k\/powerlevel10k"/g' ~/.zshrc
fi
# Add Powerlevel10k instant prompt configuration to .zshrc
cat << 'EOF' >> ~/.zshrc
# 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
EOF
# Add Powerlevel10k configuration to the end of .zshrc
cat << 'EOF' >> ~/.zshrc
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
EOF
# Import Powerlevel10k (p10k.zsh) custom config
if [ -d "$HOME/.p10k.zsh" ]; then
sudo rm -r ~/.p10k.zsh
fi
curl "$(echo "$(curl -s "https://api.github.com/gists/18828f196299e886b32a36a9c738baf2")" | grep -o '"raw_url": *"[^"]*"' | cut -d'"' -f4)" -H "Cache-Control: no-cache" -L > ~/.p10k.zsh
# Set Zsh as the default shell
print_decorated_message "Set Zsh as the default shell"
if [ "$(uname -s)" = "Darwin" ]; then
chsh -s /bin/zsh
else
sudo usermod -s /bin/zsh $(whoami)
fi
echo ""
print_decorated_message "To download fonts for the Zsh Powerlevel10k theme, visit the following link: https://github.com/romkatv/powerlevel10k?tab=readme-ov-file#manual-font-installation"
print_decorated_message "To start Zsh and replace the current shell session, run the following command: ${GREEN_BOLD}exec zsh${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment