From 6139834a4aad1497640a34cddb6bf83028161bb5 Mon Sep 17 00:00:00 2001 From: Timothy DeHerrera Date: Sun, 15 Dec 2019 20:54:11 -0700 Subject: [PATCH] develop#zsh: factor out functions into files Upgrade the `rg` function to only pipe to `PAGER` when output is a terminal. --- profiles/develop/zsh/default.nix | 31 ++++++++++++-- profiles/develop/zsh/functions/man | 8 ++++ profiles/develop/zsh/functions/mcd | 2 + profiles/develop/zsh/functions/rg | 6 +++ profiles/develop/zsh/zshrc | 66 ++++++------------------------ users/nrd.nix | 1 + 6 files changed, 58 insertions(+), 56 deletions(-) create mode 100644 profiles/develop/zsh/functions/man create mode 100644 profiles/develop/zsh/functions/mcd create mode 100644 profiles/develop/zsh/functions/rg diff --git a/profiles/develop/zsh/default.nix b/profiles/develop/zsh/default.nix index 847bd2bf..de60f23f 100644 --- a/profiles/develop/zsh/default.nix +++ b/profiles/develop/zsh/default.nix @@ -73,7 +73,7 @@ in interactiveShellInit = let zshrc = fileContents ./zshrc; - paths = with pkgs; [ + sources = with pkgs; [ "${skim}/share/skim/completion.zsh" "${oh-my-zsh}/share/oh-my-zsh/plugins/sudo/sudo.plugin.zsh" "${oh-my-zsh}/share/oh-my-zsh/plugins/extract/extract.plugin.zsh" @@ -82,8 +82,28 @@ in ]; source = map - (source: "source " + source) - paths; + (source: "source ${source}") + sources; + + functions = pkgs.stdenv.mkDerivation { + name = "zsh-functions"; + src = ./functions; + + ripgrep = "${pkgs.ripgrep}"; + man = "${pkgs.man}"; + + installPhase = let + basename = "\${file##*/}"; + in + '' + mkdir $out + + for file in $src/*; do + substituteAll $file $out/${basename} + chmod 755 $out/${basename} + done + ''; + }; plugins = concatStringsSep "\n" ( @@ -98,8 +118,13 @@ in '' ${plugins} + fpath+=( ${functions} ) + autoload -Uz ${functions}/*(:t) + ${zshrc} + eval "$(${pkgs.direnv}/bin/direnv hook zsh)" + eval $(${pkgs.gitAndTools.hub}/bin/hub alias -s) source ${pkgs.skim}/share/skim/key-bindings.zsh ''; }; diff --git a/profiles/develop/zsh/functions/man b/profiles/develop/zsh/functions/man new file mode 100644 index 00000000..35dc049b --- /dev/null +++ b/profiles/develop/zsh/functions/man @@ -0,0 +1,8 @@ +# colorful man pages +LESS_TERMCAP_md=$'\e[01;31m' \ +LESS_TERMCAP_me=$'\e[0m' \ +LESS_TERMCAP_se=$'\e[0m' \ +LESS_TERMCAP_so=$'\e[01;44;33m' \ +LESS_TERMCAP_ue=$'\e[0m' \ +LESS_TERMCAP_us=$'\e[01;32m' \ +@man@/bin/man "$@" diff --git a/profiles/develop/zsh/functions/mcd b/profiles/develop/zsh/functions/mcd new file mode 100644 index 00000000..bd28cf3d --- /dev/null +++ b/profiles/develop/zsh/functions/mcd @@ -0,0 +1,2 @@ +# mkdir & cd to it +mkdir -p "$1" && cd "$1" diff --git a/profiles/develop/zsh/functions/rg b/profiles/develop/zsh/functions/rg new file mode 100644 index 00000000..8ba77659 --- /dev/null +++ b/profiles/develop/zsh/functions/rg @@ -0,0 +1,6 @@ +# pipe rg into less with colors +if [[ -t 1 && -n $PAGER ]]; then + @ripgrep@/bin/rg -p $@ | $PAGER +else + @ripgrep@/bin/rg $@ +fi diff --git a/profiles/develop/zsh/zshrc b/profiles/develop/zsh/zshrc index c5dd877b..f223084a 100644 --- a/profiles/develop/zsh/zshrc +++ b/profiles/develop/zsh/zshrc @@ -1,3 +1,4 @@ +# tilde directories hash -d \ nixos=/etc/nixos \ dl=~/Downloads \ @@ -7,9 +8,6 @@ hash -d \ github=~git/github.com \ gitlab=~git/gitlab.com -# hub setup -eval $(hub alias -s) - # Zsh options setopt extendedglob setopt incappendhistory @@ -31,50 +29,32 @@ SAVEHIST=10000 HISTFILE=$HOME/.history DIRSTACKSIZE=5 -# user defined functions -if [[ -d $HOME/.zfunctions ]]; then - typeset -U fpath - fpath=($HOME/.zfunctions $fpath) - - for file in $HOME/.zfunctions/*; do - autoload -Uz $file - done -fi - -# source local config not synced to git repo -if [[ -f $HOME/.zshlocal ]]; then -. $HOME/.zshlocal -fi - -# create local zshrc if it does not exist -[[ -f $HOME/.zshrc ]] \ - || printf "#" > $HOME/.zshrc - # history substring search keybinds bindkey '^[OA' history-substring-search-up bindkey '^[OB' history-substring-search-down bindkey -M vicmd 'k' history-substring-search-up bindkey -M vicmd 'j' history-substring-search-down -zle -N zle-line-init - # if in tmux launch skim in tmux pane [[ ${+TMUX} == 1 ]] \ && SKIM_TMUX=1 #load compinit autoload -Uz compinit -typeset -i updated_at=$(date +'%j' -r ~/.zcompdump || -stat -f '%Sm' -t '%j' ~/.zcompdump) + +typeset -i updated_at=$(date +'%j' -r ~/.zcompdump \ + || stat -f '%Sm' -t '%j' ~/.zcompdump) + +# save time if completion cache has been update recently if [ $(date +'%j') != $updated_at ]; then -compinit + compinit else -compinit -C + compinit -C fi # Case insens only when no case match; after all completions loaded zstyle ':completion:*' matcher-list \ -"" 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*' + "" 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*' # Auto rehash for new binaries zstyle ':completion:*' rehash true @@ -83,29 +63,9 @@ zstyle ':completion:*' rehash true bindkey "jj" vi-cmd-mode bindkey -v '^?' backward-delete-char +# remove duplicates from paths +typeset -U path +typeset -U fpath + # keep shell state frozen ttyctl -f - -# colorful man pages -man () { - LESS_TERMCAP_md=$'\e[01;31m' \ - LESS_TERMCAP_me=$'\e[0m' \ - LESS_TERMCAP_se=$'\e[0m' \ - LESS_TERMCAP_so=$'\e[01;44;33m' \ - LESS_TERMCAP_ue=$'\e[0m' \ - LESS_TERMCAP_us=$'\e[01;32m' \ - command man "$@" -} - -# pipe rg into less with colors -rgl () { - rg --color=always $@ | less -} - -# mkdir & cd to it -function mcd() { - mkdir -p "$1" && cd "$1"; -} - -# direnv setup -eval "$(direnv hook zsh)" diff --git a/users/nrd.nix b/users/nrd.nix index e9a9819b..64c6e589 100644 --- a/users/nrd.nix +++ b/users/nrd.nix @@ -24,6 +24,7 @@ in file = { ".ec2-keys".source = ../secrets/ec2; ".cargo/credentials".source = ../secrets/cargo; + ".zshrc".text = "#"; }; };