automate/020_zsh_quickstart.sh

656 lines
19 KiB
Bash
Executable File

#!/usr/bin/env bash
set -x
set -Eeu -o -pipefail
# ND Do not put alias files in the script they are now in their own 020_script.
sudo apt install -y zsh nodejs fzf chroma powerline-go
# compaudit
# There are insecure directories: /usr/local/share/zsh/site-functions
# compaudit | xargs chmod g-w
# compaudit 2>&1 | sed -n '1d; p'
mkdir -p /usr/local/share/zsh/site-functions
chmod g-w /usr/local/share/zsh/site-functions
#DEST=$HOME
DEST=${1:-/etc/skel}
HOME="${DEST}"
# KEY_DIR=/etc/apt/keyrings
ALIAS_DIR=".zshrc.d"
# Install zgenom plugin manager and zsh quickstart to get a default setup for zsh
# .zgen-setup pulls zgenom automatically and puts it in .zqs-zgenom, plugins in .zgenom
if [ ! -d "${DEST}/zsh-quickstart-kit" ]; then
git clone https://github.com/unixorn/zsh-quickstart-kit "${DEST}"/zsh-quickstart-kit
else
echo "Quickstart kit already exists, skipping clone..."
fi
ln -rsf "${DEST}"/zsh-quickstart-kit/zsh/.zshrc "${DEST}/.zshrc"
ln -rsf "${DEST}"/zsh-quickstart-kit/zsh/.zsh_functions "${DEST}/.zsh_functions"
ln -rsf "${DEST}"/zsh-quickstart-kit/zsh/.zgen-setup "${DEST}/.zgen-setup"
mkdir -p "${DEST}/${ALIAS_DIR}"
echo "setopt PROMPT_SUBST" >>"${DEST}"/.zshrc
echo "NVIM_PYTHON_LOG_FILE=/var/tmp/nvim_python.log" >>"${DEST}/${ALIAS_DIR}/008_nvim.zsh"
echo "NVIM_PYTHON_LOG_LEVEL=DEBUG" >>"${DEST}/${ALIAS_DIR}/008_nvim.zsh"
sudo touch /var/tmp/nvim_python.log
# debians locate db locate
# sed -i 's,^\(export LOCATE_PATH=\).*,\1'/var/lib/mlocate/mlocate.db',' "${DEST}"/.zshrc
echo "export LOCATE_PATH=/var/lib/mlocate/mlocate.db" >"${DEST}/${ALIAS_DIR}/005_locatedb.zsh"
# Create a file named .zgen-local-plugins and add your zgenom load commands there.
# Don't forget to run `zgenom save` at the end of your .zgen-local-plugins file.
# Warning: .zgen-local-plugins REPLACES the starter list setup, it doesn't add to it.
# Comment out default docker plugin and replace with omz docker and compose plugins
sed -i -e "s|zgenom load srijanshetty/docker-zsh|# zgenom load srijanshetty/docker-zsh|" "${DEST}/zsh-quickstart-kit/zsh/.zgen-setup"
#sed -i '/\# zgenom load srijanshetty\/docker-zsh/a\ \ zgenom load ohmyzsh plugins\/docker\n\ \ zgenom load ohmyzsh plugins\/docker-compose' "${DEST}"/zsh-quickstart-kit/zsh/.zgen-setup
conf_print_fzf() {
cat <<'EOF'
# zsh quickstart created this file if missing expecting fzf to be installed in ${HOME}
# we have it installed via apt so the executables are in the path and the key-bindings
# and completitions are in examples fom the package.
# Setup fzf
# ---------
#if [[ ! "\$PATH" == *\${HOME}/.vim/bundle/fzf/bin/* ]]; then
# export PATH="\$PATH:\${HOME}/.vim/bundle/fzf/bin"
#fi
# auto-completion
# ---------------
[[ \$- == *i* ]] && source "/usr/share/doc/fzf/examples/completion.zsh" 2> /dev/null
# Key bindings
# ------------
source "/usr/share/doc/fzf/examples/key-bindings.zsh"
EOF
}
conf_print_fzf | tee "${DEST}/.fzf.zsh"
# The contents of the starter plugin function with changes above, pulling from upstream git with now not squash local changes.
conf_print_local_plugins() {
cat <<EOF
echo "creating a zgen save"
ZGEN_LOADED=()
ZGEN_COMPLETIONS=()
# Load ohmyzsh base
zgenom ohmyzsh
# zman fzf to quickly find and browse Zsh manuals in a webbrowser
zgenom load mattmc3/zman
# .zgen-local-plugins is sourced during startup *instead* of running .zgen-setup load-starter-plugin-list function,
# so make sure to include everything from this function that you want to keep.
# If zsh-syntax-highlighting is bundled after zsh-history-substring-search,
# they break, so get the order right.
zgenom load zsh-users/zsh-syntax-highlighting
zgenom load zsh-users/zsh-history-substring-search
zgenom load fdellwing/zsh-bat
# Set keystrokes for substring searching
zmodload zsh/terminfo
bindkey "\$terminfo[kcuu1]" history-substring-search-up
bindkey "\$terminfo[kcud1]" history-substring-search-down
# Tab complete rakefile targets
#zgenom load unixorn/rake-completion.zshplugin
# Automatically run zgen update and zgen selfupdate every 7 days
#zgenom load unixorn/autoupdate-zgen
zgenom load unixorn/jpb.zshplugin
# Colorize the things if you have grc installed. Well, some of the
# things, anyway.
zgenom load unixorn/warhol.plugin.zsh
#
# Auto-pair brackets etc (very lightweight)
zgenom load hlissner/zsh-autopair
# OS X helpers
zgenom load unixorn/tumult.plugin.zsh
# Warn you when you run a command that you've got an alias for
# zgenom load djui/alias-tips
# Add my collection of git helper scripts
zgenom load unixorn/git-extra-commands
# Add my bitbucket git helpers plugin
zgenom load unixorn/bitbucket-git-helpers.plugin.zsh
# A collection of scripts that might be useful to sysadmins
zgenom load skx/sysadmin-util
# Adds aliases to open your current repo & branch on github.
zgenom load peterhurford/git-it-on.zsh
# Tom Limoncelli's tooling for storing private information (keys, etc)
# in a repository securely by encrypting them with gnupg
zgenom load StackExchange/blackbox
# Load some ohmyzsh plugins
zgenom ohmyzsh plugins/aws
#zgenom ohmyzsh plugins/chruby
zgenom ohmyzsh plugins/colored-man-pages
zgenom ohmyzsh plugins/colorize
zgenom ohmyzsh plugins/git
zgenom ohmyzsh plugins/github
zgenom ohmyzsh plugins/golang
zgenom ohmyzsh plugins/pip
zgenom ohmyzsh plugins/python
zgenom ohmyzsh plugins/rsync
zgenom ohmyzsh plugins/tmux
zgenom ohmyzsh plugins/screen
zgenom ohmyzsh plugins/ssh-agent
zgenom ohmyzsh plugins/sudo
zgenom ohmyzsh plugins/vagrant
zgenom ohmyzsh plugins/docker
if [ \$(uname -a | grep -ci Darwin) = 1 ]; then
# Load OSX-specific plugins
zgenom ohmyzsh plugins/brew
zgenom ohmyzsh plugins/osx
fi
# pipx completitons
zgenom load thuandt/zsh-pipx
# A set of shell functions to make it easy to install small apps and
# utilities distributed with pip.
zgenom load sharat87/pip-app
zgenom load chrissicool/zsh-256color
# ZVM - A better and friendly vi(vim) mode plugin for ZSH.'
zgenom load jeffreytse/zsh-vi-mode
# Load more completion files for zsh from the zsh-lovers github repo
#zgenom load zsh-users/zsh-completions src
# aggregates zsh completions from: zsh-users/zsh-completions, zchee/zsh-completions,
# nilsonholger/osx-zsh-completions and various other custom or third-party sources.
zgenom load clarketm/zsh-completions
# Docker completion
# zgenom load srijanshetty/docker-zsh
#zgenom load akarzim/zsh-docker-aliases
#zgenom load robbyrussell/oh-my-zsh plugins/docker
#zgenom load robbyrussell/oh-my-zsh plugins/docker-compose
# Very cool plugin that generates zsh completion functions for commands
# if they have getopt-style help text. It doesn't generate them on the fly,
# you'll have to explicitly generate a completion, but it's still quite cool.
zgenom load RobSis/zsh-completion-generator
# Add Fish-like autosuggestions to your ZSH
zgenom load zsh-users/zsh-autosuggestions
# Add support for autoswitching conda/python venv
# zgenom load MichaelAquilina/zsh-autoswitch-virtualenv
# zgenom load bckim92/zsh-autoswitch-conda
# Add functional testing for zsh
# zgenom load molovo/revolver
# zgenom load olets/zsh-test-runner
# zgenom load zunit-zsh/zunit
# IDE-like type-ahead completion for Zsh, asynchronous/non-blocking
#zgenom load marlonrichert/zsh-autocomplete
# k is a zsh script / plugin to make directory listings more readable,
# adding a bit of color and some git status information on files and directories
zgenom load rimraf/k
# Bullet train prompt setup
#zgenom load caiogondim/bullet-train-ohmyzsh-theme bullet-train
#zgenom load robbyrussell/oh-my-zsh themes/agnoster
zgenom load oh-my-zsh themes/agnoster
## Load Prezto
# zgen prezto
## Load prezto plugins
# zgen prezto <modulename> <option> <value(s)>
# zgen prezto <modulename>
## prezto options
# zgen prezto editor key-bindings 'emacs'
# zgen prezto prompt theme 'sorin'
## prezto and modules
#zgen prezto
# zgen prezto git
# zgen prezto command-not-found
# zgen prezto syntax-highlighting
## plugins
# zgenom load/path/to/super-secret-private-plugin
# Load me last
GENCOMPL_FPATH=\$HOME/.zsh/complete
GENCOMPL_PY=python3
# Save it all to init script
zgenom save
EOF
}
conf_print_local_plugins | tee "${DEST}/.zgen-local-plugins"
# -- Additions to .zshrc.d --
# Dedup path
conf_print_dedup() {
cat <<-EOF
# Dedup the path, auto installers help themselves to you config files.'
typeset -U PATH path
EOF
}
conf_print_dedup | tee "${DEST}/${ALIAS_DIR}/zzz_dedup_path.zsh"
# Uopdates for omz, quickstart and zgenom all gets a bit much.
# prevent update
conf_print_qs_refresh() {
cat <<-EOF
# Use unset 'QUICKSTART_KIT_REFRESH_IN_DAYS' to disable.'
# QUICKSTART_KIT_REFRESH_IN_DAYS=0'
unset "QUICKSTART_KIT_REFRESH_IN_DAYS"
EOF
}
conf_print_qs_refresh | tee "${DEST}/${ALIAS_DIR}/001_quickstart-refresh.zsh"
#
echo '# zgenom autoupdate' >"${DEST}/${ALIAS_DIR}/001_zgenom-refresh.zsh"
# oh-my-zsh disable update check
echo 'DISABLE_AUTO_UPDATE="true"' >"${DEST}/${ALIAS_DIR}/001_auto-update-off.zsh"
# sed -i 's,^\(export LOCATE_PATH=\).*,\1'/var/lib/mlocate/mlocate.db',' ~/.zshrc
# -- Theme --
#
#
# Replace with powerline-go Use powerline-daemon to speedup the prompt.
# echo 'powerline-daemon -q' >"${DEST}/${ALIAS_DIR}/006_powerline-daemon.zsh"
# conf_print_powerline_daemon() {
# cat <<EOF
# if ! pgrep -x "powerline-daemon" > /dev/null; then
# powerline-daemon -q
# fi
# EOF
# }
# conf_print_powerline_daemon | tee "${DEST}/${ALIAS_DIR}/006_powerline-daemon.zsh"
conf_print_powerline_go() {
cat <<'EOF'
# Powerline-go setup for Zsh
function powerline_precmd() {
# 1. Find the binary
local P_GO=$(command -v powerline-go || echo "$GOPATH/bin/powerline-go")
# 2. Check if it exists before running
if [[ ! -x "$P_GO" ]]; then
return
fi
# 3. Use -eval to let powerline-go handle PS1 and RPS1
# Note: We've merged your desired modules and path-aliases here
eval "$($P_GO \
-error $? \
-shell zsh \
-eval \
-modules "user,host,ssh,cwd,perms,git,venv,aws,duration,exit,newline" \
--cwd-max-depth 3 \
--cwd-max-dir-size 7 \
-theme ${HOME}/.config/powerline-go/catppuccin-mocha.json)"
}
function install_powerline_precmd() {
for s in "${precmd_functions[@]}"; do
if [ "$s" = "powerline_precmd" ]; then
return
fi
done
precmd_functions+=(powerline_precmd)
}
# Avoid loading in basic linux TTY to prevent broken glyphs
if [ "$TERM" != "linux" ]; then
install_powerline_precmd
fi
EOF
}
conf_print_powerline_go | tee "${DEST}/${ALIAS_DIR}/006_powerline-go.zsh"
mkdir -p "${DEST}/.config/powerline-go"
conf_print_powerline_go_theme() {
cat <<EOF
{
"BoldForeground": false,
"Reset": 0,
"UsernameFg": 189,
"UsernameBg": 60,
"UsernameRootBg": 161,
"HostnameFg": 189,
"HostnameBg": 60,
"HomeSpecialDisplay": true,
"HomeFg": 231,
"HomeBg": 111,
"PathFg": 189,
"PathBg": 237,
"CwdFg": 231,
"SeparatorFg": 240,
"ReadonlyFg": 161,
"ReadonlyBg": 235,
"SshFg": 231,
"SshBg": 173,
"RepoCleanFg": 16,
"RepoCleanBg": 114,
"RepoDirtyFg": 231,
"RepoDirtyBg": 161,
"GitAheadFg": 231,
"GitAheadBg": 60,
"GitBehindFg": 231,
"GitBehindBg": 60,
"GitStagedFg": 16,
"GitStagedBg": 114,
"GitNotStagedFg": 231,
"GitNotStagedBg": 173,
"GitUntrackedFg": 231,
"GitUntrackedBg": 180,
"GitConflictedFg": 231,
"GitConflictedBg": 161,
"VirtualEnvFg": 16,
"VirtualEnvBg": 180,
"TimeFg": 189,
"TimeBg": 236,
"DurationFg": 189,
"DurationBg": 237,
"CmdPassedFg": 114,
"CmdPassedBg": 236,
"CmdFailedFg": 161,
"CmdFailedBg": 236
}
EOF
}
conf_print_powerline_go_theme | tee "${DEST}/.config/powerline-go/catppuccin-mocha.json"
conf_print_ls_colors() {
cat <<'EOF'
# Catppuccin Mocha LS_COLORS
# This sets colors for both GNU ls and entries in zsh completion
# Base colors
export LS_COLORS="di=34:ln=36:so=32:pi=33:ex=31:bd=34;46:cd=34;43:su=30;41:sg=30;46:tw=30;42:ow=30;44:"
# Archives (Lavender)
export LS_COLORS=$LS_COLORS"*.tar=35:*.tgz=35:*.arc=35:*.zip=35:*.z=35:*.Z=35:*.gz=35:*.bz2=35:*.bz=35:*.tz=35:*.rpm=35:*.deb=35:"
# Images (Teal)
export LS_COLORS=$LS_COLORS"*.jpg=36:*.jpeg=36:*.gif=36:*.bmp=36:*.tga=36:*.svg=36:*.png=36:*.webp=36:"
# Media (Maroon)
export LS_COLORS=$LS_COLORS"*.mp4=31:*.mkv=31:*.mp3=31:*.wav=31:*.flac=31:"
alias ls='ls --color=auto'
# Sync Zsh completions with LS_COLORS
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
EOF
}
conf_print_ls_colors | tee "${DEST}/${ALIAS_DIR}/002_ls_colors.zsh"
# -- End Theme --
sudo sed -i 's,^\(CHARMAP=\).*,\1'\"UTF-8\"',' /etc/default/console-setup
# Removed infavor of powerline-go | set theme for oh-my-zsh
conf_print_theme() {
cat <<-EOF
# https://zshthem.es/browse-zsh-themes/
ZSH_THEME="agnoster" # powerline based theme
#ZSH_THEME="3den"
#ZSH_THEME="Pure"
EOF
}
# conf_print_theme | tee "${DEST}/${ALIAS_DIR}/002_theme.zsh"
# set colorize tool
conf_print_coloize() {
cat <<-EOF
ZSH_COLORIZE_TOOL="chroma"
ZSH_COLORIZE_CHROMA_FORMATTER=terminal256
EOF
}
conf_print_coloize | tee "${DEST}/${ALIAS_DIR}/003_colorize.zsh"
# see also ZVM jeffreytse/zsh-vi-mode plugin
conf_print_vi_mode() {
cat <<-'EOF'
# Set vi mode
bindkey -v
bindkey '^R' history-incremental-search-backward
# ZVM settings - https://github.com/jeffreytse/zsh-vi-mode/blob/master/zsh-vi-mode.zsh
# Always starting with insert mode for each command line
ZVM_LINE_INIT_MODE=$ZVM_MODE_INSERT
# # Change to Zsh's default readkey engine (default for zvm is NEX)
# ZVM_READKEY_ENGINE=$ZVM_READKEY_ENGINE_ZLE
# The plugin will auto execute this zvm_after_select_vi_mode function
# function zvm_after_select_vi_mode() {
# case $ZVM_MODE in
# $ZVM_MODE_NORMAL)
# # Something you want to do...
# ;;
# $ZVM_MODE_INSERT)
# # Something you want to do...
# ;;
# $ZVM_MODE_VISUAL)
# # Something you want to do...
# ;;
# $ZVM_MODE_VISUAL_LINE)
# # Something you want to do...
# ;;
# $ZVM_MODE_REPLACE)
# # Something you want to do...
# ;;
# esac
# }
# Change cursor style for different vi modes
ZVM_INSERT_MODE_CURSOR=$ZVM_CURSOR_BEAM
ZVM_NORMAL_MODE_CURSOR=$ZVM_CURSOR_BLOCK
ZVM_OPPEND_MODE_CURSOR=$ZVM_CURSOR_UNDERLINE
EOF
}
conf_print_vi_mode | tee "${DEST}/${ALIAS_DIR}/001_vi-mode.sh"
#git clone http://github.com/bhilburn/powerlevel9k.git "${DEST}"/.zgen/robbyrussell/ohmyzsh-master/themes/powerlevel9k
## add a separate zsh aliases directory
conf_print_enable_alias_dir() {
cat <<EOF
# Enable ${ALIAS_DIR} to keep aliases separate from env variables
if [ -d ~/.zsh_aliases.d ]; then
for f in ~/.zsh_aliases.d/**/*.zsh(N); do
source "$f"
done
fi
EOF
}
conf_print_enable_alias_dir | tee "${DEST}/${ALIAS_DIR}/000_enable-aliases-directory.zsh"
ALIASES_HOME=".zsh_aliases.d"
mkdir -p "${DEST}/${ALIASES_HOME}"
# Override EDITOR and VISUAL set be quickstart in its:
# zsh-quickstart-kit/zsh/.zsh_aliases
conf_print_editor() {
cat <<'EOF'
if [ -x /usr/bin/nvim ]; then
alias vi="/usr/bin/nvim"
alias vim="/usr/bin/nvim"
export EDITOR="/usr/bin/nvim"
fi
if [ -x "$(command -v neovide)" ]; then
VISUAL=$(command -v neovide)
export VISUAL
fi
EOF
}
conf_print_editor | tee "${DEST}/${ALIASES_HOME}/001_editor.zsh"
# Set the path to the pipx bin directory
cat <<'EOF' | tee "${DEST}/${ALIAS_DIR}/001_pipx-path.zsh"
export PATH="$PATH:$HOME/.local/bin"
EOF
# tell bash to check the next word after the alias (i.e sudo) by adding a space to the end of the alias value.
echo "# tell bash to check the next word after the alias (i.e sudo) by adding a space to the end of the alias value." >"${DEST}/${ALIAS_DIR}/002_sudo.zsh"
echo 'alias sudo="sudo "' >>"${DEST}/${ALIASES_HOME}/002_sudo.zsh"
conf_print_py_aliases() {
cat <<EOF
_py_version() {
PY_VERSIONS=(2 3)
LINE=(\${(s: :)history[\$HISTCMD]})
COMMAND=\${LINE[1]}
ARGS=\${(j: :)LINE[2,-1]}
while (( 1 )) {
read -s -k1 VERSION"?\$COMMAND [\${PY_VERSIONS[1]}]: "
if [[ \$VERSION == $'\n' ]]; then
VERSION=\${PY_VERSIONS[1]}
break
elif (( \${PY_VERSIONS[(I)\$VERSION]} )); then
echo \$VERSION
break
fi
echo "\$VERSION is not supported (\${(j:, :)PY_VERSIONS})"
}
if [[ -x \$(which \$COMMAND\$VERSION) ]]; then
COMMAND=\$COMMAND\$VERSION
else
COMMAND="python\$VERSION \$(which -a \$COMMAND | grep -v aliased | head -n 1)"
fi
eval \$COMMAND \$ARGS
}
# versioned python commands
# alias python="_py_version"
# alias pip="_py_version"
# alias pydoc="_py_version"
# alias idle="_py_version"
# alias ipython="_py_version"
# alias jupyter="_py_version"
EOF
}
conf_print_py_aliases | tee "${DEST}/${ALIASES_HOME}/003_py_aliases.zsh"
mkdir -p "${DEST}/${ALIAS_DIR}/"
conf_print_transfer() {
cat <<EOF
#
# Defines transfer alias and provides easy command line file and folder sharing.
#
# Authors:
# Remco Verhoef <remco@dutchcoders.io>
#
curl --version 2>&1 > /dev/null
if [ \$? -ne 0 ]; then
echo "Could not find curl."
return 1
fi
transfer() {
# check arguments
if [ \$# -eq 0 ];
then
echo -e "No arguments specified.\n\nUsage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"
return 1
fi
# get temporarily filename, output is written to this file show progress can be showed
tmpfile=\$( mktemp -t transferXXX )
# upload stdin or file
file=\$1
if tty -s;
then
basefile=\$(basename "\$file" | sed -e 's/[^a-zA-Z0-9._-]/-/g')
if [ ! -e \$file ];
then
echo "File \$file doesn't exists."
return 1
fi
if [ -d \$file ];
then
# zip directory and transfer
zipfile=\$( mktemp -t transferXXX.zip )
cd \$(dirname \$file) && zip -r -q - \$(basename \$file) >> \$zipfile
curl --progress-bar --upload-file "\$zipfile" "https://transfer.sh/\$basefile.zip" >> \$tmpfile
rm -f \$zipfile
else
# transfer file
curl --progress-bar --upload-file "\$file" "https://transfer.sh/\$basefile" >> \$tmpfile
fi
else
# transfer pipe
curl --progress-bar --upload-file "-" "https://transfer.sh/\$file" >> \$tmpfile
fi
# cat output link
cat \$tmpfile
# log file link
APPLICATION="\${0##*/}"
RIGHTNOW="\$(date)"
EXPIRES="\$(date -d "+14 days")"
echo -e "\$(cat "\$tmpfile") - uploaded \$RIGHTNOW - expires \$EXPIRES\n" >> ~/\$APPLICATION.log
echo "\n\nSee ~/\$APPLICATION.log for all transfers.\n"
# cleanup
rm -f \$tmpfile
}
EOF
}
conf_print_transfer | tee "${DEST}/${ALIAS_DIR}/003_transfer.zsh"
conf_print_nvm() {
cat <<EOF
plugins+=(nvm)
zstyle ':omz:plugins:nvm' lazy yes
EOF
}
conf_print_nvm | tee "${DEST}/${ALIAS_DIR}/002_nvm.zsh"
# https://github.com/catppuccin/fzf/blob/main/themes/catppuccin-fzf-mocha.sh
conf_print_fzf_colors() {
cat <<EOF
export FZF_DEFAULT_OPTS=" \
--color=bg+:#313244,bg:#1E1E2E,spinner:#F5E0DC,hl:#F38BA8 \
--color=fg:#CDD6F4,header:#F38BA8,info:#CBA6F7,pointer:#F5E0DC \
--color=marker:#B4BEFE,fg+:#CDD6F4,prompt:#CBA6F7,hl+:#F38BA8 \
--color=selected-bg:#45475A \
--color=border:#6C7086,label:#CDD6F4"
EOF
}
conf_print_fzf_colors | tee "${DEST}/${ALIAS_DIR}/002_fzf_colors.zsh"
sudo chown -R root:root /usr/local/share/zsh/site-functions
sudo chmod -R 755 /usr/local/share/zsh/site-functions