forked from masasam/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.zshrc
596 lines (498 loc) · 17.9 KB
/
.zshrc
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# zshrc
plugins=(… zsh-completions)
autoload -U compinit promptinit
compinit
promptinit
export LANG=ja_JP.UTF-8
autoload -Uz colors
colors
unsetopt promptcr # last line (\n) probrem countermeasure
HISTFILE=~/Dropbox/zsh/.zsh_history
HISTSIZE=1000000
SAVEHIST=1000000
LISTMAX=10000 # ask you if you have over 10000 history
zshaddhistory() {
local line=${1%%$'\n'}
local cmd=${line%% *}
# Only those that satisfy all of the following conditions are added to the history
[[ ${#line} -ge 5
&& ${cmd} != ll
&& ${cmd} != ls
&& ${cmd} != la
&& ${cmd} != cd
&& ${cmd} != man
&& ${cmd} != scp
&& ${cmd} != vim
&& ${cmd} != nvim
&& ${cmd} != less
&& ${cmd} != ping
&& ${cmd} != open
&& ${cmd} != file
&& ${cmd} != which
&& ${cmd} != whois
&& ${cmd} != drill
&& ${cmd} != uname
&& ${cmd} != md5sum
&& ${cmd} != pacman
&& ${cmd} != mytimer
&& ${cmd} != traceroute
&& ${cmd} != speedtest-cli
]]
}
unsetopt extended_history
setopt hist_find_no_dups
setopt hist_reduce_blanks
setopt hist_no_store
setopt append_history # Add history
setopt inc_append_history # Add history incremental
setopt share_history # Share history other terminal
setopt hist_ignore_all_dups # Duplicate command delete it older
setopt hist_ignore_dups # Same command as before don't add to history
setopt hist_ignore_space # Commands beginning with a space delete from history list
unsetopt hist_verify # While calling history and executing stop editing once
setopt hist_reduce_blanks # Extra white space packed and recorded
setopt hist_save_no_dups # When writing to the history file,Ignore the same as the old command.
setopt hist_no_store # Do not register the history command in the history
setopt hist_expand # Automatically expand history on completion
setopt list_packed # Complementary completion list displayed
unsetopt auto_remove_slash
setopt mark_dirs # Matching directory with expanding file name appending / to the end
setopt list_types # Identification of the type of file in complementary candidate list
unsetopt menu_complete # When there are multiple completion candidates, list display
setopt auto_list # When there are multiple completion candidates, list display
setopt auto_param_keys # Automatically complement parentheses' correspondence etc
setopt auto_resume # If you execute the same command name as the suspended process, resume
setopt auto_cd # Move by directory only
setopt no_beep # Do not emit beep with command input error
setopt brace_ccl # Enable brace expansion function
setopt bsd_echo
setopt complete_in_word
setopt equals # = Expand COMMAND to the path name of COMMAND
setopt extended_glob # Enable extended globbing
unsetopt flow_control # (Within shell editor) Disable C-s and C-q
setopt no_flow_control # Do not use flow control by C-s / C-q
setopt hash_cmds # Hash the path when each command is executed
setopt no_hup # Do not kill background jobs when logging out
setopt long_list_jobs # By default, jobs -L is set as the output of the internal command jobs
setopt mail_warning
setopt numeric_glob_sort # Interpret numbers as numbers and sort
setopt path_dirs # Search for subdirectories in PATH when / is included in command name
setopt print_eight_bit # Appropriate display of Japanese in completion candidate list
setopt magic_equal_subst # With command line arguments you can complement even after = = PREFIX = / USR etc
setopt list_packed # When completing completion candidates, display as compacted as possible.
setopt complete_aliases # Include alias as a candidate for completion.
setopt noautoremoveslash # Do not delete the last / when the directory name is an argument
setopt multios # TEE and CAT functions such as multiple redirects and pipes are used as necessary
setopt short_loops # You will be able to use simplified grammar with FOR, REPEAT, SELECT, IF, FUNCTION
setopt auto_param_slash # Automatically add / at the end with directory name completion to prepare for the next completion
setopt auto_menu # Completion key Completion candidate is complemented automatically in order by repeated hitting
# Completion of sudo
zstyle ':completion:*:sudo:*' command-path /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin
zstyle ':completion:*' use-cache true
# Select completion candidate with ← ↓ ↑ →
zstyle ':completion:*:default' menu select=1
# Since there may be uniquely determined files, first complement them
zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z} r:|[-_.]=**'
# Candidate directories on cdpath only when there is no candidate in the current directory
zstyle ':completion:*:cd:*' tag-order local-directories path-directories
# Process name completion of ps command
zstyle ':completion:*:processes' command 'ps x -o pid,s,args'
# Color a completion candidate
eval `dircolors -b`
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
# # prompt
PROMPT="%{%(?.$fg_bold[cyan].$fg_bold[red])%}%m%{$fg_bold[white]%}%%%{$reset_color%} "
PROMPT2="%{$fg[magenta]%}%_%{$reset_color%}%{$fg_bold[white]%}>>%{$reset_color%} "
# Show your current location on the right prompt
RPROMPT="%{$fg_bold[white]%}[%{$reset_color%}%{$fg[cyan]%}%~%{$reset_color%}%{$fg_bold[white]%}]%{$reset_color%}"
# emacs keybind
bindkey -e
# Present candidate for moved directory
setopt auto_pushd
# Do not record duplicate directories with auto_pushd.
setopt pushd_ignore_dups
# It points out the misspelling of the command and presents the expected correct command.
setopt correct
# Permission when creating files
umask 022
# vcs_info
RPROMPT="%{${fg[blue]}%}[%~]%{${reset_color}%}"
autoload -Uz vcs_info
setopt prompt_subst
zstyle ':vcs_info:git:*' check-for-changes true
zstyle ':vcs_info:git:*' stagedstr "%F{yellow}!"
zstyle ':vcs_info:git:*' unstagedstr "%F{red}+"
zstyle ':vcs_info:*' formats "%F{green}%c%u[%b]%f"
zstyle ':vcs_info:*' actionformats '[%b|%a]'
precmd () { vcs_info }
RPROMPT=$RPROMPT'${vcs_info_msg_0_}'
# Tmux, pass the name of the command currently executed to screen
case "${TERM}" in screen-256color)
preexec() {
echo -ne "\ek#${1%% *}\e\\"
}
precmd() {
echo -ne "\ek$(basename $(pwd))\e\\"
vcs_info
};;
tmux-256color)
preexec() {
echo -ne "\ek#${1%% *}\e\\"
}
precmd() {
echo -ne "\ek$(basename $(pwd))\e\\"
vcs_info
};;
xterm)
preexec() {
echo -ne "\ek#${1%% *}\e\\"
}
precmd() {
echo -ne "\ek$(basename $(pwd))\e\\"
vcs_info
};;
esac
# Delete by word with C-w
WORDCHARS='*?_-.[]~=&;!#$%^(){}<>'
# if command not found then find it when using arch linux(sudo pacman -S pkgfile)
if [ -f /usr/share/doc/pkgfile/command-not-found.zsh ]; then
source /usr/share/doc/pkgfile/command-not-found.zsh
fi
# keychain config
/usr/bin/keychain $HOME/.ssh/id_rsa
source $HOME/.keychain/$HOST-sh
# completion mosh
compdef mosh=ssh
# aliases
alias ls='ls -v -F --color=auto'
alias ll='ls -al'
alias la='ls -A'
alias -s {gz,tgz,zip,lzh,bz2,tbz,Z,tar,arj,xz}=aunpack #./hogefuga.tar.gz(pacman -S atool)
alias cp='cp -ip'
alias mv='mv -i'
alias rm='rm -i'
alias du='du -h'
alias df='df -h'
alias free='free -h'
alias e='emacsclient'
alias remacs='emacs -q -l ~/Dropbox/emacs/default.el'
alias open='xdg-open'
alias mysql="mysql --pager='less -S -n -i -F -X'"
alias melpabackup='rm -rf ${HOME}/Dropbox/emacs/elpa/`ls -rt ${HOME}/Dropbox/emacs/elpa | head -n 1`; tar cfz ${HOME}/Dropbox/emacs/elpa/`date '+%Y%m%d%H%M%S'`.tar.gz -C ${HOME}/.emacs.d elpa; cd -'
alias melpacleanup='rm -rf ${HOME}/.emacs.d/elpa'
alias goupdate='cd ${HOME}/src/github.com/masasam/dotfiles; make goinstall; cd -'
alias rustupdate='rustup update'
alias cargoupdate='cargo install-update -a'
alias cargocleanup='rm -rf ${HOME}/.cargo/bin/*; cd ${HOME}/src/github.com/masasam/dotfiles; make rustinstall; cd -'
alias yarnupdate='yarn global upgrade'
alias pipbackup='cd ${HOME}/src/github.com/masasam/dotfiles; make pipbackup; cd -'
alias pipupdate='cd ${HOME}/src/github.com/masasam/dotfiles; make pipupdate; cd -'
alias pipcheck='pip-review --user'
alias archupdate='yaourt -Syua; paccache -ruk0'
alias archbackup='cd ${HOME}/src/github.com/masasam/dotfiles; make backup; cd -'
alias soundrecord='arecord -t wav -f dat -q | lame -b 128 -m s - out.mp3'
alias rust='cargo-script'
alias fontlist='fc-list | cut -d: -f1 | less'
alias fontlistja='fc-list :lang=ja | cut -d: -f1 | less'
alias jupytertheme='jt -t chesterish -T -f roboto -fs 9 -tf merriserif -tfs 11 -nf ptsans -nfs 11 -dfs 8 -ofs 8'
alias gitmaster='git branch --set-upstream-to origin/master master'
alias gitdevelop='git branch --set-upstream-to origin/develop master'
# PATH
export GOPATH=$HOME
export PATH="$PATH:$GOPATH/bin"
export EDITOR='emacsclient'
export XDG_CONFIG_HOME=$HOME/.config
PATH="$(ruby -e 'print Gem.user_dir')/bin:$PATH"
export PATH="$HOME/.cargo/bin:$PATH"
PATH="$HOME/.node_modules/bin:$PATH"
export npm_config_prefix=~/.node_modules
PATH="$HOME/.local/bin:$PATH"
export GTAGSCONF=/usr/share/gtags/gtags.conf
export GTAGSLABEL=pygments
export PAGER=less
export LESS='-g -i -M -R -S -W -z-4 -x4'
export RBENV_ROOT="${HOME}/.rbenv"
if [ -d "${RBENV_ROOT}" ]; then
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"
fi
#eval "$(nodenv init -)"
# cdr
autoload -Uz is-at-least
if [[ -n $(echo ${^fpath}/chpwd_recent_dirs(N)) && -n $(echo ${^fpath}/cdr(N)) ]]; then
autoload -Uz chpwd_recent_dirs cdr add-zsh-hook
add-zsh-hook chpwd chpwd_recent_dirs
zstyle ':completion:*:*:cdr:*:*' menu selection
zstyle ':completion:*' recent-dirs-insert both
zstyle ':chpwd:*' recent-dirs-max 5000
zstyle ':chpwd:*' recent-dirs-default true
zstyle ':chpwd:*' recent-dirs-file "$HOME/Dropbox/zsh/chpwd-recent-dirs"
zstyle ':chpwd:*' recent-dirs-pushd true
fi
# cd after then ls
function chpwd() {
ls -v -F --color=auto
}
# Invoke the ``dired'' of current working directory in Emacs buffer.
function dired() {
emacsclient -e "(dired \"${1:-$PWD}\")" & wmctrl -a emacs
}
## Chdir to the ``default-directory'' of currently opened in Emacs buffer.
function cde () {
EMACS_CWD=`emacsclient -e "
(expand-file-name
(with-current-buffer
(if (featurep 'elscreen)
(let* ((frame-confs (elscreen-get-frame-confs (selected-frame)))
(num (nth 1 (assoc 'screen-history frame-confs)))
(cur-window-conf (cadr (assoc num (assoc 'screen-property frame-confs))))
(marker (nth 2 cur-window-conf)))
(marker-buffer marker))
(nth 1
(assoc 'buffer-list
(nth 1 (nth 1 (current-frame-configuration))))))
default-directory))" | sed 's/^"\(.*\)"$/\1/'`
echo "chdir to $EMACS_CWD"
cd "$EMACS_CWD"
}
function select-history() {
BUFFER=$(history -n -r 1 | fzf-tmux -d --reverse --no-sort +m --query "$LBUFFER" --prompt="History > ")
CURSOR=$#BUFFER
}
zle -N select-history
bindkey '^r' select-history
function rails-routes() {
BUFFER=$(bin/rails routes | fzf-tmux -d --reverse --no-sort +m --query "$LBUFFER" --prompt="rails routes > ")
CURSOR=$#BUFFER
}
function cdr-fzf() {
local selected_dir=$(cdr -l | awk '{ print $2 }' | fzf-tmux -d --reverse --prompt="cd > ")
if [ -n "$selected_dir" ]; then
BUFFER="cd ${selected_dir}"
zle accept-line
fi
zle clear-screen
}
zle -N cdr-fzf
bindkey '^xf' cdr-fzf
bindkey '^x^f' cdr-fzf
function ghq-fzf() {
local selected_dir=$(ghq list | fzf-tmux -d --reverse --query="$LBUFFER" --prompt="ghq list > ")
if [ -n "$selected_dir" ]; then
BUFFER="cd $(ghq root)/${selected_dir}"
zle accept-line
fi
zle reset-prompt
}
zle -N ghq-fzf
bindkey '^x^l' ghq-fzf
bindkey '^xl' ghq-fzf
function ghq-delete () {
ghq list --full-path | fzf-tmux -d --reverse --prompt="github-delete > " | xargs -r rm -r
}
zle -N ghq-delete
bindkey '^xD' ghq-delete
function ghs-import () {
[ "$#" -eq 0 ] && echo "Usage : ghs-import QUERY" && return 1
ghs "$@" | fzf-tmux -d --reverse | awk '{print $1}' | ghq import
}
function github-issue-fzf() {
hub issue | fzf-tmux -d --reverse --prompt="github issue > " | \
sed -e "s/\].*//" | xargs -Inum git checkout -b feature/num
}
zle -N github-issue-fzf
bindkey '^x^i' github-issue-fzf
function git-branch-fzf() {
local selected_branch=$(git for-each-ref --format='%(refname)' --sort=-committerdate refs/heads | \
perl -pne 's{^refs/heads/}{}' | \
fzf-tmux -d --reverse --query "$LBUFFER" --prompt="git branch > ")
if [ -n "$selected_branch" ]; then
BUFFER="git checkout ${selected_branch}"
zle accept-line
fi
zle reset-prompt
}
zle -N git-branch-fzf
bindkey "^x^b" git-branch-fzf
bindkey "^xb" git-branch-fzf
function git-hash-fzf() {
local current_buffer=$BUFFER
local git_hash="$(git log --oneline --branches | fzf-tmux -d --reverse --prompt="git hash > " | awk '{print $1}')"
BUFFER="${current_buffer}${git_hash}"
CURSOR=$#BUFFER
}
zle -N git-hash-fzf
bindkey '^x^h' git-hash-fzf
function git-stash-fzf() {
local current_buffer=$BUFFER
local stash="$(git stash list | fzf-tmux -d --reverse --prompt="git stash > " | awk -F'[ :]' '{print $1}')"
BUFFER="${current_buffer}${stash}"
CURSOR=$#BUFFER
}
zle -N git-stash-fzf
bindkey '^x^s' git-stash-fzf
function ps-fzf() {
local current_buffer=$BUFFER
local process_id="$(ps auxf | fzf-tmux -d --reverse --prompt="ps > " | awk '{print $2}')"
BUFFER="${current_buffer}${process_id}"
CURSOR=$#BUFFER
}
zle -N ps-fzf
bindkey '^xp' ps-fzf
bindkey '^x^p' ps-fzf
function alias-fzf() {
BUFFER=$(alias | fzf-tmux -d --reverse --query "$LBUFFER" --prompt="Alias > " | awk -F"=" '{print $1}')
print -z "$BUFFER"
}
function keybind-fzf() {
zle $(bindkey | fzf-tmux -d --reverse --prompt="Keybind > " | cut -d " " -f 2)
}
zle -N keybind-fzf
bindkey '^xB' keybind-fzf
function ansible-fzf() {
local repositoryname='ansible-setup-server'
ghq root && cat ~/.config/hub | grep user && \
cd $(ghq root)/github.com/$(cat ~/.config/hub | grep user | awk '{print $3}')/${repositoryname}
if [ $? = 0 ]; then
local selected_yml=$(ls | grep .yml$ | fzf-tmux -d --reverse --prompt="Ansible > ")
if [ -n "$selected_yml" ]; then
ansible-playbook --ask-vault-pass ${selected_yml}
if [ $? = 0 ]; then
notify-send -u critical 'Ansible' 'Your playbook execution ended' -i utilities-terminal
else
notify-send -u critical 'Ansible' 'Error has occured' -i dialog-error
fi
fi
cd -
fi
}
zle -N ansible-fzf
bindkey '^x^a' ansible-fzf
function weather-fzf() {
curl wttr.in/$(echo -e "Sapporo\nSendai\nTokyo\nYokohama\nKawasaki\nNagano\nNagoya\nKanazawa\nKyoto\nOsaka-shi\nKobe\nOkayama-Shi\nHiroshima-Shi\nTakamatsu\nMatsuyama\nHakata" | fzf-tmux -d --reverse --prompt="Weather > ") | less -R
}
function gitlog-fzf() {
git log --graph --color=always \
--format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" | \
fzf-tmux -d --prompt="git log > " --ansi --no-sort --reverse --tiebreak=index --bind=ctrl-s:toggle-sort \
--bind "ctrl-m:execute:
(grep -o '[a-f0-9]\{7\}' | head -1 |
xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF'
{}
FZF-EOF"
}
function gitroot() {
cd ./$(git rev-parse --show-cdup)
if [ $# = 1 ]; then
cd $1
fi
}
function github-new() {
if [ $# = 1 ]; then
ghq root && cat ~/.config/hub | grep user \
&& cd $(ghq root)/github.com/$(cat ~/.config/hub \
| grep user | awk '{print $3}') && mkdir $1
if [ $? = 0 ]; then
cd $1
git init .
hub create
touch README.md
git add README.md
git commit -m 'first commit'
git push origin master
fi
else
echo 'usage: github-new name'
fi
}
function ipsort() {
if [ $# = 1 ]; then
cat $1 | sort -n -t'.' -k1,1 -k2,2 -k3,3 -k4,4
else
echo 'usage: ipsort iplist'
fi
}
function webm2gif() {
if [ $# = 1 ]; then
fname_ext=$1
fname="${fname_ext%.*}"
ffmpeg -i $1 -pix_fmt rgb24 $fname.gif
else
echo 'usage: webm2gif file.webm'
fi
}
function md2pdf() {
if [ $# = 1 ]; then
fname_ext=$1
fname="${fname_ext%.*}"
pandoc $1 -o $fname.pdf -V documentclass=ltjarticle --pdf-engine=lualatex
else
echo 'usage: md2pdf file.md'
fi
}
function md2docx() {
if [ $# = 1 ]; then
fname_ext=$1
fname="${fname_ext%.*}"
pandoc $1 -t docx -o $fname.docx --toc --highlight-style=zenburn
else
echo 'usage: md2docx file.md'
fi
}
function remove-exif() {
if [ $# = 1 ]; then
jhead -purejpg $1
else
echo 'usage: remove-exif file.jpg'
fi
}
function mytimer() {
if [ $# = 1 ]; then
_mytimer $1 &
else
echo 'usage: mytimer minute'
fi
}
function _mytimer() {
NUM=`expr 60 \* $1`
sleep $NUM
notify-send -u critical 'Terminal' 'It is time' -i utilities-terminal
}
function github-upstream() {
if [ $# = 1 ]; then
git remote add upstream git://github.com/$1
else
echo 'usage: github-upstream name'
fi
}
function gitlab-upstream() {
if [ $# = 1 ]; then
git remote add upstream git://gitlab.com/$1
else
echo 'usage: gitlab-upstream name'
fi
}
function gitignore() {
curl -L -s https://www.gitignore.io/api/$@
}
function github-stars() {
if [ $# = 2 ]; then
open "https://github.com/search?q=language%3A$1+stars%3A%3E%3D$2&type=Repositories&ref=searchresults"
else
echo 'usage: githab-stars language stars'
fi
}
function terminal-size() {
echo "width"
tput cols
echo "height"
tput lines
}
# zsh-syntax-highlighting(pacman -S zsh-syntax-highlighting)
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# zsh-completions for google-cloud-sdk(yaourt google-cloud-sdk)
#source /opt/google-cloud-sdk/completion.zsh.inc
# zsh-completions for aws
source ~/.local/bin/aws_zsh_completer.sh
# password
source ~/Dropbox/zsh/env.sh