I learn zsh
authorSergey Matveev <stargrave@stargrave.org>
Wed, 10 Feb 2021 21:57:36 +0000 (00:57 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Wed, 10 Feb 2021 21:59:04 +0000 (00:59 +0300)
bin/bin/pe
bin/bin/zsnap.zsh
vim/.vim/plugin/zshfe.zsh
zsh/.zshenv
zsh/.zshrc

index 48b7ededf422428d9ed72ed41d9f7fe4a8c4a452..bcd149b25d6447add59254378258cabf9282243e 100755 (executable)
@@ -6,4 +6,4 @@ path-extractor | while read fn ; do
     seen+=($fn 1)
     print $fn
 done | fzf -m | tr "\n" " " | read data
-tmux set-buffer "${data:- }"
+tmux set-buffer ${data:- }
index a14979b750616313e817e7b73f5e939bd655b92b..a75650c2a2c3ac09e37b6b216aec4125879e3766 100755 (executable)
@@ -1,65 +1,73 @@
-#!/bin/zsh -e
+#!/usr/bin/env zsh
+
+set -e
 
 usage() {
-cat <<EOF
+    cat <<EOF
 Usage: $0 DATASET@SNAPPREFIX snap-initial
        $0 DATASET@SNAPPREFIX snap
        $0 DATASET@SNAPPREFIX sync-initial DST
        $0 DATASET@SNAPPREFIX sync DST
        $0 DATASET@SNAPPREFIX clean COUNT
 EOF
-exit 1
+    exit 1
 }
 
 enccmd="gpg --compress-level 0 --encrypt --recipient offline --cipher-algo AES-128"
 
-[ $# -ge 2 ] || usage
+[[ $# -ge 2 ]] || usage
 
-what="$1"
-action="$2"
+what=$1
+action=$2
 
 now=$(date "+%Y%m%d%H%M")
-snaps=$(zfs list -t snap -d 1 -o name -H | grep $what | sort -nr)
-latest=$(echo $snaps | sed -n 1p)
+depth=${#${(s./.)what}}
+snaps=$(zfs list -t snap -d $depth -o name -H | grep $what | sort -nr)
+snaps=(${(f)snaps})
+latest=${snaps[1]}
+latest_filename=${latest:gs#/#%}
 
-case "$action" in
+case $action in
     snap-initial)
         zfs snap -r $what-$now
-        echo $what-$now
+        print $what-$now
         ;;
     snap)
-        [ -n "$latest" ]
-        [ "$what-$now" != "$latest" ]
+        [[ -n $latest ]]
+        [[ "$what-$now" != $latest ]]
         zfs snap -r $what-$now
-        echo $what-$now
+        print $what-$now
         ;;
     sync-initial)
-        dst="$3"
-        [ -d "$dst" ] || usage
-        set PIPE_FAIL
-        zfs send -Rv $latest | zstd | ${=enccmd} > $dst/$latest.zfs.zst.gpg
+        dst=$3
+        [[ -d $dst ]] || usage
+        setopt PIPE_FAIL
+        zfs send -Rv $latest | zstd | ${=enccmd} > \
+            $dst/$latest_filename.zfs.zst.gpg
         sync
-        touch $dst/$latest.from
+        touch $dst/$latest_filename.from
         ;;
     sync)
-        dst="$3"
-        [ -d "$dst" ] || usage
-        latest_dst=$(find $dst -type f -name "*.from" | sort -rn | sed -n 1p)
-        [ -n "$latest_dst" ]
-        latest_dst=$(basename ${latest_dst%.from})
-        [ "$latest_dst" != "$latest" ]
-        set PIPE_FAIL
-        zfs send -Rv -i $latest_dst $latest | zstd | ${=enccmd} > $dst/$latest.zfs.zst.gpg
+        dst=$3
+        [[ -d $dst ]] || usage
+        latest_dst=($dst/*(.Onn[1]))
+        [[ $latest_dst ]]
+        latest_dst=${latest_dst[1]}
+        latest_dst=${${latest_dst##*/}%.from}
+        [[ $latest_dst != $latest_filename ]]
+        setopt PIPE_FAIL
+        zfs send -Rv -i ${latest_dst:gs#%#/} $latest | zstd | ${=enccmd} > \
+            $dst/$latest_filename.zfs.zst.gpg
         sync
-        echo "$latest_dst" > $dst/$latest.from
+        print $latest_dst > $dst/$latest_filename.from
         ;;
     clean)
-        count="$3"
-        [ -n "$count" ] || usage
+        count=$3
+        [[ -n $count ]] || usage
         count=$(( $count + 1 ))
-        for snap in $(echo $snaps) ; do
+        for snap in $snaps ; do
             count=$(( $count - 1 ))
-            [ $count -le 0 ] || continue
+            [[ $count -le 0 ]] || continue
             zfs destroy -Rv $snap
         done
         ;;
index 0dfcd68e96761bff887a860988e5ad7b1338322a..b0d8cd93a5efecbe3c8da39a8cc390b467b02b27 100755 (executable)
@@ -5,4 +5,4 @@ zmodload zsh/zpty
 zpty zshfe zsh
 zpty -w zshfe "print ZSHFE $*"$'\t EFHSZ\nexit\n'
 zpty -r zshfe zshfe "*exit"
-echo "$zshfe" | sed -n "s/^ZSHFE \(.*\) EFHSZ/\1/p"
+echo $zshfe | sed -n "s/^ZSHFE \(.*\) EFHSZ/\1/p"
index 9bb060525f80c7d90704d48d332be877c639663e..ff28109e97f5fc66eb3ebda4c6e838bf923eb289 100644 (file)
@@ -8,7 +8,7 @@ SAVEHIST=10240
 
 # Terminal {{{
 export COLORFGBG="default;default;0"
-export COLORTERM="xterm-256color"
+export COLORTERM=xterm-256color
 [ "$TERM" = "screen-256color" ] || TERM=xterm-256color
 export TERM
 # }}}
@@ -21,7 +21,7 @@ export LESS=RXij3
 
 # ls options {{{
 export CLICOLOR=1
-export LSCOLORS="BxGxcxdxCxegDxabagacad"
+export LSCOLORS=BxGxcxdxCxegDxabagacad
 # }}}
 
 # Lynx options {{{
@@ -54,32 +54,38 @@ export XDG_RUNTIME_DIR=/tmp/stargrave-xdg
 # }}}
 
 # Paths {{{
-export CFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"
-export PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
-export PATH=$HOME/local/bin:$HOME/bin:$PATH
-export LD_LIBRARY_PATH=$HOME/local/lib:$LD_LIBRARY_PATH
-
-export MANPATH=/usr/local/share/man:/usr/local/man:/usr/share/man
-export MANPATH=/usr/share/openssl/man:$MANPATH
-export MANPATH=/usr/local/lib/perl5/site_perl/man:$MANPATH
-export MANPATH=/usr/local/lib/perl5/5.26/perl/man:$MANPATH
-export MANPATH=$HOME/local/share/man:$MANPATH
-
-export INFOPATH=/usr/local/share/info:/usr/local/info:$HOME/local/share/info
-
-export MANPATH=$HOME/src/suckless/tabbed:$MANPATH
-export PATH=$HOME/src/suckless/tabbed:$PATH
+path=(~/bin ~/local/bin /usr/local/bin /usr/local/sbin /usr/bin /usr/sbin /bin /sbin)
+manpath=(
+    ~/local/share/man
+    /usr/local/lib/perl5/5.26/perl/man
+    /usr/local/lib/perl5/site_perl/man
+    /usr/share/openssl/man
+    /usr/local/share/man
+    /usr/local/man
+    /usr/share/man
+)
+export -TU INFOPATH infopath
+infopath=(~/local/share/info /usr/local/share/info /usr/local/info)
+
+manpath=(~/src/suckless/tabbed $manpath)
+path=(~/src/suckless/tabbed $path)
+
+path=(~/work/goredo $path)
+# }}}
 
-export PATH=$HOME/work/goredo:$PATH
+# C related {{{
+export CFLAGS="-I/usr/local/include"
+export LDFLAGS="-L/usr/local/lib"
+export -TU LD_LIBRARY_PATH ld_library_path
+ld_library_path=(~/local/lib)
 # }}}
 
 # Go related {{{
-export PATH=$HOME/work/go/bin:$PATH
 export GOCACHE=/tmp/go-cache
 export GOPROXY=direct
 export GOSUMDB=off
 export GOPATH=$HOME/work/gopath
-export PATH=$GOPATH/bin:$PATH
+path=($GOPATH/bin ~/work/go/bin $path)
 # }}}
 
 # Various {{{
index 387f852f7dca9ef012c07f50bc9079a5e2054db1..c8591ba5972046a12e51459d5fdbc643e5aeee06 100644 (file)
@@ -41,7 +41,7 @@ bindkey "^[[A" history-beginning-search-backward-end
 bindkey "^[[B" history-beginning-search-forward-end
 
 beginning-history-incremental-pattern-search-backward() {
-    zle history-incremental-pattern-search-backward $(echo $BUFFER | sed "s/ /*/g")
+    zle history-incremental-pattern-search-backward ${BUFFER:gs/ /*/}
 }
 zle -N beginning-history-incremental-pattern-search-backward
 bindkey "^[[1;2A" beginning-history-incremental-pattern-search-backward
@@ -51,14 +51,14 @@ bindkey -M isearch "^[[B" history-incremental-pattern-search-forward
 
 # Prompt {{{
 function zle-line-init zle-keymap-select {
-    mode_vi="${${KEYMAP/vicmd/+}/(main|viins)/-}"
-    [ $timer ] && timer_show=$(( $SECONDS - $timer ))
+    mode_vi=${${KEYMAP/vicmd/+}/(main|viins)/-}
+    [[ $timer ]] && timer_show=$(( $SECONDS - $timer ))
     prompt="%2~|"
     prompt+="%U${timer_show}%u|"
     prompt+="%B%?%b"
     prompt+="${mode_vi}"
     prompt+="%B%F{magenta}%#%f%b "
-    PS1="$prompt"
+    PS1=$prompt
     zle reset-prompt
 }
 zle -N zle-line-init
@@ -92,7 +92,7 @@ f() {
 }
 
 ssht() {
-    ssh -C -t "$1" tmux attach -t0
+    ssh -C -t $1 tmux attach -t0
 }
 alias sshnm="ssh -S none"
 # }}}
@@ -174,11 +174,11 @@ bindkey -s "\eOP" " inc\n" # F1
 zstyle ":completion:*:functions" ignored-patterns "_*"
 zstyle ":completion:*" matcher-list "" 'm:{a-z\-}={A-Z\_}' 'r:|?=** m:{a-z\-}={A-Z\_}'
 _mycomp () {
-    [ "$words[1]" != "man" ] || { _man && return 0 }
-    [ $CURRENT -eq 1 ] && _command_names ||  _files && return 0
+    [[ ${words[1]} != man ]] || { _man && return 0 }
+    [[ $CURRENT -eq 1 ]] && _command_names ||  _files && return 0
     # MAGIC_EQUAL_SUBST {{{
-    [[ "$PREFIX" = *\=* ]] || return 1
-    compstate[parameter]="${PREFIX%%\=*}"
+    [[ $PREFIX = *\=* ]] || return 1
+    compstate[parameter]=${PREFIX%%\=*}
     compset -P 1 "*="
     _value
     # }}}
@@ -215,6 +215,6 @@ export FZF_DEFAULT_OPTS="--color=16 --info=inline"
 cf() {
     local dir=$(find ${1:-.} -path "*/\.git" -prune -o -type d -print |
         fzf --height 40% --reverse --preview="tree -CN {}")
-    [[ -z "$dir" ]] || cd "$dir"
+    [[ -z $dir ]] || cd $dir
 }
 # }}}