Help and docs
Sign in

grindlemire/dotfiles code browser

nvim 18f6679
Bash · 196 lines · 5.2 KB
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
# Get the nice ^r, ^a, and ^e behavior
set -o emacs

# aliases
alias la='ls -a'   # hidden
alias ll='ls -lh'  # long
alias lt='ls -lth' # long time sorted
alias lla='ls -lha' # long and hidden
alias emacs='emacs -nw'
alias cap='ret=$?'
alias check='[ $ret == 0 ] && true || false'
alias vd='cd'

# ls colors for bsd/linux
ls --color &>/dev/null 2>&1 && alias ls='ls --color=tty' || alias ls='ls -G'

Red="\033[0;31m"
Green='\033[0;32m'
Color_Off='\033[0m'

godir() {
    cd $HOME/go/src/github.com/grindlemire/$@
}

# Installed Apps and scripts added to PATH
export PATH=~/Apps/bin:~/dotfiles/scripts:$PATH

# editor needs to be set for commits without -m
export EDITOR='vim'

# go stuff
export GOPATH=$HOME/go
export PATH=/usr/local/go/bin:$GOPATH/bin:/usr/local/bin:$PATH

# git signing stuff
git_sign_init() {
    git config --global gpg.format ssh
    git config --global user.signingKey "$(cat ~/.ssh/$1.pub)"
    git config --global commit.gpgsign true
    git config --global tag.gpgsign true
}

has_param() {
    local term="$1"
    shift
    for arg; do
        if [[ $arg == "$term" ]]; then
            return 0
        fi
    done
    return 1
}

gitprune() {
    CMD='git branch -D $branch'
    if has_param '--dry' "$@"; then
        CMD='echo "${Green}$branch ${Color_Off}is merged into main and can be deleted"'
    else
        # fetch all the remote branches and remove the deleted branches from autocomplete
        git fetch --prune --all
    fi

    # taken from https://stackoverflow.com/questions/43489303/how-can-i-delete-all-git-branches-which-have-been-squash-and-merge-via-github
    git checkout -q main && git for-each-ref refs/heads/ "--format=%(refname:short)" | \
    while read branch; 
        do mergeBase=$(git merge-base main $branch) && 
        [[ $(git cherry main $(git commit-tree $(git rev-parse "$branch^{tree}") -p $mergeBase -m _)) == "-"* ]] && 
        eval "$CMD"; 
    done
    return 0
}

# turn a video into a gif
to_gif() {
    FNAME="$(echo $1 | xargs -I file basename file .mov)"
    cmd="ffmpeg -i $1 \
        -vf \"fps=12,scale=1280:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse\" \
        -loop 0 ${FNAME}.gif"
    echo -e "${Green} Running Cmd:\n    ${cmd}${Color_Off}\n"
    eval "$cmd"
}

###############################################################################
# zsh stuff
###############################################################################

sum() {
    awk '{ sum += $1 } END { print sum }'
}

myip() {
    ifconfig en0 | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'
}

# set window title and share pwd accross sessions
update_terminal_cwd() {
    local PWD_URL="file://$HOSTNAME${PWD// /%20}"
    printf '\e]7;%s\a' "$PWD_URL"
}

clone() {
	git clone [email protected]:$1
}

fixssh() {
	. ensure-ssh-agent
}

# this will prune git branches that have been merged and allows you to edit them before deleting them
alias gprune=gitprune

git_branch() {
        printf "%s" "$(git branch 2>/dev/null | grep \* | awk -F '\\* ' '{$0=$2}1')"
}

# alias git pull and git push from the current branch
gpull() {
    BRANCH=$(git_branch)
    if [ -n "$1" ]; then
        BRANCH=$1
    fi

    CMD="git pull origin ${BRANCH}"
    echo -e "${Green} Running Cmd:\n    ${CMD} ${Color_Off}\n"
    eval "$CMD"
}
gpush() {
    BRANCH=$(git_branch)
    if [ -n "$1" ]; then
        BRANCH=$1
    fi

    CMD="git push origin ${BRANCH}"
    echo -e "${Green} Running Cmd:\n    ${CMD} ${Color_Off}\n"
    eval "$CMD"
}


setopt PROMPT_SUBST
# prompt
PROMPT='%(?^%F{green}[%n@%m] [%1~] [$(git_branch)]%f^%F{red}[%n@%m] [%1~] [$(git_branch)]%f)$ '

# in-place delete
bindkey '^[[3~'  delete-char
# zsh history
HISTFILE=$HOME/.zhistory
HISTSIZE=50000
SAVEHIST=50000
# setopt APPEND_HISTORY
setopt SHARE_HISTORY
history() { builtin history 1 }
# up/down searching
autoload -U up-line-or-beginning-search
autoload -U down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
bindkey "^[[A" up-line-or-beginning-search # Up
bindkey "^[[B" down-line-or-beginning-search # Down
# cmd line completion
autoload -U compinit && compinit
zstyle ':completion:*' menu select
# batch rename
autoload -U zmv
# extended globbing but don't error on no match
setopt EXTENDED_GLOB
unsetopt NOMATCH
autoload -U add-zsh-hook
add-zsh-hook precmd update_terminal_cwd
add-zsh-hook preexec update_terminal_cwd
add-zsh-hook chpwd update_terminal_cwd

# set vi mode and make the cursor a block underline cursor if we are in cmd mode
bindkey -v 
# I like these bindings and use them all the time. Make sure they stay the same
bindkey "^R" history-incremental-search-backward
bindkey "^E" end-of-line
bindkey "^A" beginning-of-line
function zle-keymap-select zle-line-init zle-line-finish {
  case $KEYMAP in
    vicmd)      echo -ne "\e[4 q";; # block underline cursor for cmd
    viins|main) echo -ne "\e[5 q";; # line cursor
  esac

  zle reset-prompt
  zle -R
}
zle -N zle-line-init
zle -N zle-line-finish
zle -N zle-keymap-select

# source in the virtualenv helpers
. ~/dotfiles/virtualenv.sh 2>/dev/null
# source in the docker-compose helpers
. ~/dotfiles/docker.sh 2>/dev/null
# source in the untracked environment specific configuration
. ~/dotfiles/local-zshrc.sh 2>/dev/null