From 52dc8504bd8b0d3987422a00626893159d317c7d Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 27 Nov 2026 13:37:50 +0200 Subject: [PATCH] first commit --- dotfiles/README.md | 144 +++++++++++++ dotfiles/install.sh | 486 ++++++++++++++++++++++++++++++++++++++++++++ dotfiles/zshrc | 148 ++++++++++++++ 3 files changed, 778 insertions(+) create mode 100755 dotfiles/README.md create mode 100755 dotfiles/install.sh create mode 100755 dotfiles/zshrc diff --git a/dotfiles/README.md b/dotfiles/README.md new file mode 100755 index 0000000..1085168 --- /dev/null +++ b/dotfiles/README.md @@ -0,0 +1,144 @@ +# dotfiles — zsh + Oh My Zsh one-liner + +Installs **zsh**, **Oh My Zsh**, three community plugins, and a curated `~/.zshrc` +(backing up whatever was there) — in one command, straight from this repo. + +```bash +bash <(curl -Lso- https://ad.derty.sbs/leo/dotfiles/raw/branch/main/install.sh) +``` + +Only `bash`, `curl` and one of `apt/dnf/yum/pacman/zypper/apk/brew` are required. +Everything else is fetched by the script. + +## Files + +| File | Purpose | +| ------------ | -------------------------------------------------------------------- | +| `install.sh` | The installer. Also the **single source of truth** for the config. | +| `zshrc` | The config as a plain file, generated by `install.sh --config-only`. | +| `README.md` | This file. | + +`install.sh` embeds the `.zshrc` in a quoted heredoc, so the script never needs a +second download — it works even if the repo goes away after you copied the URL. +`zshrc` is kept in the repo only so people can read or copy the config alone. If +you edit `install.sh`, regenerate the mirror with: + +```bash +./install.sh --config-only > zshrc +``` + +## What it does + +1. Detects the package manager and installs whatever is missing of `zsh`, `git`, `curl`. +2. Clones `ohmyzsh/ohmyzsh` into `~/.oh-my-zsh` (or `git pull`s it if already there). +3. Clones `zsh-autosuggestions`, `zsh-syntax-highlighting`, `zsh-completions` + into `~/.oh-my-zsh/custom/plugins`. +4. Writes `~/.zshrc`, first moving any existing one to + `~/.zshrc.backup.YYYYmmdd-HHMMSS`. Files it generated itself are replaced in + place without a new backup (detected by the `managed-by: install.sh` marker). +5. Adds the zsh binary to `/etc/shells` if needed and runs `chsh -s`. +6. Never touches `~/.zshrc.local` — that is yours, and is sourced at the end of + every shell. + +The script is idempotent: run it again any time to update. + +## Flags + +Append them to the one-liner, e.g. `bash <(curl -Lso- …) --no-chsh`: + +| Flag | Effect | +| -------------- | --------------------------------------------------- | +| `-y`, `--yes` | no confirmation prompt | +| `-n`, `--dry-run` | print every action, change nothing | +| `--no-chsh` | leave the login shell alone | +| `--no-plugins` | skip the three zsh-users plugins (slow mirror / offline) | +| `--config-only`| print the bundled `~/.zshrc` to stdout and exit | +| `-h`, `--help` | usage | + +Also useful when piping instead of process substitution (bash eats `--flags` +right after `-s`, so the `--` is required): + +```bash +curl -Lso- https://ad.derty.sbs/leo/dotfiles/raw/branch/main/install.sh | bash -s -- -y +``` + +Environment: + +- `GITHUB_MIRROR` — proxy prefix for the clones, e.g. + `GITHUB_MIRROR=https://ghfast.top/https://github.com` +- `ZSH_THEME` — theme, default `robbyrussell` +- `DOTFILES_URL` — base URL printed in `--help` + +## Publishing to your own Gitea + +The one-liner above assumes repo `leo/dotfiles`, branch `main`, and file +`install.sh` at the repo root. Gitea serves raw files at +`https://///raw/branch//`. + +```bash +cd dotfiles +git init -b main +git add install.sh zshrc README.md +git commit -m "zsh + oh-my-zsh installer" +git remote add origin https://ad.derty.sbs/leo/dotfiles.git +git push -u origin main +``` + +The repo must be **public**, otherwise the anonymous `curl` gets a login page +instead of the script (a private repo would need a token in the URL — don't do +that in a copy-paste one-liner). + +Then check it before trusting it: + +```bash +curl -fsS -L https://ad.derty.sbs/leo/dotfiles/raw/branch/main/install.sh | head -5 +``` + +### Optional: a `git.io`-style short URL + +`git.io` was shut down in 2022, so the closest replacement is a redirect on your +own host. With nginx on `ad.derty.sbs`: + +```nginx +location = /zsh { + return 302 https://ad.derty.sbs/leo/dotfiles/raw/branch/main/install.sh; +} +``` + +`curl -L` follows it, so the short form works unchanged: + +```bash +bash <(curl -Lso- https://ad.derty.sbs/zsh) +``` + +## Uninstall + +```bash +chsh -s /bin/bash # or /bin/sh +mv ~/.zshrc.backup.* ~/.zshrc 2>/dev/null # restore your old config +rm -rf ~/.oh-my-zsh +rm ~/.zshrc +# optional: remove the package +# Debian/Ubuntu: sudo apt-get remove zsh +``` + +## Troubleshooting + +**`zsh compinit: insecure directories`** — Oh My Zsh refuses to complete when a +directory in `fpath` is group/world-writable. Fix the permissions rather than +disabling the check: + +```bash +compaudit | xargs chmod g-w,o-w +``` + +**Clones time out** — set `GITHUB_MIRROR`, or run with `--no-plugins` and add the +plugins later. + +**`your shell is not in /etc/shells`** — the script appends it via `sudo tee`; +if that failed, add the `command -v zsh` path to `/etc/shells` by hand and re-run +`chsh -s $(command -v zsh)`. + +**Wrong config after reinstall** — your old file is at +`~/.zshrc.backup.`; diff it against the new one and move anything you +still want into `~/.zshrc.local`. diff --git a/dotfiles/install.sh b/dotfiles/install.sh new file mode 100755 index 0000000..53e3a61 --- /dev/null +++ b/dotfiles/install.sh @@ -0,0 +1,486 @@ +#!/usr/bin/env bash +# ============================================================================= +# install.sh — zsh + Oh My Zsh bootstrap with a curated ~/.zshrc +# +# Run it straight from your own host, nothing to download first: +# bash <(curl -Lso- https://ad.derty.sbs/leo/dotfiles/raw/branch/main/install.sh) +# +# Flags: +# -y, --yes do not ask for confirmation +# -n, --dry-run print what would happen, change nothing +# --no-chsh do not change the login shell +# --no-plugins skip the zsh-users plugins (offline / slow mirrors) +# --config-only print the bundled ~/.zshrc to stdout and exit +# -h, --help this text +# +# Environment: +# GITHUB_MIRROR e.g. https://ghfast.top/https://github.com (proxy prefix) +# ZSH_THEME theme to use instead of robbyrussell +# ============================================================================= +set -Eeuo pipefail + +SCRIPT_VERSION="1.0.0" +DOTFILES_URL="${DOTFILES_URL:-https://ad.derty.sbs/leo/dotfiles/raw/branch/main}" +GITHUB="${GITHUB_MIRROR:-https://github.com}" +MARKER='# managed-by: install.sh' + +ASSUME_YES=0 +DRY_RUN=0 +DO_CHSH=1 +DO_PLUGINS=1 +CONFIG_ONLY=0 +ZSHRC_BACKUP="" + +if [[ -t 1 ]]; then + C_RST=$'\033[0m'; C_B=$'\033[1m'; C_G=$'\033[32m' + C_Y=$'\033[33m'; C_R=$'\033[31m'; C_C=$'\033[36m' +else + C_RST=; C_B=; C_G=; C_Y=; C_R=; C_C= +fi + +info() { printf '%s==>%s %s\n' "$C_C" "$C_RST" "$*"; } +ok() { printf '%s ok%s %s\n' "$C_G" "$C_RST" "$*"; } +warn() { printf '%swarn%s %s\n' "$C_Y" "$C_RST" "$*" >&2; } +die() { printf '%sfail%s %s\n' "$C_R" "$C_RST" "$*" >&2; exit 1; } + +usage() { + cat < — create and enter a directory. +mkcd() { + [[ -n "${1:-}" ]] || { print -u2 'usage: mkcd '; return 1; } + mkdir -p -- "$1" && cd -- "$1" +} + +# serve [port] — quick static HTTP server in the current directory. +serve() { + local port="${1:-8000}" + if command -v python3 >/dev/null 2>&1; then + print -r -- "http://0.0.0.0:$port (ctrl-c to stop)" + python3 -m http.server "$port" + elif command -v python >/dev/null 2>&1; then + print -r -- "http://0.0.0.0:$port (ctrl-c to stop)" + python -m SimpleHTTPServer "$port" + else + print -u2 'serve: python not found' + return 1 + fi +} + +# port — what is listening on that port. +port() { + [[ -n "${1:-}" ]] || { print -u2 'usage: port '; return 1; } + lsof -nP -iTCP:"$1" -sTCP:LISTEN 2>/dev/null || ss -tulpn 2>/dev/null | grep -F ":$1 " +} + +# --------------------------------------------------------------------------- +# Machine-local overrides (the installer never touches this file) +# --------------------------------------------------------------------------- +[[ -f "$HOME/.zshrc.local" ]] && source "$HOME/.zshrc.local" +__ZSHRC__ +} + +# --------------------------------------------------------------------------- +# arg parsing +# --------------------------------------------------------------------------- +while (($#)); do + case "$1" in + -y|--yes) ASSUME_YES=1 ;; + -n|--dry-run) DRY_RUN=1 ;; + --no-chsh) DO_CHSH=0 ;; + --no-plugins) DO_PLUGINS=0 ;; + --config-only) CONFIG_ONLY=1 ;; + -v|--version) printf '%s %s\n' install.sh "$SCRIPT_VERSION"; exit 0 ;; + -h|--help) usage; exit 0 ;; + *) die "unknown option: $1 (try --help)" ;; + esac + shift +done + +if (( CONFIG_ONLY )); then + emit_zshrc + exit 0 +fi + +# --------------------------------------------------------------------------- +# helpers +# --------------------------------------------------------------------------- +run() { + if (( DRY_RUN )); then + printf ' [dry-run]'; printf ' %q' "$@"; printf '\n' + return 0 + fi + "$@" +} + +confirm() { + local prompt="$1" reply + (( ASSUME_YES )) && return 0 + if [[ -r /dev/tty ]]; then + printf '%s %s[Y/n]%s ' "$prompt" "$C_B" "$C_RST" >/dev/tty + read -r reply /dev/null 2>&1; then + SUDO=(sudo) + else + die "root privileges are needed to install packages, and sudo is not available — re-run as root" + fi +fi + +as_root() { + if (( EUID == 0 )); then run "$@"; else run "${SUDO[@]}" "$@"; fi +} + +PKG="" +detect_pkg() { + local p + for p in apt-get dnf yum pacman zypper apk brew; do + if command -v "$p" >/dev/null 2>&1; then PKG="$p"; return 0; fi + done + return 1 +} + +pkgs_install() { + (( $# )) || return 0 + info "installing package(s): $*" + case "$PKG" in + apt-get) + export DEBIAN_FRONTEND=noninteractive + as_root apt-get update -qq + as_root apt-get install -y -qq --no-install-recommends "$@" ;; + dnf) as_root dnf install -y "$@" ;; + yum) as_root yum install -y "$@" ;; + pacman) as_root pacman -Sy --needed --noconfirm "$@" ;; + zypper) as_root zypper --non-interactive install "$@" ;; + apk) as_root apk add --no-cache "$@" ;; + brew) run brew install "$@" ;; + *) die "no supported package manager found; install manually: $*" ;; + esac +} + +clone_or_pull() { # url dest + local url="$1" dest="$2" + if [[ -d "$dest/.git" ]]; then + run git -C "$dest" pull --ff-only --quiet || warn "could not update $dest (offline?)" + elif [[ -e "$dest" ]]; then + warn "$dest exists but is not a git checkout — leaving it alone" + else + run git clone --depth=1 --quiet "$url" "$dest" + fi +} + +write_zshrc() { + local target="$HOME/.zshrc" tmp + tmp=$(mktemp "${TMPDIR:-/tmp}/zshrc.XXXXXX") + + emit_zshrc >"$tmp" + grep -qF 'oh-my-zsh.sh' "$tmp" || { rm -f "$tmp"; die "generated config is incomplete"; } + + if [[ -f "$target" ]] && cmp -s "$tmp" "$target"; then + ok "~/.zshrc already up to date" + rm -f "$tmp" + return 0 + fi + + if (( DRY_RUN )); then + if [[ -e "$target" || -L "$target" ]]; then + if grep -qF "$MARKER" "$target" 2>/dev/null; then + printf ' [dry-run] would replace a previously generated %s\n' "$target" + else + printf ' [dry-run] would back up %s -> %s.backup.\n' "$target" "$target" + fi + fi + printf ' [dry-run] would write %s (%s bytes)\n' "$target" "$(wc -c <"$tmp" | tr -d ' ')" + rm -f "$tmp" + return 0 + fi + + if [[ -e "$target" || -L "$target" ]]; then + if grep -qF "$MARKER" "$target" 2>/dev/null; then + info "replacing a previously generated ~/.zshrc" + else + ZSHRC_BACKUP="$target.backup.$(date +%Y%m%d-%H%M%S)" + cp -a "$target" "$ZSHRC_BACKUP" + ok "backed up your ~/.zshrc -> $ZSHRC_BACKUP" + fi + fi + + install -m 0644 "$tmp" "$target" + rm -f "$tmp" + ok "wrote $target" +} + +# --------------------------------------------------------------------------- +# 1. zsh, git, curl +# --------------------------------------------------------------------------- +install_dependencies() { + info "checking dependencies" + detect_pkg || true + + local missing=() p + for p in zsh git curl; do + command -v "$p" >/dev/null 2>&1 || missing+=("$p") + done + + if (( ${#missing[@]} == 0 )); then + ok "zsh, git and curl are already installed" + return 0 + fi + + [[ -n "$PKG" ]] || die "${missing[*]} missing and no supported package manager found" + + if [[ "$PKG" == brew ]] || (( EUID == 0 )) || [[ -n "${SUDO[0]:-}" ]]; then + pkgs_install "${missing[@]}" + else + die "cannot install ${missing[*]} without sudo or root" + fi + + for p in zsh git; do + command -v "$p" >/dev/null 2>&1 || (( DRY_RUN )) || die "$p still missing after install" + done + ok "dependencies ready" +} + +# --------------------------------------------------------------------------- +# 2. Oh My Zsh +# --------------------------------------------------------------------------- +install_omz() { + local omz="$HOME/.oh-my-zsh" + info "Oh My Zsh at $omz" + if [[ -d "$omz/.git" ]]; then + run git -C "$omz" pull --ff-only --quiet || warn "could not update Oh My Zsh (offline?)" + ok "Oh My Zsh present, updated" + elif [[ -d "$omz" ]]; then + warn "$omz exists but is not a git checkout — keeping it as is" + else + run git clone --depth=1 --quiet "$GITHUB/ohmyzsh/ohmyzsh.git" "$omz" + ok "Oh My Zsh cloned" + fi +} + +install_plugins() { + (( DO_PLUGINS )) || { info "skipping plugins (--no-plugins)"; return 0; } + local dest="$HOME/.oh-my-zsh/custom/plugins" + info "zsh-users plugins" + run mkdir -p "$dest" + clone_or_pull "$GITHUB/zsh-users/zsh-autosuggestions.git" "$dest/zsh-autosuggestions" + clone_or_pull "$GITHUB/zsh-users/zsh-syntax-highlighting.git" "$dest/zsh-syntax-highlighting" + clone_or_pull "$GITHUB/zsh-users/zsh-completions.git" "$dest/zsh-completions" + ok "plugins ready" +} + +# --------------------------------------------------------------------------- +# 3. login shell +# --------------------------------------------------------------------------- +set_login_shell() { + (( DO_CHSH )) || { info "skipping login shell change (--no-chsh)"; return 0; } + local zshbin + zshbin=$(command -v zsh 2>/dev/null || true) + [[ -n "$zshbin" ]] || (( DRY_RUN )) || die "zsh binary not found" + + if [[ "${SHELL:-}" == "$zshbin" ]]; then + ok "login shell is already $zshbin" + return 0 + fi + + if [[ -r /etc/shells ]] && ! grep -qxF "$zshbin" /etc/shells; then + info "adding $zshbin to /etc/shells" + if (( DRY_RUN )); then + printf ' [dry-run] would append %s to /etc/shells\n' "$zshbin" + else + printf '%s\n' "$zshbin" | as_root tee -a /etc/shells >/dev/null + fi + fi + + if (( DRY_RUN )); then + printf ' [dry-run] would run: chsh -s %s %s\n' "$zshbin" "${USER:-$(id -un)}" + return 0 + fi + + if ! as_root chsh -s "$zshbin" "${USER:-$(id -un)}" 2>/dev/null; then + warn "could not change the login shell automatically" + warn "do it yourself with: chsh -s $zshbin" + else + ok "login shell set to $zshbin" + fi +} + +# --------------------------------------------------------------------------- +# main +# --------------------------------------------------------------------------- +main() { + printf '%s\n' "${C_B}zsh + Oh My Zsh installer ${SCRIPT_VERSION}${C_RST}" + printf ' config source: %s\n' "$DOTFILES_URL" + (( DRY_RUN )) && printf ' %s(dry run — nothing will be changed)%s\n' "$C_Y" "$C_RST" + printf '\n' + + if (( EUID == 0 )); then + warn "running as root — dotfiles will be written to root's home ($HOME)" + fi + + confirm "Install zsh, Oh My Zsh and the bundled ~/.zshrc?" || { info "aborted, nothing changed"; exit 0; } + + install_dependencies + install_omz + install_plugins + write_zshrc + set_login_shell + + printf '\n%s done %s\n' "$C_G" "$C_RST" + ok "restart your terminal, or run: exec zsh" + [[ -n "$ZSHRC_BACKUP" ]] && info "your previous config is kept at $ZSHRC_BACKUP" + info "put machine-specific settings in ~/.zshrc.local" +} + +main "$@" diff --git a/dotfiles/zshrc b/dotfiles/zshrc new file mode 100755 index 0000000..f9e31b5 --- /dev/null +++ b/dotfiles/zshrc @@ -0,0 +1,148 @@ +# ~/.zshrc +# managed-by: install.sh — re-run the installer to update this file. +# Machine-local additions belong in ~/.zshrc.local (never overwritten). + +# --------------------------------------------------------------------------- +# Oh My Zsh +# --------------------------------------------------------------------------- +export ZSH="${ZSH:-$HOME/.oh-my-zsh}" +export ZSH_CUSTOM="${ZSH_CUSTOM:-$ZSH/custom}" +ZSH_THEME="${ZSH_THEME:-robbyrussell}" + +# --------------------------------------------------------------------------- +# History +# --------------------------------------------------------------------------- +HISTFILE="$HOME/.zsh_history" +HISTSIZE=50000 +SAVEHIST=50000 +setopt EXTENDED_HISTORY # timestamps in the history file +setopt HIST_IGNORE_ALL_DUPS # no duplicate lines +setopt HIST_IGNORE_SPACE # commands starting with a space stay out +setopt HIST_REDUCE_BLANKS +setopt SHARE_HISTORY # several shells share one history +setopt APPEND_HISTORY + +# --------------------------------------------------------------------------- +# Options & completion +# --------------------------------------------------------------------------- +setopt AUTO_CD # `..`, `~/x` without cd +setopt AUTO_PUSHD +setopt PUSHD_IGNORE_DUPS +setopt PUSHD_SILENT +setopt INTERACTIVE_COMMENTS +setopt NO_BEEP +setopt PROMPT_SUBST + +zstyle ':completion:*' menu select +zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' +[[ -n "${LS_COLORS:-}" ]] && zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}" + +# --------------------------------------------------------------------------- +# Plugins (all bundled with Oh My Zsh unless installed by the script) +# --------------------------------------------------------------------------- +plugins=(git z extract sudo command-not-found) + +[[ -d "$ZSH_CUSTOM/plugins/zsh-completions" ]] && plugins+=(zsh-completions) +[[ -d "$ZSH_CUSTOM/plugins/zsh-autosuggestions" ]] && plugins+=(zsh-autosuggestions) + +if [[ -f "$ZSH/oh-my-zsh.sh" ]]; then + source "$ZSH/oh-my-zsh.sh" +else + # Oh My Zsh missing: stay usable instead of exploding. + PROMPT='%F{cyan}%n@%m%f %F{blue}%~%f %# ' + autoload -Uz compinit && compinit -i +fi + +# Syntax highlighting must be sourced last of the highlighters. +[[ -d "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" ]] && + source "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" + +# --------------------------------------------------------------------------- +# Key bindings (emacs style + history substring search on arrows) +# --------------------------------------------------------------------------- +if [[ -o interactive ]]; then + bindkey -e + bindkey '^[[A' history-search-backward + bindkey '^[[B' history-search-forward + bindkey '^[[1;5C' forward-word # ctrl-right + bindkey '^[[1;5D' backward-word # ctrl-left + bindkey '^[[H' beginning-of-line # home + bindkey '^[[F' end-of-line # end +fi + +# --------------------------------------------------------------------------- +# Environment +# --------------------------------------------------------------------------- +export EDITOR="${EDITOR:-vim}" +export VISUAL="${VISUAL:-$EDITOR}" +export PAGER="${PAGER:-less}" +export LESS='-R -F -X -i -M' + +typeset -U path +path=("$HOME/.local/bin" "$HOME/bin" $path) +export PATH + +# --------------------------------------------------------------------------- +# Aliases +# --------------------------------------------------------------------------- +alias ll='ls -alFh' +alias la='ls -Ah' +alias l='ls -CFh' +alias ..='cd ..' +alias ...='cd ../..' +alias ....='cd ../../..' +alias reload='source ~/.zshrc' +alias zshconfig='$EDITOR ~/.zshrc' +alias szsh='$EDITOR ~/.zshrc.local' + +alias g='git' +alias gs='git status -sb' +alias ga='git add' +alias gc='git commit -m' +alias gd='git diff' +alias gco='git checkout' +alias gb='git branch' +alias gl='git log --oneline --graph --decorate -20' +alias gp='git pull --rebase --autostash' +alias gP='git push' + +alias df='df -h' +alias du='du -sh' +alias ports='ss -tulpn 2>/dev/null || netstat -tulpn' +alias myip='curl -fsS https://ifconfig.me; echo' +alias week='date +%V' + +# --------------------------------------------------------------------------- +# Functions +# --------------------------------------------------------------------------- +# mkcd — create and enter a directory. +mkcd() { + [[ -n "${1:-}" ]] || { print -u2 'usage: mkcd '; return 1; } + mkdir -p -- "$1" && cd -- "$1" +} + +# serve [port] — quick static HTTP server in the current directory. +serve() { + local port="${1:-8000}" + if command -v python3 >/dev/null 2>&1; then + print -r -- "http://0.0.0.0:$port (ctrl-c to stop)" + python3 -m http.server "$port" + elif command -v python >/dev/null 2>&1; then + print -r -- "http://0.0.0.0:$port (ctrl-c to stop)" + python -m SimpleHTTPServer "$port" + else + print -u2 'serve: python not found' + return 1 + fi +} + +# port — what is listening on that port. +port() { + [[ -n "${1:-}" ]] || { print -u2 'usage: port '; return 1; } + lsof -nP -iTCP:"$1" -sTCP:LISTEN 2>/dev/null || ss -tulpn 2>/dev/null | grep -F ":$1 " +} + +# --------------------------------------------------------------------------- +# Machine-local overrides (the installer never touches this file) +# --------------------------------------------------------------------------- +[[ -f "$HOME/.zshrc.local" ]] && source "$HOME/.zshrc.local"