diff --git a/modules/cli/zsh/comp.zsh b/modules/cli/zsh/comp.zsh index f309a04..cf27416 100644 --- a/modules/cli/zsh/comp.zsh +++ b/modules/cli/zsh/comp.zsh @@ -1,8 +1,3 @@ -# used by kubectl plugin & maybe some other oh-my-zsh plugins -export ZSH_CACHE_DIR="$HOME/.cache/zsh"; -mkdir -p "$ZSH_CACHE_DIR/completions" -fpath+="$ZSH_CACHE_DIR/completions" - # default ls colors LS_COLORS=${LS_COLORS:-'di=34:ln=35:so=32:pi=33:ex=31:bd=36;01:cd=33;01:su=31;40;07:sg=36;40;07:tw=32;40;07:ow=33;40;07:'} diff --git a/modules/cli/zsh/default.nix b/modules/cli/zsh/default.nix index ccf65b2..79ac9b7 100644 --- a/modules/cli/zsh/default.nix +++ b/modules/cli/zsh/default.nix @@ -108,29 +108,31 @@ initExtraFirst = #bash '' - # Create a new tmux session (with a random name) and attach. - if [[ -z "$TMUX" ]]; then - exec tmux -u new-session -s "#$(hexdump -n 4 -v -e '/1 "%02X"' /dev/urandom)" - elif [[ $SHLVL -eq 1 ]]; then - session=$(tmux display-message -p "#S") - # kill current session if we are quitting the only pane - function __onExit { - if [[ $(tmux list-panes -s -t $session | wc -l) == 1 ]]; then - tmux kill-session -t $session - fi - } - trap __onExit EXIT - fi + if command -v tmux > /dev/null; then + # Create a new tmux session (with a random name) and attach. + if [[ -z "$TMUX" ]]; then + exec tmux -u new-session -s "#$(hexdump -n 4 -v -e '/1 "%02X"' /dev/urandom)" + elif [[ $SHLVL -eq 1 ]]; then + session=$(tmux display-message -p "#S") + # kill current session if we are quitting the only pane + function __onExit { + if [[ $(tmux list-panes -s -t $session | wc -l) == 1 ]]; then + tmux kill-session -t $session + fi + } + trap __onExit EXIT + fi - # execute arbitrary commands on startup since `zsh -sc` is not a real option :c - if [[ -n CMD ]]; then - # unset the cmd before executing it (for long processes) - cmd=$CMD - unset CMD - if [[ -n $TMUX ]]; then - tmux set-environment -r CMD - fi - eval $cmd + # execute arbitrary commands on startup since `zsh -sc` is not a real option :c + if [[ -n CMD ]]; then + # unset the cmd before executing it (for long processes) + cmd=$CMD + unset CMD + if [[ -n $TMUX ]]; then + tmux set-environment -r CMD + fi + eval $cmd + fi fi ''; initExtraBeforeCompInit = builtins.readFile ./comp.zsh; @@ -151,6 +153,33 @@ ''; initExtra = builtins.readFile ./init.zsh; + initContent = lib.mkMerge [ + (lib.mkOrder 535 (builtins.readFile ./keymap.zsh)) + (lib.mkOrder 570 + #bash + '' + # used by kubectl plugin & maybe some other oh-my-zsh plugins + export ZSH_CACHE_DIR="$HOME/.cache/zsh"; + mkdir -p "$ZSH_CACHE_DIR/completions" + fpath+="$ZSH_CACHE_DIR/completions" + '') + (lib.mkOrder 901 + #bash + '' + unset HISTFILE + setopt rm_star_silent + setopt interactivecomments + setopt autopushd + + # disable space between right prompt and end of line + ZLE_RPROMPT_INDENT=0 + '') + (lib.mkOrder 1400 "${lib.getExe pkgs.nix-your-shell} zsh | source /dev/stdin") + (lib.mkOrder 1410 '' + ${lib.getExe pkgs.starship} init zsh | source /dev/stdin + '') + ]; + envExtra = '' # disable /etc/zshrc & co (nixos one is really bad) setopt no_global_rcs @@ -233,7 +262,6 @@ ncdu ripgrep fzf - nix-your-shell unzip sshfs-fuse zip diff --git a/modules/cli/zsh/init.zsh b/modules/cli/zsh/init.zsh index 7c8c6f2..6a117a7 100644 --- a/modules/cli/zsh/init.zsh +++ b/modules/cli/zsh/init.zsh @@ -4,7 +4,6 @@ pasteinit() { OLD_SELF_INSERT=${${(s.:.)widgets[self-insert]}[2,3]} zle -N self-insert url-quote-magic # I wonder if you'd need `.url-quote-magic`? } - pastefinish() { zle -N self-insert $OLD_SELF_INSERT } @@ -13,56 +12,6 @@ zstyle :bracketed-paste-magic paste-finish pastefinish # https://github.com/zsh-users/zsh-autosuggestions/issues/351 ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(bracketed-paste) -# Make sure the terminal is in application mode, when zle is -# active. Only then are the values from $terminfo valid. -if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then - autoload -Uz add-zle-hook-widget - function zle_application_mode_start { echoti smkx } - function zle_application_mode_stop { echoti rmkx } - add-zle-hook-widget -Uz zle-line-init zle_application_mode_start - add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop -fi - -# setup keys -typeset -g -A key -key[Home]="${terminfo[khome]}" -key[End]="${terminfo[kend]}" -key[Insert]="${terminfo[kich1]}" -key[Backspace]="${terminfo[kbs]}" -key[Delete]="${terminfo[kdch1]}" -key[Up]="${terminfo[kcuu1]}" -key[Down]="${terminfo[kcud1]}" -key[Left]="${terminfo[kcub1]}" -key[Right]="${terminfo[kcuf1]}" -key[PageUp]="${terminfo[kpp]}" -key[PageDown]="${terminfo[knp]}" -key[Shift-Tab]="${terminfo[kcbt]}" -key[Control-Left]="${terminfo[kLFT5]}" -key[Control-Right]="${terminfo[kRIT5]}" - -# start with base emacs commands -bindkey -e - -bindkey "${key[Home]}" beginning-of-line -bindkey "${key[End]}" end-of-line -bindkey "${key[Insert]}" overwrite-mode -bindkey "${key[Backspace]}" backward-delete-char -bindkey "${key[Delete]}" delete-char -bindkey "${key[Left]}" backward-char -bindkey "${key[Right]}" forward-char -bindkey "${key[PageUp]}" beginning-of-buffer-or-history -bindkey "${key[PageDown]}" end-of-buffer-or-history -bindkey "${key[Shift-Tab]}" reverse-menu-complete - -bindkey "${key[Control-Left]}" backward-word -bindkey "${key[Control-Right]}" forward-word - -autoload -Uz up-line-or-beginning-search down-line-or-beginning-search -zle -N up-line-or-beginning-search -zle -N down-line-or-beginning-search -bindkey "${key[Up]}" up-line-or-beginning-search -bindkey "${key[Down]}" down-line-or-beginning-search - # ^Z when a job is suspended runs it in the foreground. foreground() { fg @@ -70,12 +19,6 @@ foreground() { zle -N foreground bindkey ^Z foreground -eval "$(nix-your-shell zsh)" - -setopt rm_star_silent -setopt interactivecomments -setopt autopushd - (whence -w run-help | grep -q alias) && unalias run-help autoload run-help @@ -155,6 +98,3 @@ proxy() { echo "Proxying port $1 to http://proxy.sdg.moe" ssh -NR "5000:localhost:$1" zoriya@ssh.sdg.moe } - -# disable space between right prompt and end of line -ZLE_RPROMPT_INDENT=0 diff --git a/modules/cli/zsh/keymap.zsh b/modules/cli/zsh/keymap.zsh new file mode 100644 index 0000000..da523f8 --- /dev/null +++ b/modules/cli/zsh/keymap.zsh @@ -0,0 +1,50 @@ +# Make sure the terminal is in application mode, when zle is +# active. Only then are the values from $terminfo valid. +if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then + autoload -Uz add-zle-hook-widget + function zle_application_mode_start { echoti smkx; } + function zle_application_mode_stop { echoti rmkx; } + add-zle-hook-widget -Uz zle-line-init zle_application_mode_start + add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop +fi + +# setup keys +typeset -g -A key +key[Home]="${terminfo[khome]}" +key[End]="${terminfo[kend]}" +key[Insert]="${terminfo[kich1]}" +key[Backspace]="${terminfo[kbs]}" +key[Delete]="${terminfo[kdch1]}" +key[Up]="${terminfo[kcuu1]}" +key[Down]="${terminfo[kcud1]}" +key[Left]="${terminfo[kcub1]}" +key[Right]="${terminfo[kcuf1]}" +key[PageUp]="${terminfo[kpp]}" +key[PageDown]="${terminfo[knp]}" +key[Shift-Tab]="${terminfo[kcbt]}" +key[Control-Left]="${terminfo[kLFT5]}" +key[Control-Right]="${terminfo[kRIT5]}" + +# start with base emacs commands +bindkey -e + +bindkey "${key[Home]}" beginning-of-line +bindkey "${key[End]}" end-of-line +bindkey "${key[Insert]}" overwrite-mode +bindkey "${key[Backspace]}" backward-delete-char +bindkey "${key[Delete]}" delete-char +bindkey "${key[Left]}" backward-char +bindkey "${key[Right]}" forward-char +bindkey "${key[PageUp]}" beginning-of-buffer-or-history +bindkey "${key[PageDown]}" end-of-buffer-or-history +bindkey "${key[Shift-Tab]}" reverse-menu-complete + +bindkey "${key[Control-Left]}" backward-word +bindkey "${key[Control-Right]}" forward-word + +autoload -Uz up-line-or-beginning-search down-line-or-beginning-search +zle -N up-line-or-beginning-search +zle -N down-line-or-beginning-search +bindkey "${key[Up]}" up-line-or-beginning-search +bindkey "${key[Down]}" down-line-or-beginning-search + diff --git a/modules/cli/zsh/starship.nix b/modules/cli/zsh/starship.nix index 3648d90..d90a7c2 100644 --- a/modules/cli/zsh/starship.nix +++ b/modules/cli/zsh/starship.nix @@ -1,6 +1,8 @@ {lib, ...}: { programs.starship = { enable = true; + # integrate manually + enableZshIntegration = false; settings = { add_newline = false;