Last active
February 20, 2022 01:44
-
-
Save GINK03/e96cf8fa45a5c97846d5b6cb362d5ce7 to your computer and use it in GitHub Desktop.
zsh-extend-module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################### | |
# tmuxでvim, python等を実行した時、nerdfontで # | |
# 起動アプリケーションとその引数を表示 # | |
############################################### | |
if [[ -n "$TMUX" ]]; then | |
TMUX_CURRENT_WINDOW=`tmux display-message -p '#I'` | |
fi | |
autoload -Uz add-zsh-hook | |
preexec_support_tmux () { | |
if [[ -n "$TMUX" ]]; then | |
if [[ "$1" =~ "^python3 .*\.py" ]]; then | |
# pythonでファイルを実行した時 | |
string=$1 | |
parts=(${(@s: :)string}) | |
title=`echo $parts[2]|cut -b1-6` | |
tmux rename-window "#[fg=#000000,bold,bg=#0fdc24] ${title}#[default]" | |
elif [[ "$1" =~ "^n .*" || "$1" =~ "^vim .*" ]]; then | |
# vimでファイルを開いた時 | |
string=$1 | |
parts=(${(@s: :)string}) | |
title=`echo $parts[2]|cut -b1-6` | |
tmux rename-window " ""${title}" | |
elif [[ "$1" =~ "^less .*" || "$1" =~ "^bat .*" || "$1" =~ "^b .*" ]]; then | |
# lessでファイルを開いた時 | |
string=$1 | |
parts=(${(@s: :)string}) | |
title=`echo $parts[2]|cut -b1-6` | |
tmux rename-window " ""${title}" | |
elif [[ "$1" =~ "^man .*" ]]; then | |
# manでファイルを開いた時 | |
string=$1 | |
parts=(${(@s: :)string}) | |
title=`echo $parts[2]|cut -b1-6` | |
tmux rename-window " ""${title}" | |
elif [[ "$1" =~ "^htop" ]]; then | |
# htopを起動した時 | |
tmux rename-window " htop" | |
else | |
tmux set -w automatic-rename on | |
fi | |
fi | |
} | |
precmd_support_tmux () { | |
return_code=$? | |
if [[ -n "$TMUX" ]]; then | |
tmux set -t $TMUX_CURRENT_WINDOW automatic-rename on | |
if [[ $return_code -eq 1 ]]; then | |
tmux rename-window -t $TMUX_CURRENT_WINDOW "#[fg=white,bold,bg=red] #[default]" | |
fi | |
fi | |
} | |
add-zsh-hook preexec preexec_support_tmux | |
add-zsh-hook precmd precmd_support_tmux | |
################################################ | |
# 24 bit color(true color)のサポート状況を確認 # | |
################################################ | |
if [ -f ~/.var/dots/bin/24-bit-color.sh ]; then | |
bash ~/.var/dots/bin/24-bit-color.sh | |
fi | |
#################### | |
# エイリアスの定義 # | |
#################### | |
function define_alias() { | |
# exaを簡単に | |
alias e=exa | |
# 間違いを正す | |
alias l=ls | |
alias sl=ls # ls -> sl | |
# nvimを簡単に | |
alias n=nvim | |
# tmuxの動作モード指定 | |
alias tmux="tmux -2" | |
# batの行数を標示しない, ページャは常に有効 | |
alias b="bat --paging=always --style=plain" | |
alias bat="bat --paging=always --style=plain" | |
# lessの代わりに積極的に用いる | |
alias les="bat --paging=always --style=plain" | |
# grepを簡単に | |
alias g=grep | |
# findを簡単に | |
alias f='find .' | |
alias fgrep='find . | grep' | |
# pecoを簡単に | |
alias p=peco | |
# cwdから指定のファイルを探すコマンド | |
alias fp="find . -type f | peco" | |
# xargsが長いので簡単に | |
alias x="xargs -o" | |
# use only black with "--skip-string-normalization", "--line-length 300", "--target-version py38" | |
# https://github.com/psf/black | |
alias black='black --skip-string-normalization --line-length 300 --target-version py38' | |
# source ~/.zshrcが長いので sz に省略 | |
alias sz='cd ~/.var/e96cf8fa45a5c97846d5b6cb362d5ce7 && git pull && source ~/.zshrc && echo "reloaded ~/.zshrc!" && cd -' | |
# git関連のエイリアス | |
alias gdiff="git diff" | |
## リモートブランチとの差のdiff | |
alias grdiff="git diff origin/master --" | |
alias gadd="git add" | |
alias gmv="git mv" | |
alias gcommit="git commit" | |
alias gckeckout="git checkout" | |
alias gbranch="git branch" | |
} | |
define_alias | |
################################ | |
# 言語とPATHとクレデンシャル等 # | |
################################ | |
function define_basic(){ | |
# ターミナルの色サポート設定 | |
export TERM=xterm | |
# 言語設定を日本語に変更 | |
export LC_ALL=ja_JP.UTF-8 | |
# PATH | |
export PATH=$PATH:$HOME/.bin | |
export PATH=$PATH:$HOME/.var/dots/bin | |
export PATH=$PATH:$HOME/.cargo/bin | |
export PATH=$PATH:/snap/bin | |
export PATH=$PATH:$HOME/.poetry/bin | |
export PATH=$PATH:~/.local/bin | |
# sudoのpathにuserのpathと/sbinも含める | |
# alias sudo='sudo env "PATH=$PATH:/sbin"' | |
# | |
# GCPのクレデンシャルがあれば設定する | |
if [ -f $HOME/.var/GCP_CRED.json ]; then | |
echo "This machine have gcp credential" | |
export GOOGLE_APPLICATION_CREDENTIALS=$HOME/.var/GCP_CRED.json | |
else | |
echo "This machine DON'T have gcp credential" | |
fi | |
# PYTHON-PATHを追加する | |
export PYTHONPATH=$HOME/.var/dots/lib/psyche:$PYTHONPATH | |
# 補完を無効にする, ターミナルが遅くなるため | |
DISABLE_CORRECTION="true" | |
setopt CSH_NULL_GLOB | |
unsetopt correct_all | |
} | |
define_basic | |
################################### | |
## dot fileのプロジェクトをclone ## | |
################################### | |
function dotfiles(){ | |
if [ ! -d ~/.var ]; then | |
echo "create local .var directry." | |
mkdir ~/.var | |
fi | |
if [ ! -d ~/.var/dots ]; then | |
echo "exists local .var/dots directry." | |
# in ssh protocol, [email protected]:nardtree/gimpei-dot-files.git | |
git clone --recurse-submodules https://[email protected]/nardtree/gimpei-dot-files.git ~/.var/dots | |
cd ~/.var/dots && git pull | |
echo "pulled dos" | |
fi | |
} | |
dotfiles | |
########################################### | |
## dot fileのセットアップスクリプトを実行## | |
########################################### | |
function setup_dotfiles() { | |
dotfiles | |
~/.var/dots/setup-scripts/001-initial-python-modules.py | |
~/.var/dots/setup-scripts/005-create-personal-file-system.py | |
~/.var/dots/setup-scripts/009-install-my-bins.py | |
~/.var/dots/setup-scripts/015-install-python-basic-modules.py | |
} | |
################################# | |
## OSの種類によって変更する挙動 # | |
################################# | |
case `uname` in | |
Darwin) | |
alias google-chrome='/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome' | |
# osx用のnvimのpathを追加 | |
export PATH=$HOME/.bin/nvim-osx64/bin:$PATH | |
# osx用のnpmの実行パスを追加 | |
export PATH=$HOME/.opt/node_modules/bin:$PATH | |
# ~/.bin以下のnvimバイナリを優先する | |
alias n=$HOME/.bin/nvim | |
alias l=ls | |
# 一部のbrewで入れるパッケージが特殊なpathに配置されるので追加する | |
alias mtr=$(brew --prefix)/Cellar/mtr/0.94/sbin/mtr | |
alias iftop=$(brew --prefix)/Cellar/iftop/1.0pre4/sbin/iftop | |
# ユーザ定義のエイリアスを引き継ぐために必要なtrik | |
alias sudo="sudo " | |
# GOPATHを定義 | |
export GOPATH=$HOME/.go | |
export PATH=$HOME/.go/bin:$PATH | |
;; | |
Linux) | |
# ulimit -n(fileデスクリプタの数)を上げる | |
ulimit -n unlimited | |
# nは$HOME/.bin/nvimを直接指定 | |
alias n=$HOME/.bin/nvim | |
# lsの色を変える | |
export LS_COLORS=$LS_COLORS:'di=1;34:' | |
alias ls="ls --color=auto" | |
# lはperspective-lsにマップする | |
alias l="~/.bin/perspective-ls" | |
# lessはパイプで受け取ったときに色情報を保存 | |
alias less="less -r" | |
# grep(egrep)は一致した時に色情報をパイプで流す時にフォワード | |
alias grep="grep --color=always" | |
alias egrep="egrep --color=always" | |
# デフォルトのエディタをnanoからvimに変更 | |
export EDITOR=/usr/bin/vim | |
# tmux内でctrl+a, ctrl+eを有効化する | |
bindkey -e | |
# GOPATHを定義 | |
export GOPATH=$HOME/.go | |
export PATH=$HOME/.go/bin:$PATH | |
;; | |
FreeBSD) | |
;; | |
esac | |
######################## | |
# auto start ssh-agent # | |
######################## | |
function autostart_ssh_agent() { | |
# 1. ssh_auth_sockがあるかチェック | |
# 2. ssh_authの鍵があれば読み込む(無ければ警告) | |
if [ -z "$SSH_AUTH_SOCK" ] ; then | |
if [ ! -f $HOME/.ssh/id_github ]; then | |
echo "This machine don't have ~/.ssh/id_github." | |
else | |
eval `ssh-agent -s` >/dev/null | |
ssh-add -k $HOME/.ssh/id_github | |
echo "ssh-agentを起動し秘密鍵を登録しました" | |
fi | |
else | |
# ssh-add -lが失敗する場合は再度ssh-agentを初期化する | |
ssh-add -l 2>/dev/null | |
if [ $? -ne 0 ]; then | |
eval `ssh-agent -s` >/dev/null | |
ssh-add -k $HOME/.ssh/id_github | |
echo "ssh-agentを起動しなおし秘密鍵を登録しました" | |
fi | |
fi | |
} | |
autostart_ssh_agent | |
######################## | |
## osのバージョン確認 ## | |
######################## | |
function detect_os_version() { | |
# "lsb_release"があるとubuntu | |
# "sw_vers"があるとosx | |
if type "lsb_release" > /dev/null; then | |
export OSVER=`lsb_release -d | python3 -c 'import sys,re; line = sys.stdin.read(); line = re.search("\s([Ubuntu|Debian].{1,})$", line).group(1); print(line, end="")'` | |
elif type "sw_vers" > /dev/null; then | |
export OSVER=`sw_vers -productName`","`sw_vers -productVersion`","`sw_vers -buildVersion` | |
else | |
export OSVER='未特定のOS' | |
fi | |
} | |
detect_os_version | |
#################################################### | |
## 必須ソフトウェアがインストールされているか確認 ## | |
#################################################### | |
function check_bins() { | |
for bin in peco vim nvim tmux | |
do | |
if type $bin > /dev/null; then | |
# echo $bin"はインストール済みです" | |
else | |
echo $bin"はインストールされていません" | |
fi | |
done | |
} | |
check_bins | |
################ | |
## pythonでラップしたツール | |
################ | |
export get_date_info_script=' | |
import os | |
import sys | |
from pathlib import Path | |
import datetime | |
for arg in sys.argv[1:]: | |
if not Path(arg).exists(): | |
print(f"{arg}は存在しません") | |
continue | |
stat = os.stat(arg) | |
st_mtime = datetime.datetime.fromtimestamp(stat.st_mtime).strftime("%Y-%m-%d %H:%M:%S") | |
st_atime = datetime.datetime.fromtimestamp(stat.st_atime).strftime("%Y-%m-%d %H:%M:%S") | |
print(f"{arg}, mdate = {st_mtime}, adate = {st_atime}") | |
' | |
alias get_date_info="python3 -c '$get_date_info_script'" | |
#################################### | |
## config等が存在するか設定の確認 ## | |
#################################### | |
function check_config() { | |
# pecoの設定が無ければ作成する | |
if [ ! -f ~/.config/peco/config.json ]; then | |
mkdir -p ~/.config/peco | |
CONFIG='{ "Keymap": { "Pgup": "peco.ScrollPageUp", "Pgdn": "peco.ScrollPageDown" }, "Style": { "Basic": ["on_default", "default"], "SavedSelection": ["bold", "on_yellow", "white"], "Selected": ["underline", "on_cyan", "black"], "Query": ["yellow", "bold"], "Matched": ["red", "on_blue"] } }' | |
echo $CONFIG | python3 -c 'import sys, json; doc = sys.stdin.read(); print(json.dumps(json.loads(doc.strip()), indent=2))' > ~/.config/peco/config.json | |
echo "peco用の設定を作成しました" | |
fi | |
# pecoの設定のタイムスタンプを確認 | |
# get_date_info ~/.config/peco/config.json | |
# nvimの設定のタイムスタンプの確認 | |
# get_date_info ~/.config/nvim/init.vim | |
# tmuxの設定のタイムスタンプの確認 | |
# get_date_info ~/.tmux.conf | |
# ~/.jupyter/jupyter_notebook_config.pyが存在するか確認 | |
if [[ ! -L ~/.jupyter/jupyter_notebook_config.py ]]; then | |
mkdir ~/.jupyter | |
ln -s ~/.var/dots/files/jupyter_notebook_config.py ~/.jupyter/jupyter_notebook_config.py | |
fi | |
} | |
check_config | |
# ターミナルのデザインとメタ情報 | |
# 実行ファイルの色を変える | |
export CLICOLOR=1 | |
export LSCOLORS=GxFxCxDxBxegedabagaced | |
# Ubuntu bash likeな色にする | |
autoload -U colors && colors | |
PS1="%{$fg[cyan]%}%n@%m%{$reset_color%}:%1~$ " | |
NEWLINE=$'\n' | |
autoload -U colors && colors | |
autoload -Uz vcs_info | |
zstyle ':vcs_info:*' enable git svn | |
zstyle ':vcs_info:git*' formats "%b" | |
function precmd() { | |
vcs_info | |
if [[ "$vcs_info_msg_0_" = 'master' ]]; then | |
GIT_BRANCH="%{$fg[red]%}${vcs_info_msg_0_}%{$reset_color%}"; | |
else | |
GIT_BRANCH="%{$fg[green]%}${vcs_info_msg_0_}%{$reset_color%}"; | |
fi | |
CWD=%~ | |
# DATETIME="%D{%Y-%m-%d %H:%M:%S}" | |
# ELAPSE=`python3 -c 'import datetime; now = datetime.datetime.now(); start = datetime.datetime(year=now.year, month=1, day=1);end = datetime.datetime(year=start.year+1, month=1, day=1); a = (now - start).total_seconds()/(end - start).total_seconds(); print(f"今年、{a*100:0.07f}%%経過" ,end="")'` | |
PS1="%{$fg[cyan]%}%n@%m%{$reset_color%}:[$CWD] ($GIT_BRANCH) ${NEWLINE}$ " | |
} | |
setopt prompt_subst | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment