-
Notifications
You must be signed in to change notification settings - Fork 1
/
.zsh_functions
241 lines (200 loc) · 5.97 KB
/
.zsh_functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#! /bin/zsh
# constants
export DIARY_DIR=""
export NOTES_DIR=""
export RESEARCH_DIR=""
# Unicode printing
ucat() {
[[ -n "$1" ]] || { echo "Usage: ucat [file]"; return; }
native2ascii -encoding UTF-8 -reverse $1
}
uconv() {
[[ -n "$1" ]] || { echo "Usage: uconv [file]"; return; }
iconv -f euc-kr -t UTF-8 $1 >> tmp.txt
mv tmp.txt $1
echo "Converted $1"
}
uconvs() {
[[ -n "$1" ]] || { echo "Usage: uconvs [some extension]"; return; }
for file in *.$1
do
iconv -f euckr -t utf8 "$file" | sponge "$file"
echo "Converted $file"
done
}
uhead() {
[[ -n "$1" ]] || { echo "Usage: uhead [file]"; return; }
head $1 | native2ascii -encoding UTF-8 -reverse
}
uless() {
[[ -n "$1" ]] || { echo "Usage: umore [file]"; return; }
cat $1 | native2ascii -encoding UTF-8 -reverse | less
}
uforel() {
[[ -n "$1" ]] || { echo "Usage: uforel [file]"; return; }
jq -C '.images[].relations[]' $1 | less -R
}
recent() {
if [ -z "$1" ]
then
$1=".";
fi
fn=`ls -t $1 | head -n1`;
vi "$1$fn";
}
# File conversion
rmd2html() {
[[ -n "$1" ]] || { echo "Usage: rmd2html [file]"; return; }
tmp=`mktemp /tmp/XXXXXX.md`
rscript -e "library(knitr); knit('$1', '$tmp');" > /tmp/null
#TODO: Get optional parameter for css file
html="${1%.*}.html"
echo "create $html"
if [ -f "css/custom-bootstrap.css" ]; then
echo "include custom-bootstrap.css"
pandoc $tmp -o $html -c css/custom-bootstrap.css
else
pandoc $tmp -o $html
fi
echo "cleaning up..."
rm -f $tmp
echo "done"
open $html
}
md2tex() {
[[ -n "$1" ]] || { echo "Usage: md2tex [file]"; return; }
# TODO: replace {% img %} tags with raw latex
pdf=${1/.markdown/.pdf}
pandoc --latex-engine=xelatex --include-in-header=$HOME/.my.tex -t beamer $1 -o $pdf
}
# Hacker's diary
diary() {
[[ -n "$1" ]] || { echo "Usage: diary [title]"; return; }
dir=$DIARY_DIR/$(date "+%Y")
mkdir -p $dir
#TODO: filename - transliterate hangul to roman letters
strip=${1//-/}
merge=${strip// /-}
clean=${merge//[^a-zA-Z0-9\-]/}
lower="$(echo $clean | tr '[:upper:]' '[:lower:]')"
short="${lower:0:30}"
final=${short//-./.}
fname=$dir/$(date "+%Y-%m-%d")-$final.md
echo $fname
if [ ! -e "$fname" ]; then
temp[1]="'''"
temp[2]="layout: diary"
temp[3]="title: $1"
temp[4]="date: $(date '+%Y-%m-%d %H:%M')"
temp[5]="comments: false"
temp[6]="categories: []"
temp[7]="original: null"
temp[8]="'''"
printf "%s\n" "${temp[@]}" > $fname
fi
vi $fname
}
# Compile TeX and open pdf
ctex() {
[[ -n "$1" ]] || { echo "Usage: ctex [somefile].tex"; return; }
pdflatex -shell-escape $1
bibtex ${1/.tex}.aux
pdflatex $1
pdflatex $1
open ${1/.tex}.pdf
}
note() {
[[ -n "$1" ]] || { echo "Usage: note [title]"; return; }
fname="$NOTES_DIR/$(date '+%Y-%m-%d') $1.md"
echo $fname
nvim $fname
}
remark() {
cp -r ~/skel/remark/* .
}
# research notes
re() {
dir=$RESEARCH_DIR
fname=$dir/$(date "+%Y-%m-%d").md
if [ ! -e "$fname" ]
then
echo "---
layout: docs
title: \"$(date '+%Y-%m-%d (%a)')\"
date: $(date '+%Y-%m-%d %H:%M')
comments: false
categories: []
---" > $fname
fi
echo $fname
nvim $fname
}
mmd2html() {
[[ -n "$1" ]] || { echo "Usage: mmd2html [input.md] [output.html]"; return; }
python -c "from lutils import conv; conv.md2html(\"$1\", \"$2\")"
echo "Done."
}
ipyremote() {
pkill -f "ssh -N -n -X -L localhost:18888"
ssh -N -n -X -L localhost:18888:localhost:18888 epark@merci &
}
update_terminal_cwd() {
# Identify the directory using a "file:" scheme URL,
# including the host name to disambiguate local vs.
# remote connections. Percent-escape spaces.
local SEARCH=' '
local REPLACE='%20'
local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
printf '\e]7;%s\a' "$PWD_URL"
}
realpath() {
[[ -n "$1" ]] || { echo "Usage: readlinkf [filename]"; return; }
echo $PWD/$1
}
zs() {
[[ -n "$1" ]] || { echo "Usage: zs [words] [limit (default=10)]"; return; }
zwords="$1"
zlim=${2:-10}
dbfile="$HOME/Library/Containers/com.mozkan.flashpdfsearch/Data/Library/Application Support/com.mozkan.flashpdfsearch/idb.sqlite"
# get wordids
where="ZWORD=\"${zwords/ /\" or ZWORD=\"}\""
zwordids=$(sqlite3 "$dbfile" "select ZWORDID from ZWORDS where $where;")
echo -e "COUNT\tPATH"
# show filenames
where=" ZINDEXITEM.ZWORDID=\"${zwordids/$'\n'/\" or ZINDEXITEM.ZWORDID=\"}\""
sql="select count(*), ZDOCS.ZURL from ZINDEXITEM inner join ZDOCS on ZINDEXITEM.ZDOCNO = ZDOCS.ZDOCNO where $where group by ZINDEXITEM.ZDOCNO order by count(*) desc limit $zlim;"
sqlite3 "$dbfile" "$sql" | awk -F "|" '{printf("%d\t\"%s\"\n"), $1, $2}'
# show counts
where="ZWORDID=\"${zwordids/$'\n'/\" or ZWORDID=\"}\""
nfiles=`sqlite3 "$dbfile" "select count (distinct ZDOCNO) from ZINDEXITEM where $where;"`
echo "Showing $zlim files from $nfiles"
}
notes() {
# Source: https://medium.com/adorableio/simple-note-taking-with-fzf-and-vim-2a647a39cfa
previous_file="$1"
file_to_edit=`select_file $previous_file`
if [ -n "$file_to_edit" ] ; then
cd $STUDY_DIR
file=$(echo "$file_to_edit" | cut -d ':' -f1)
"$EDITOR" $file
notes "$file_to_edit"
fi
}
select_file() {
given_file="$1"
cd $STUDY_DIR
grep -In --line-buffered --color=never -r "" --include "*.md" --include "*.markdown" * |\
fzf --exact --delimiter=":" --preview="cat {1}" --preview-window=right:70%:wrap --height=100% --query="$given_file"
}
rm_mendeley_dups() {
cd /Users/lucypark/Dropbox/Apps/mendeley
ls | grep "(2)" | tr '\n' '\0' | xargs -0 rm
}
# sed -i '' 's/foo/bar/' file
# mkvirtualenv myenv --system-site-packages
randimgcat() {
# requires https://github.com/wookayin/python-imgcat
randimg=$(ls | shuf -n 1)
echo ${randimg}
convert -resize x300 ${randimg} - | imgcat
}