Skip to content

Instantly share code, notes, and snippets.

@m-manu
Created January 19, 2021 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-manu/21d953cac27b50a84559b678ef6bd67f to your computer and use it in GitHub Desktop.
Save m-manu/21d953cac27b50a84559b678ef6bd67f to your computer and use it in GitHub Desktop.
# zsh prompt:
export PROMPT="%/ %% "
# zsh styles:
export CLICOLOR=1
export STYLE_COLOR_RED="\x1b[31;01m"
export STYLE_COLOR_GREEN="\x1b[32;01m"
export STYLE_COLOR_BLUE="\x1b[34;01m"
export STYLE_UNDERLINE="\x1b[04;01m"
export STYLE_BLINK="\x1b[05;01m"
export STYLE_HIGHLIGHT="\x1b[07;01m"
export STYLE_RESET="\x1b[39;49;00m"
# zsh history:
export HISTFILE="$HOME/.zsh_history"
export HISTSIZE=10000
export SAVEHIST=10000
setopt SHARE_HISTORY
setopt EXTENDED_HISTORY
# Better shell commands
alias ll="ls -lh"
alias l.="ls -d .*"
alias cd1="cd .."
alias cd2="cd ../.."
alias cd3="cd ../../.."
alias cd4="cd ../../../.."
alias grep="grep --color"
# Other options:
setopt interactivecomments
# Functions for use in scripts:
function err() {
echo -e "$STYLE_COLOR_RED"$*"$STYLE_RESET" 1>&2
}
export err
function edit() {
# touch "$1"
open -a "Visual Studio Code" "$1"
}
function title() {
echo -n -e "\033]0;$1\007"
}
function hosts() {
if [ $# -eq 0 ]; then
hosts_file_num_lines=$(wc -l /etc/hosts | awk '{print $1}')
marker_string_frequency=$(grep StevenBlack /etc/hosts | wc -l)
if [ "$marker_string_frequency" -gt 1 ]; then
echo -e $STYLE_COLOR_GREEN"/etc/hosts file contains entries for AdBlock"$STYLE_RESET" (contains $hosts_file_num_lines lines)"
else
echo -e $STYLE_COLOR_GREEN"/etc/hosts file is right now pristine"$STYLE_RESET" (contains $hosts_file_num_lines lines)"
fi
elif [ $# -eq 1 ]; then
if [ "$1" = "pristine" ]; then
if [ -f /etc/hosts_pristine ]; then
sudo cp -v /etc/hosts_pristine /etc/hosts
echo -e $STYLE_COLOR_GREEN"Replaced /etc/hosts file with a pristine one"$STYLE_RESET
else
err "File /etc/hosts_pristine doesn't exist!"
fi
elif [ "$1" = "adblock" ]; then
if [ -f /etc/hosts_pristine -a -f /etc/hosts_adblock ]; then
sudo bash -c 'cat /etc/hosts_pristine /etc/hosts_adblock > /etc/hosts'
echo -e $STYLE_COLOR_GREEN"Replaced /etc/hosts file with an AdBlock one"$STYLE_RESET
else
err "Either /etc/hosts_pristine or /etc/hosts_adblock (or both) missing!"
fi
elif [ "$1" = "update" ]; then
wc /etc/hosts_adblock
sudo bash -c 'curl https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts > /etc/hosts_adblock'
wc /etc/hosts_adblock
echo -e $STYLE_COLOR_BLUE"Updated /etc/hosts_adblock from internet"$STYLE_RESET
else
err "Invalid parameter: $1"
fi
else
err "Invalid number of arguments!"
fi
}
function git-backup() {
if [ $# -ne 2 ]; then
err "Usage: git-backup <git-repo-dir> <backup-dir>"
return
fi
git_repo_path="$1"
backup_dest_path="$2"
git_repo_dir_name=$(basename "$git_repo_path")
bundle_file_path="$backup_dest_path/$git_repo_dir_name.bundle"
echo "Bundling git repo \"$git_repo_path\" to \"$bundle_file_path\":"
if [ -d "$backup_dest_path" -a -w "$backup_dest_path" ]; then
git -C "$git_repo_path" bundle create "$bundle_file_path" --all
else
err $STYLE_COLOR_RED"Error: \"$backup_dest_path\" should be a writeable directory. It isn't."$STYLE_RESET
fi
}
function find-unique-extensions() {
find . -type d -name ".svn" -prune -o -type d -name ".git" -prune -o -type f -print | sed "s/^.*\.//g" | grep -v "/" | sort | uniq
}
export SOURCE_EXTENSIONS="htm,java,sh,scala,xml,xsd,xsl,html,css,js,properties,sql,cpp,conf,cfg,md,json,yml,yaml,thrift,txt,gradle,sbt,ru,rb,gitignore,tmpl,toml,rake,r,py,svg,r,tex,kt,go,php"
export SOURCE_FILENAME_PATTERNS="\.bashrc,control,preinst,postinst,prerm,postrm,\.zprofile"
function echo_source_files_pattern() {
echo -n "\""
for e in $(echo "$SOURCE_EXTENSIONS" | tr "," " ")
do
echo -n '.*\.'$e'$|'
done
for e in $(echo "$SOURCE_FILENAME_PATTERNS" | tr "," " ")
do
echo -n ".*$e$|"
done
echo -n "^$\""
}
function sf() {
find -E . -name ".idea" -prune -o -name ".git" -prune -o -type f -iregex "$(echo_source_files_pattern)" -print
}
function s() {
input_dir="$1"
if [ -z "$input_dir" ]; then
input_dir="."
fi
sf | xargs grep --color "$@"
}
# Additional bin paths:
export PATH="$HOME/bin:$PATH" # Gets priority
export PATH="$PATH:/opt/Android/sdk/platform-tools"
# For GNUPG
export GPG_TTY=$(tty)
# Go:
export GOPATH="$HOME/go"
# Java:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home"
function print-metadata() {
if [ $# -eq 0 ]; then
gfind . -type f -printf '%p\t%s\t%TY-%Tm-%Td %Tr\n' | sort
else
err "Error: Does not take any command-line argument"
fi
}
function switch-key {
link_path="$HOME/.ssh/id_rsa"
if [ $# -eq 0 ]; then
readlink "$link_path"
grep "IdentityFile" $HOME/.ssh/config
else
key_file="$HOME/.ssh/id_rsa_$1"
sed -i '' "s/\.ssh\/id_rsa.*$/\.ssh\/id_rsa_$1/g" ~/.ssh/config
if [ -f "$key_file" ]; then
rm "$link_path"
ln -v -s "$key_file" "$link_path"
else
err "File $key_file doesn't exist"
fi
fi
}
# Scala:
export PATH="/usr/local/opt/scala@2.12/bin:$PATH"
# DNS:
function dns() {
if [ $# -lt 1 -o $# -gt 2 ]; then
err "Error: Invalid number of arguments!"
return
fi
arg_domain="$1"
arg_record_type="all"
if [ "$2" != "" ]; then
arg_record_type="$2"
fi
curl -s "https://dns.google/resolve?name=$arg_domain&type=$arg_record_type" | python3 -m json.tool
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment