]> git.madduck.net Git - etc/zsh.git/blob - .zsh/zshrc/20-tempfile+dir_functions

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

echo vit file location
[etc/zsh.git] / .zsh / zshrc / 20-tempfile+dir_functions
1 #
2 # Helper functions for temporary files, directories, and pastebining
3 #
4 # Copyright © 1994–2017 martin f. krafft <madduck@madduck.net>
5 # Released under the terms of the Artistic Licence 2.0
6 #
7 # Source repository: http://git.madduck.net/v/etc/zsh.git
8 #
9
10 cdt() {
11   emulate -L zsh
12   REPLY=$(mktemp -td ${1:-cdt}.XXXXXX)
13   builtin cd $REPLY
14   rm -f ${TMPDIR:-/tmp}/cdt.latest
15   ln -s $REPLY ${TMPDIR:-/tmp}/cdt.latest
16 }
17
18 _cdt() {
19   cdt "$@"
20   zle reset-prompt
21   zle -M "$REPLY"
22 }
23 zle -N _cdt
24 bindkey '\ed' _cdt
25
26 vit() {
27   emulate -L zsh
28   local prefix i
29   for i in "$@"; do
30     case "$i" in
31       -) local stdin=1; shift;;
32       *) if [ -z "${prefix:-}" ]; then
33            prefix="$i"; shift
34          else
35            zwarn "prefix $prefix already specified, skipping: $i"
36          fi
37          ;;
38     esac
39   done
40   REPLY=$(mktemp -t ${prefix:-vit}-XXXXXX.txt)
41   [ -n "$stdin" ] && cat >| $REPLY
42   sensible-editor +start $REPLY </dev/tty >/dev/tty
43   ln -sf $REPLY ${TMPDIR:-/tmp}/vit.latest
44   echo "$REPLY"
45 }
46 _vit() {
47   vit "$@" >/dev/null
48   zle reset-prompt
49   zle -M "$REPLY"
50 }
51 zle -N _vit
52 bindkey '\ef' _vit
53
54 pastebin() {
55   local target="${(%):-"%D{%F-%H%M%S}"}-${1##*/}"
56   pub "${1}==${target}" 2>&1
57 }
58
59 _pastebinit() {
60   emulate -L zsh
61   if [[ -f "$REPLY" ]]; then
62     read -q "yesno?\rShould I paste the file $REPLY? [yN] "
63     zle reset-prompt
64   else
65     vit paste
66     yesno=y
67   fi
68   if [[ ${yesno:-n} == y ]]; then
69     zle -cR "pasting $REPLY …"
70     REPLY=$(pastebin "${REPLY}")
71     zle -M "$REPLY"
72   fi
73 }
74 zle -N _pastebinit
75 bindkey '\ep' _pastebinit
76
77 _copy_reply() {
78   if [[ -n "$REPLY" ]]; then
79     if (( $+commands[xclip] )); then
80       echo -En "$REPLY" | xclip -in
81       zle -M "Copied to primary clipboard: $REPLY"
82     else
83       zle -M "Cannot copy to clipboard, xclip command not found"
84     fi
85   else
86     zle -M "Nothing to copy, \$REPLY is empty"
87   fi
88 }
89 zle -N _copy_reply
90 bindkey '\ec' _copy_reply
91
92 TS() {
93   local topic ret quiet
94   typeset -g REPLY
95   topic="${(j:_:)@}"
96   REPLY="${TMPDIR:-/tmp}/script.${topic:-$LOGNAME}.$$.${(%):-"%D{%Y%m%d.%H%M}"}"
97   PS1="
98 %# " RPS1= script -qe -c "zsh -f" -f "$REPLY"
99   ret=$?
100   zinfo "typescript is in $REPLY ."
101   return $ret
102 }
103
104 # vim:ft=zsh