Skip to content

Instantly share code, notes, and snippets.

@dvingo
Last active May 4, 2023 00:32
Show Gist options
  • Save dvingo/ef713b519e096710ed559e650035bfcc to your computer and use it in GitHub Desktop.
Save dvingo/ef713b519e096710ed559e650035bfcc to your computer and use it in GitHub Desktop.
.bash_aliases
# Put on the last line of .bashrc:
# [ -f ~/.bash_aliases ] && source ~/.bash_aliases
# In ~/.bash_profile:
# [[ -f ~/.bashrc ]] && . ~/.bashrc
#
os=$(uname -s)
is_linux() {
[ "$os" = "Linux" ]
}
is_mac() {
[ "$os" = "Darwin" ]
}
# -----------------------------------------------------------------------------
# Get the current git branch.
git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
# Delete branches which contain the same history as current branch - local delete only.
git_delete_merged_branches() {
git branch --merged | grep -v "$(git branch --show-current)" | xargs git branch -d
}
# \e[ is interpreted by the terminal emulator as an escape. same as when you enter: C-v esc
# 38;5;${i}m = tells terminal to use 256 color mode. ${i} specifies the color.
# Reference: https://misc.flogisoft.com/bash/tip_colors_and_formatting
print_colors() {
for i in {0..256}; do
printf "\e[38;5;${i}mCOLOR#${i}\e[0m\n"
done
}
#########################################################
# List most recently changed remote branches.
#########################################################
git_recent() {
for branch in $(git branch -r | grep -v HEAD); do
# Intentionally do not quote the git show command.
echo $(git show --format="%ci|%cr|%an" "$branch" | head -n1)'|'"$branch" \
| awk -F '|' '{printf "%s %s %-18s %s\n", $1, $2, $3, $4}'
done | sort -r
}
# Git aliases
alias ga='git add'
alias gd='git diff'
alias gc='git commit'
alias gcnv='git commit --no-verify'
alias gco='git checkout'
alias gb='git branch'
alias gp='git pull'
alias ggraph="git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
alias gl='git log --format="%Cgreen%s %Cblue%h%Creset%n%ad -- %ar %an%n" --date="format:%A %F %T %Z"'
alias gls='git log --format="%n%Cgreen%s %Cblue%h%Creset%n%ad -- %ar %an" --date="format:%A %F %T %Z" --stat'
alias g='git status'
alias gf='git fetch'
alias gfo='git fetch -p origin'
alias grec='git_recent'
alias gr='git remote'
alias gsl='git stash list'
alias gss='git stash show'
alias gpoh='git push origin HEAD'
alias gsearch='git grep "pause-game" $(git rev-list --all -- src/main/space/matterandvoid/arith/data/mult.cljs)'
git_search() {
# todo args
local what="$1"
local path="$2"
git grep "$what" $(git rev-list --all -- "$path")
}
# "my-grep" - recursive search for term
# -r - recursive
# -I - skip binary files
# -n - prefix each line of output with the 1-based line number within its input file.
alias mgrep='grep -rIn --color'
# ls
if is_linux; then
alias l="ls -alhF --time-style='+%F %T'"
alias ll="ls -lhF --time-style='+%F %T'"
alias lt="ls -lthF --time-style='+%F %T'"
elif is_mac; then
alias l='ls -alhGF'
alias ll='ls -lhGF'
alias lt='ls -lthGF'
fi
alias p='cd ~/projects; l'
alias httpserver='python -m http.server'
alias sl='cd ~/projects/solutions-logs; l'
# VPN
alias pvpn='sudo /usr/local/bin/protonvpn'
# Replicate OSX utility on GNU/Linux
# Usage: cat ~/.ssh/myKey.pub | pbcopy
if is_linux; then
function pbcopy {
xclip -selection clipboard
}
fi
# https://wiki.archlinux.org/index.php/KDE_Wallet#Using_the_KDE_Wallet_to_store_Git_credentials
if is_linux && [ -x /usr/bin/ksshaskpass ]; then
export GIT_ASKPASS='/usr/bin/ksshaskpass'
fi
function dns_update_ip {
if [ $# -ne 3 ]; then
echo "usage: "
echo "dns_update_ip <domain> <ip> <password>"
echo "see here for more info: "
echo "https://www.namecheap.com/support/knowledgebase/article.aspx/29/11/how-do-i-use-a-browser-to-dynamically-update-the-hosts-ip/"
return
fi
local domain="$1"
local ip="$2"
local password="$3"
local url="https://dynamicdns.park-your-domain.com/update?host=@&domain="
local full_url="${url}${domain}&password=${password}&ip=${ip}"
curl "$full_url"
}
# push to every remote in the current git repo.
git_push_all() {
for remote in $(git remote); do
printf "\n\nPushing to:\n$(git remote -v | grep "$remote" | sed -n 1p)\n"
echo git push "$remote" HEAD;
git push "$remote" HEAD;
done
}
# Add and commit all files
git_do_commit () {
echo git add -A
git add -A
git status
# make a commit
if ! git diff-index --quiet HEAD; then
if [ -n "$1" ]; then
echo "New commit message is :
'$1'"
git commit -m "$1"
else
while read -ren 120 -p "Commit message: " msg; do
if [ -n "$msg" ]; then
echo "New commit message is :
'$msg'"
git commit -m "$msg"
break
else
printf "Enter a messaGE!!!!\n\n"
fi
done
fi
else
echo No changes to commit.
exit 1
fi
git_push_all
}
alias gdc='git_do_commit'
alias gpa='git_push_all'
#
# # ex - archive extractor
# # usage: ex <file>
ex ()
{
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via ex()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# allow aliases to work when using sudo
# https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Aliases
# https://linuxhandbook.com/run-alias-as-sudo
alias sudo='sudo '
if is_mac; then
alias tcp-listen='sudo lsof -P -iTCP -sTCP:LISTEN'
elif is_linux; then
alias tcp-listen='netstat -tulnp'
fi
yt-dl-music() {
youtube-dl \
--extract-audio \
--audio-format best \
--audio-quality 0 \
"$1"
}
# Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
PATH="$PATH:$HOME/.local/bin"
# find brew's python version first if on mac
if is_mac; then
PATH="/usr/local/opt/python/libexec/bin:$PATH"
fi
# For python pip executables
if is_mac; then
PATH="$PATH:$(python -m site --user-base)/bin"
fi
# PS1='\[\033[01;32m\]\t\[\033[00m\]:\[\033[01;34m\]\w \n\$\[\033[00m\] '
# With git branch:
if [ $SHELL = /bin/bash ]; then
PS1='\[\033[01;32m\]\d, \t\[\033[00m\]:\[\033[01;34m\]\w \[\033[01;32m\]($(git_branch))\[\033[00m\] \n\$\[\033[00m\] '
fi
# This is from omyzsh
function __git_prompt_git() {
GIT_OPTIONAL_LOCKS=0 command git "$@"
}
function git_prompt_info() {
# If we are on a folder not tracked by git, get out.
# Otherwise, check for hide-info at global and local repository level
if ! __git_prompt_git rev-parse --git-dir &> /dev/null \
|| [[ "$(__git_prompt_git config --get oh-my-zsh.hide-info 2>/dev/null)" == 1 ]]; then
return 0
fi
local ref
ref=$(__git_prompt_git symbolic-ref --short HEAD 2> /dev/null) \
|| ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) \
|| return 0
# Use global ZSH_THEME_GIT_SHOW_UPSTREAM=1 for including upstream remote info
local upstream
if (( ${+ZSH_THEME_GIT_SHOW_UPSTREAM} )); then
upstream=$(__git_prompt_git rev-parse --abbrev-ref --symbolic-full-name "@{upstream}" 2>/dev/null) \
&& upstream=" -> ${upstream}"
fi
echo ${ref}${upstream}
#echo "${ZSH_THEME_GIT_PROMPT_PREFIX}${ref}${upstream}$(parse_git_dirty)${ZSH_THEME_GIT_PROMPT_SUFFIX}"
}
# %n - username,
# %y - tty,
# %h - current history event num.
# %F, start bg oclor %f - stop bg color
# %U start underline, %u stop underline
# %B start bold, %b end bold
# %D yy-mm-dd date
# %W mm/dd/yy date
# %n username
# This is needed to allow running functions
if [ $SHELL = /bin/zsh ]; then
setopt prompt_subst
PROMPT='%U%F{cyan}%D{%T - %F %a} %b %~:%u ($(git_prompt_info))%f
$ '
fi
new_clj_app () {
if [ -z "$1" ]; then
echo 'Pass a name.'
echo "new_clj_app com.myorg/my-app"
return
fi
local name="$1"
echo -e clj -Tclj-new create \
:template "'"'"/Users/user/projects/dv.fulcro-template::dv.fulcro-template"'"'" \
:args "'"'["+workspaces" "+node-server" "+test" "+server"]'"'" \
:name "$name"
clj -Tclj-new create \
:template '"/Users/user/projects/dv.fulcro-template::dv.fulcro-template"' \
:args '["+workspaces" "+node-server" "+test" "+server"]' \
:name "$name"
}
## karma test running
export CHROME_BIN=/usr/bin/brave-browser
### pyenv
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
#####
### golang
#####
export PATH=$PATH:/usr/local/go/bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment