Last active
January 18, 2024 18:48
-
-
Save sinamics/d009a27192aa2fd22ea97bca3c0a36fd to your computer and use it in GitHub Desktop.
Install zsh and oh my zsh plugin with aliases ( optionally ).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# To use this script copy&paste this in your terminal: | |
# bash <(curl -s https://gist.githubusercontent.com/sinamics/d009a27192aa2fd22ea97bca3c0a36fd/raw/lord_shell.sh) | |
######################################### | |
# This script will install the following packages: | |
# - zsh | |
# - oh my zsh | |
# - zsh-autosuggestions | |
# - zsh-highlighting | |
# - docker auto completion ( if docker is installed ) | |
# - docker-compose auto completion ( if docker-compose is installed ) | |
######################################### | |
# Change your theme for your needs: | |
# https://github.com/ohmyzsh/ohmyzsh/wiki/Themes | |
THEME="fino" | |
# Function to check if a command is installed and, if not, ask the user if they want to install it | |
check_and_install_command() { | |
package=$1 | |
# Check if the command is installed | |
if ! [ -x "$(command -v $package)" ]; then | |
# The command is not installed, ask the user if they want to install it | |
read -p "$package is not installed. Do you want to install it now? [Y/n] " install_command | |
if [ "$install_command" = "y" ] || [ "$install_command" = "Y" ] || [ -z "$install_command" ]; then | |
# Install the command | |
sudo apt-get update && sudo apt-get install $package | |
# Note: this will depend on the operating system and package manager being used | |
# For example, on Debian-based systems you could use "apt-get install $package" | |
else | |
# Command installation was declined | |
echo "$package installation declined. Exiting script." | |
exit 1 | |
fi | |
fi | |
} | |
# Check if git is installed | |
check_and_install_command "git" | |
check_and_install_command "curl" | |
ohMyZshInstaller() { | |
if which tput >/dev/null 2>&1; then | |
ncolors=$(tput colors) | |
fi | |
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then | |
RED="$(tput setaf 1)" | |
GREEN="$(tput setaf 2)" | |
YELLOW="$(tput setaf 3)" | |
BLUE="$(tput setaf 4)" | |
BOLD="$(tput bold)" | |
NORMAL="$(tput sgr0)" | |
else | |
RED="" | |
GREEN="" | |
YELLOW="" | |
BLUE="" | |
BOLD="" | |
NORMAL="" | |
fi | |
set -e | |
ZSH=~/.oh-my-zsh | |
if [ -d "$ZSH" ]; then | |
printf "\n${RED}You'll need to remove $ZSH if you want to re-install.${NORMAL}\n" | |
echo "${YELLOW}Do you want to delete .oh-my-zsh folder and re-install?${NORMAL} (y/n) " | |
read YESNO | |
if [ $YESNO = "y" ]; then | |
echo "deleting file..." | |
rm -rf ~/.oh-my-zsh | |
else | |
exit | |
fi | |
unset YESNO | |
fi | |
# Prevent the cloned repository from having insecure permissions. Failing to do | |
# so causes compinit() calls to fail with "command not found: compdef" errors | |
# for users with insecure umasks (e.g., "002", allowing group writability). Note | |
# that this will be ignored under Cygwin by default, as Windows ACLs take | |
# precedence over umasks except for filesystems mounted with option "noacl". | |
umask g-w,o-w | |
printf "${BLUE}Cloning Oh My Zsh...${NORMAL}\n" | |
# The Windows (MSYS) Git is not compatible with normal use on cygwin | |
if [ "$OSTYPE" = cygwin ]; then | |
if git --version | grep msysgit > /dev/null; then | |
echo "Error: Windows/MSYS Git is not supported on Cygwin" | |
echo "Error: Make sure the Cygwin git package is installed and is first on the path" | |
exit 1 | |
fi | |
fi | |
env git clone --depth=1 https://github.com/robbyrussell/oh-my-zsh.git $ZSH || { | |
printf "Error: git clone of oh-my-zsh repo failed\n" | |
exit 1 | |
} | |
printf "${BLUE}Looking for an existing zsh config...${NORMAL}\n" | |
if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then | |
printf "${YELLOW}Found ~/.zshrc.${NORMAL} ${GREEN}Backing up to ~/.zshrc.pre-oh-my-zsh${NORMAL}\n"; | |
mv ~/.zshrc ~/.zshrc.pre-oh-my-zsh; | |
fi | |
printf "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc${NORMAL}\n" | |
cp $ZSH/templates/zshrc.zsh-template ~/.zshrc | |
sed "/^export ZSH=/ c\\ | |
export ZSH=$ZSH | |
" ~/.zshrc > ~/.zshrc-omztemp | |
mv -f ~/.zshrc-omztemp ~/.zshrc | |
} | |
# install zsh | |
echo "installing Zsh..." | |
sudo apt-get install zsh | |
######################################### | |
# install oh my zsh | |
######################################### | |
echo "installing Oh-My-Zsh..." | |
ohMyZshInstaller | |
######################################### | |
# install plugin zsh-autosuggestions | |
######################################### | |
echo "installing zsh-autosuggestions..." | |
git clone https://github.com/zsh-users/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions || { | |
printf "Error happened when installing zsh-autosuggestions" | |
exit 1 | |
} | |
######################################### | |
# install zsh plugin syntax-highlighting | |
######################################### | |
echo "installing zsh-syntax-highlighting..." | |
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting || { | |
printf "Error happened when installing zsh-highlighting" | |
exit 1 | |
} | |
######################################### | |
# install docker and bash completion | |
######################################### | |
# Check if Docker is installed | |
if ! command -v docker &> /dev/null | |
then | |
echo "Docker is not installed." | |
read -p "${YELLOW}Do you want to install Docker? [Y/n]${NORMAL} " answer | |
if [[ $answer =~ ^[Yy] ]] | |
then | |
# Assuming the use of a Debian-based system for Docker installation | |
curl -fsSL https://get.docker.com -o get-docker.sh | |
sh get-docker.sh | |
sudo usermod -aG docker $USER | |
echo "Docker has been installed." | |
else | |
echo "Docker installation aborted." | |
fi | |
fi | |
if [[ $(which docker) && $(docker --version) ]]; then | |
curl -L https://raw.githubusercontent.com/docker/docker/v$(sudo docker version --format '{{.Server.Version}}')/contrib/completion/bash/docker -o /etc/bash_completion.d/docker || { | |
printf "Error happened when installing docker bash completion" | |
} | |
fi | |
######################################### | |
# install docker-compose bash completion | |
######################################### | |
if [[ $(which docker) && $(which docker-compose) ]]; then | |
curl -L https://raw.githubusercontent.com/docker/compose/$(sudo docker compose version --short)/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose || { | |
printf "Error happened when installing docker-compose bash completion" | |
} | |
fi | |
# set theme | |
sed -i s/^ZSH_THEME=".\+"$/ZSH_THEME=\"$THEME\"/g ~/.zshrc | |
# set plugin | |
sed -i 's/\(^plugins=([^)]*\)/\1 sudo zsh-autosuggestions zsh-syntax-highlighting/' ~/.zshrc | |
printf "\n${YELLOW}You want to install Sinamics .bash_aliases ? (y/n)${NORMAL}\n" | |
read YESNO | |
if [ $YESNO = "y" ]; then | |
curl -sS https://gist.githubusercontent.com/sinamics/fad5ebbb62f57e0ef9966e20613dcf2e/raw/.bash_aliases -o ~/.bash_aliases || { | |
echo "Error happened when i'm tryng to set zsh your default shell" | |
} | |
if ! grep -Fxq ". ~/.bash_aliases" .zshrc; then | |
{ | |
echo 'if [ -f ~/.bash_aliases ]; then' | |
echo '. ~/.bash_aliases' | |
echo 'fi' | |
} >> ~/.zshrc | |
fi | |
else | |
echo "You are all set" | |
fi | |
printf "\n${YELLOW}You want to install starship theme with Sinamics config (y/n)${NORMAL}\n" | |
read YESNO | |
if [ $YESNO = "y" ]; then | |
# install starship | |
curl -sS https://starship.rs/install.sh | sh | |
# add starship to czshrc | |
{ | |
echo 'eval "$(starship init zsh)"' | |
} >> ~/.zshrc | |
# create config folder | |
mkdir -p ~/.config && touch ~/.config/starship.toml | |
# download sinamics config | |
curl -s https://gist.githubusercontent.com/sinamics/346a3c09f7c0d56c691da3c9b6306141/raw/starship.toml -o ~/.config/starship.toml | |
fi | |
unset YESNO | |
printf "\n${YELLOW}You want to install the sinamics motd welcome message (y/n)${NORMAL}\n" | |
read YESNO | |
if [ $YESNO = "y" ]; then | |
if [ ! -d /etc/zsh/zprofile.d ]; then | |
sudo mkdir /etc/zsh/zprofile.d | |
fi | |
sudo curl -sS https://gist.githubusercontent.com/sinamics/d1f96f39b797ccb2eba6e8bd539510bc/raw/motd.sh -o /etc/zsh/zprofile.d/motd.sh || { | |
echo "Could not install the motd welcome message" | |
} | |
if ! grep -F "if [ -d /etc/zsh/zprofile.d ]; then" /etc/zsh/zprofile > /dev/null; then | |
{ | |
echo "if [ -d /etc/zsh/zprofile.d ]; then" | |
echo " for i in /etc/zsh/zprofile.d/*.sh; do" | |
echo " if [ -r $i ]; then" | |
echo " sudo /bin/bash \$i" | |
echo " fi" | |
echo " done" | |
echo " unset i" | |
echo "fi" | |
} >> /etc/zsh/zprofile | |
fi | |
fi | |
unset YESNO | |
printf "\n\n" | |
printf "${YELLOW}You may need to restart your shell or run 'exec su -l $USER' to apply group changes.${NORMAL}" | |
printf "\n\n" | |
printf "\n${YELLOW}You want zsh be your standard shell? (y/n)${NORMAL}\n" | |
read YESNO | |
if [ $YESNO = "y" ]; then | |
chsh -s /bin/zsh || { | |
echo "Error happened when i'm tryng to set zsh your default shell" | |
} | |
zsh && source ~/.zshrc | |
else | |
echo "okidoki for use zsh type zsh in your shell" | |
fi | |
unset YESNO | |
env zsh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment