From b525faea7eaf3df35964761d5f8b76f74dcc98e8 Mon Sep 17 00:00:00 2001 From: naoto Date: Fri, 9 Aug 2024 22:39:10 +0200 Subject: [PATCH] Initial commit, zsh config --- .gitmodules | 9 ++++++ setup | 22 ++++++++++++++ zsh/.zsh/aliases.zsh | 15 ++++++++++ zsh/.zsh/bun.zsh | 6 ++++ zsh/.zsh/deferred-eval-source.zsh | 14 +++++++++ zsh/.zsh/git_branch.zsh | 33 +++++++++++++++++++++ zsh/.zsh/prompt.zsh | 15 ++++++++++ zsh/.zsh/runfile.zsh | 23 +++++++++++++++ zsh/.zsh/vim-mode.zsh | 39 +++++++++++++++++++++++++ zsh/.zsh/zsh-autosuggestions | 1 + zsh/.zsh/zsh-history-substring-search | 1 + zsh/.zsh/zsh-syntax-highlighting | 1 + zsh/.zshenv | 3 ++ zsh/.zshrc | 41 +++++++++++++++++++++++++++ 14 files changed, 223 insertions(+) create mode 100644 .gitmodules create mode 100755 setup create mode 100644 zsh/.zsh/aliases.zsh create mode 100644 zsh/.zsh/bun.zsh create mode 100644 zsh/.zsh/deferred-eval-source.zsh create mode 100644 zsh/.zsh/git_branch.zsh create mode 100644 zsh/.zsh/prompt.zsh create mode 100644 zsh/.zsh/runfile.zsh create mode 100644 zsh/.zsh/vim-mode.zsh create mode 160000 zsh/.zsh/zsh-autosuggestions create mode 160000 zsh/.zsh/zsh-history-substring-search create mode 160000 zsh/.zsh/zsh-syntax-highlighting create mode 100644 zsh/.zshenv create mode 100644 zsh/.zshrc diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..675bfc7 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,9 @@ +[submodule "zsh/.zsh/zsh-history-substring-search"] + path = zsh/.zsh/zsh-history-substring-search + url = git@rustylab.net:mirrors/zsh-history-substring-search.git +[submodule "zsh/.zsh/zsh-autosuggestions"] + path = zsh/.zsh/zsh-autosuggestions + url = git@rustylab.net:mirrors/zsh-autosuggestions.git +[submodule "zsh/.zsh/zsh-syntax-highlighting"] + path = zsh/.zsh/zsh-syntax-highlighting + url = git@rustylab.net:mirrors/zsh-syntax-highlighting.git diff --git a/setup b/setup new file mode 100755 index 0000000..14ee23e --- /dev/null +++ b/setup @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +install_all () { + ls | grep -v setup | while read app; do + echo install_config $app + done +} + +install_config () { + [ -z "${1+x}" ] && echo "Usage: $0 install " && return 1 + stow $1 +} + +uninstall_config () { + stow -D $1 +} + +case $1 in + install) install_config $2;; + install-all) install_all;; + uninstall) uninstall_config $2;; +esac diff --git a/zsh/.zsh/aliases.zsh b/zsh/.zsh/aliases.zsh new file mode 100644 index 0000000..ffa21d3 --- /dev/null +++ b/zsh/.zsh/aliases.zsh @@ -0,0 +1,15 @@ +alias ls="ls --color" +alias swayimg="swayimg -w image -c info.show=no -c general.position=20\,20" +alias podman="sudo podman" + +findfile () { + local search + if [ "$1" = "--update" ]; then + sudo updatedb + search=$2 + else + search=$1 + fi + + locate "$PWD*$search*" +} diff --git a/zsh/.zsh/bun.zsh b/zsh/.zsh/bun.zsh new file mode 100644 index 0000000..3bc68d4 --- /dev/null +++ b/zsh/.zsh/bun.zsh @@ -0,0 +1,6 @@ +# bun completions +[ -s "$HOME/.bun/_bun" ] && source "$HOME/.bun/_bun" + +# bun +export BUN_INSTALL="$HOME/.bun" +export PATH="$BUN_INSTALL/bin:$PATH" diff --git a/zsh/.zsh/deferred-eval-source.zsh b/zsh/.zsh/deferred-eval-source.zsh new file mode 100644 index 0000000..e3355a8 --- /dev/null +++ b/zsh/.zsh/deferred-eval-source.zsh @@ -0,0 +1,14 @@ +# This loads zoxide and fnm in background +# Generates script and sources it when it's ready. +# Makes startup times faster + +trap 'source /tmp/zsh_deferred_source.$$ && rm /tmp/zsh_deferred_source.$$' USR2 +zi () {} +deferred_source_prepare () { + zoxide init zsh > /tmp/zsh_deferred_source.$$ + fnm env --use-on-cd --shell zsh >> /tmp/zsh_deferred_source.$$ + sleep 0.2 + kill -s USR2 $$ > /dev/null 2> /dev/null +} + +deferred_source_prepare &! diff --git a/zsh/.zsh/git_branch.zsh b/zsh/.zsh/git_branch.zsh new file mode 100644 index 0000000..6b5fc32 --- /dev/null +++ b/zsh/.zsh/git_branch.zsh @@ -0,0 +1,33 @@ +GIT_BRANCH= +GIT_BG_PID=0 + +trap 'git_callback' USR1 + +get_git_branch () { + git rev-parse --abbrev-ref HEAD 2> /dev/null > "/tmp/zsh_git_branch.$$" + # git rev-parse --verify REBASE_HEAD 2> /dev/null && echo 1 > "/tmp/zsh_git_rebase.$$" + kill -s USR1 $$ > /dev/null 2> /dev/null +} + +git_branch_bg () { + if [[ $GIT_BG_PID != 0 ]]; then + kill -s HUP $GIT_BG_PID > /dev/null 2>&1 + fi + if [ -f "/tmp/zsh_git_branch.$$" ]; then + rm "/tmp/zsh_git_branch.$$" + fi + get_git_branch &! + GIT_BG_PID=$! +} + +git_callback () { + GIT_BRANCH=$(cat "/tmp/zsh_git_branch.$$") + rm "/tmp/zsh_git_branch.$$" + update_right_prompt + GIT_BG_PID=0 +} + +autoload -U add-zsh-hook +# add-zsh-hook chpwd git_branch_bg +add-zsh-hook precmd git_branch_bg +git_branch_bg diff --git a/zsh/.zsh/prompt.zsh b/zsh/.zsh/prompt.zsh new file mode 100644 index 0000000..51e4ff8 --- /dev/null +++ b/zsh/.zsh/prompt.zsh @@ -0,0 +1,15 @@ +PS1='%F{blue}%~ %(?.%F{green}.%F{red})%#%f ' + +source "$HOME/.zsh/vim-mode.zsh" + +update_right_prompt () { + RPS1="%F{blue}-- $VI_KEYMAP --%f" + if [ ! -z "$GIT_BRANCH" ]; then + RPS1="%F{red} $GIT_BRANCH%f $RPS1" + fi + zle reset-prompt 2> /dev/null +} + +update_right_prompt + +source "$HOME/.zsh/git_branch.zsh" diff --git a/zsh/.zsh/runfile.zsh b/zsh/.zsh/runfile.zsh new file mode 100644 index 0000000..95a7401 --- /dev/null +++ b/zsh/.zsh/runfile.zsh @@ -0,0 +1,23 @@ +# It's like makefile/justfile but using a simple script! +# usage: run +# runfile can be placed in CWD or parents + +run () { + local runfile + runfile="$(_run "$PWD")" && "$runfile" "$@" +} + +_run () { + RUNFILE_DIR=$1 + if [ ! -e "$RUNFILE_DIR/runfile" ] && [ ! -e "$RUNFILE_DIR/.runfile" ]; then + if [ "$RUNFILE_DIR" = "/" ]; then + echo "Runfile not found" 1>&2; return 1 + else + _run "$(dirname $RUNFILE_DIR)" + fi + elif [ -e "$RUNFILE_DIR/runfile" ]; then + echo "$RUNFILE_DIR/runfile" + elif [ -e "$RUNFILE_DIR/.runfile" ]; then + echo "$RUNFILE_DIR/.runfile" + fi +} diff --git a/zsh/.zsh/vim-mode.zsh b/zsh/.zsh/vim-mode.zsh new file mode 100644 index 0000000..6005755 --- /dev/null +++ b/zsh/.zsh/vim-mode.zsh @@ -0,0 +1,39 @@ +bindkey -v +export KEYTIMEOUT=1 + +autoload -U add-zle-hook-widget + +line_pre_redraw () { + local previous_vi_keymap="${VI_KEYMAP}" + + case "${KEYMAP}" in + vicmd) + case "${REGION_ACTIVE}" in + 1) + VI_KEYMAP="VISUAL" + ;; + 2) + VI_KEYMAP="V-LINE" + ;; + *) + VI_KEYMAP="NORMAL" + ;; + esac + ;; + viins|main) + if [[ "${ZLE_STATE}" == *overwrite* ]]; then + VI_KEYMAP="REPLACE" + else + VI_KEYMAP="INSERT" + fi + ;; + esac + + if [[ "${VI_KEYMAP}" != "${previous_vi_keymap}" ]]; then + update_right_prompt + fi +} + +VI_KEYMAP="INSERT" + +add-zle-hook-widget zle-line-pre-redraw line_pre_redraw diff --git a/zsh/.zsh/zsh-autosuggestions b/zsh/.zsh/zsh-autosuggestions new file mode 160000 index 0000000..c3d4e57 --- /dev/null +++ b/zsh/.zsh/zsh-autosuggestions @@ -0,0 +1 @@ +Subproject commit c3d4e576c9c86eac62884bd47c01f6faed043fc5 diff --git a/zsh/.zsh/zsh-history-substring-search b/zsh/.zsh/zsh-history-substring-search new file mode 160000 index 0000000..87ce96b --- /dev/null +++ b/zsh/.zsh/zsh-history-substring-search @@ -0,0 +1 @@ +Subproject commit 87ce96b1862928d84b1afe7c173316614b30e301 diff --git a/zsh/.zsh/zsh-syntax-highlighting b/zsh/.zsh/zsh-syntax-highlighting new file mode 160000 index 0000000..e0165ea --- /dev/null +++ b/zsh/.zsh/zsh-syntax-highlighting @@ -0,0 +1 @@ +Subproject commit e0165eaa730dd0fa321a6a6de74f092fe87630b0 diff --git a/zsh/.zshenv b/zsh/.zshenv new file mode 100644 index 0000000..9a28387 --- /dev/null +++ b/zsh/.zshenv @@ -0,0 +1,3 @@ +export GTK_THEME=Adwaita:dark +export PATH="$HOME/.local/bin":$PATH +export EDITOR=nvim diff --git a/zsh/.zshrc b/zsh/.zshrc new file mode 100644 index 0000000..c694754 --- /dev/null +++ b/zsh/.zshrc @@ -0,0 +1,41 @@ +source "$HOME/.zsh/deferred-eval-source.zsh" # Useful when you use fnm (https://github.com/Schniz/fnm) and zoxide (https://github.com/ajeetdsouza/zoxide), remove otherwise + +HISTFILE=~/.zsh_history # File where history is stored +HISTSIZE=1000000 +SAVEHIST=1000000 + +setopt appendhistory # Enable history +setopt extendedhistory # Timestamps in history +setopt incappendhistory # Write history immediately +setopt histfindnodups # Skip duplicates in history + +zstyle ':completion:*' menu select # Highlight completion option + +bindkey -v '^?' backward-delete-char # BACKSPACE key enable +bindkey -v "^[[H" beginning-of-line # HOME key enable +bindkey -v "^[[F" end-of-line # END key enable +bindkey -v "^[[3~" delete-char # DELETE key enable + +ZSH_AUTOSUGGEST_STRATEGY=(history completion) +HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND="fg=blue,bold" + +zstyle ":completion:*:commands" rehash 1 # Better completion for newly installed commands (ignores cache) + +bindkey '^[[A' history-substring-search-up # Arrow up for history search up +bindkey '^[[B' history-substring-search-down # Arrow down for history search down +bindkey -M vicmd 'k' history-substring-search-up # K for history search up in vim normal mode +bindkey -M vicmd 'j' history-substring-search-down # J for history search down in vim normal mode +bindkey '^[[Z' reverse-menu-complete # SHIFT+TAB for previous completion + +autoload -Uz compinit # Enable completions +compinit + +source "$HOME/.zsh/aliases.zsh" +source "$HOME/.zsh/prompt.zsh" +source "$HOME/.zsh/runfile.zsh" +source "$HOME/.zsh/bun.zsh" # Useful when you use bun (https://bun.sh/), remove otherwise + +# External plugins +source "$HOME/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh" +source "$HOME/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" +source "$HOME/.zsh/zsh-history-substring-search/zsh-history-substring-search.zsh"