From 300f426b15d5f5dac8ae40b4c9d498ce646e6d82 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sat, 6 Aug 2022 20:24:34 +0100 Subject: [PATCH] Updates ZSH scripts --- zsh/.zlogin | 6 +- zsh/.zshenv | 4 +- zsh/.zshrc | 3 + zsh/aliases/general.zsh | 93 +++++++++++++++++--- zsh/aliases/git.zsh | 3 + zsh/aliases/node-js.zsh | 102 ++++++++++++++++------ zsh/helpers/misc-stuff.zsh | 7 +- zsh/helpers/setup-antigen.zsh | 3 +- zsh/lib/colors.zsh | 155 +++++++++++++++++++++++++++++++++- zsh/lib/completion.zsh | 3 + zsh/lib/expansions.zsh | 28 ++++++ 11 files changed, 358 insertions(+), 49 deletions(-) create mode 100644 zsh/lib/expansions.zsh diff --git a/zsh/.zlogin b/zsh/.zlogin index 9b5709f..2485698 100644 --- a/zsh/.zlogin +++ b/zsh/.zlogin @@ -1,9 +1,5 @@ - # ~/.zlogin - - - # Compile .zcompdump file, if modified, to increase startup speed # Executed on startin the background, so won't afftect current session # Credit @htr3n. More info: https://htr3n.github.io/2018/07/faster-zsh/ @@ -13,4 +9,4 @@ then zcompile "$zcompdump" fi -} &! \ No newline at end of file +} &! diff --git a/zsh/.zshenv b/zsh/.zshenv index 187e3fe..32466f7 100644 --- a/zsh/.zshenv +++ b/zsh/.zshenv @@ -16,7 +16,7 @@ export PAGER="less" ## Respect XDG directories export ADOTDIR="${XDG_CACHE_HOME}/zsh/antigen" -# export ANTIGEN_LOG="${XDG_CACHE_HOME}/zsh/antigen" +export OPENSSL_DIR="/usr/local/ssl" export ANTIBODY_HOME=${XDG_DATA_HOME}/antibody export CARGO_HOME="${XDG_DATA_HOME}/cargo" export DOCKER_CONFIG="${XDG_CONFIG_HOME}/docker" @@ -32,4 +32,4 @@ export XSERVERRC="${XDG_CONFIG_HOME}/X11/xserverrc" export ZDOTDIR="${XDG_CONFIG_HOME}/zsh" export ZLIB="${ZDOTDIR}/lib" -# source $XDG_CONFIG_HOME/zsh/.zshrc \ No newline at end of file +# source $XDG_CONFIG_HOME/zsh/.zshrc diff --git a/zsh/.zshrc b/zsh/.zshrc index 3966e5f..aad1c08 100644 --- a/zsh/.zshrc +++ b/zsh/.zshrc @@ -34,9 +34,12 @@ source ${utils_dir}/matrix.sh source ${utils_dir}/hr.sh source ${utils_dir}/web-search.sh source ${utils_dir}/am-i-online.sh +source ${utils_dir}/welcome-banner.sh # Left over tasks source ${zsh_dir}/helpers/misc-stuff.zsh # Import P10k config for command prompt, run `p10k configure` or edit [[ ! -f ${zsh_dir}/.p10k.zsh ]] || source ${zsh_dir}/.p10k.zsh + +welcome diff --git a/zsh/aliases/general.zsh b/zsh/aliases/general.zsh index 3e11b1d..07bd8c6 100644 --- a/zsh/aliases/general.zsh +++ b/zsh/aliases/general.zsh @@ -3,8 +3,19 @@ command_exists () { hash "$1" 2> /dev/null } +# Frequently used basics +alias a='alias' +alias c='clear' +alias e='exit' +alias f='find' +alias l='ls' +alias m='man' +alias p='pwd' +alias s='sudo' +alias t='touch' +alias v='vim' + # File listing options -alias l='ls' # Quick ls alias la='ls -A' # List all files/ includes hidden alias ll='ls -lAFh' # List all files, with full details alias lm='ls -tA -1' # List files sorted by last modified @@ -24,6 +35,60 @@ if command_exists exa ; then alias tree='f() { exa -aF --tree -L=${1:-2} --icons };f' fi +# List contents of packed file, depending on type +ls-archive () { + if [ -z "$1" ]; then + echo "No archive specified" + return; + fi + if [[ ! -f $1 ]]; then + echo "File not found" + return; + fi + ext="${1##*.}" + if [ $ext = 'zip' ]; then + unzip -l $1 + elif [ $ext = 'rar' ]; then + unrar l $1 + elif [ $ext = 'tar' ]; then + tar tf $1 + elif [ $ext = 'tar.gz' ]; then + echo $1 + elif [ $ext = 'ace' ]; then + unace l $1 + else + echo "Unknown Archive Format" + fi +} + +alias lz='ls-archive' + +# Make directory, and cd into it +mkcd() { + local dir="$*"; + local mkdir -p "$dir" && cd "$dir"; +} + +# Make dir and copy +mkcp() { + local dir="$2" + local tmp="$2"; tmp="${tmp: -1}" + [ "$tmp" != "/" ] && dir="$(dirname "$2")" + [ -d "$dir" ] || + mkdir -p "$dir" && + cp -r "$@" +} + +# Move dir and move into it +mkmv() { + local dir="$2" + local tmp="$2"; tmp="${tmp: -1}" + [ "$tmp" != "/" ] && dir="$(dirname "$2")" + [ -d "$dir" ] || + mkdir -p "$dir" && + mv "$@" +} + # Getting outa directories alias c~='cd ~' alias c.='cd ..' @@ -42,6 +107,7 @@ alias ff='find . -type f -name' # Find a file by name within current directory # Command line history alias h='history' # Shows full history alias h-search='fc -El 0 | grep' # Searchses for a word in terminal history +alias top-history='history 0 | awk '{print $2}' | sort | uniq -c | sort -n -r | head' # Clearing terminal if command_exists hr ; then @@ -73,29 +139,32 @@ alias as='alias | grep' # Search aliases alias ar='unalias' # Remove given alias # System Monitoring -alias meminfo='free -m -l -t' -alias psmem='ps auxf | sort -nr -k 4' # Show top memory eater -alias psmem10='ps auxf | sort -nr -k 4 | head -10' # Top 10 memory eaters -alias pscpu='ps auxf | sort -nr -k 3' # Show top CPU eater -alias pscpu10='ps auxf | sort -nr -k 3 | head -10' # Top 10 CPU eaters +alias meminfo='free -m -l -t' # Show free and used memory +alias memhog='ps -eo pid,ppid,cmd,%mem --sort=-%mem | head' # Processes consuming most mem +alias cpuhog='ps -eo pid,ppid,cmd,%cpu --sort=-%cpu | head' # Processes consuming most cpu alias cpuinfo='lscpu' # Show CPU Info alias distro='cat /etc/*-release' # Show OS info +# App Specific +if command_exists code ; then + alias vsc='code .' # Open VS Code in current dir +fi + # Utilities alias myip='curl icanhazip.com' alias weather='curl wttr.in' alias weather-short='curl "wttr.in?format=3"' +alias cheat='curl cheat.sh/' +alias tinyurl='curl -s "http://tinyurl.com/api-create.php?url=' alias ports='netstat -tulanp' -if command_exists cointop ; then - alias crypto='cointop' -fi +if command_exists cointop ; then; alias crypto='cointop'; fi +if command_exists gotop ; then; alias gto='gotop'; fi -# Random +# Random lolz alias cls='clear;ls' # Clear and ls alias plz="fc -l -1 | cut -d' ' -f2- | xargs sudo" # Re-run last cmd as root alias yolo='git add .; git commit -m "YOLO"; git push origin master' alias when='date' # Show date alias whereami='pwd' alias dog='cat' -alias simonsays='sudo' -alias gtfo='exit' \ No newline at end of file +alias gtfo='exit' diff --git a/zsh/aliases/git.zsh b/zsh/aliases/git.zsh index 5c330e8..f4bfe06 100644 --- a/zsh/aliases/git.zsh +++ b/zsh/aliases/git.zsh @@ -245,3 +245,6 @@ open-github-pulls() { } alias ghp='open-github-pulls' + +# Prompt for main SSH key passphrase, so u don't need to enter it again until session killed +alias add-key='eval "$(ssh-agent)" && ssh-add ~/.ssh/id_rsa' diff --git a/zsh/aliases/node-js.zsh b/zsh/aliases/node-js.zsh index b8aa810..b43ca10 100644 --- a/zsh/aliases/node-js.zsh +++ b/zsh/aliases/node-js.zsh @@ -1,5 +1,5 @@ -# Aliases and helper functions for Node.js projects, including yarn, npm, nvm +# ZSH helper utils, and aliases for Node.js development (yarn, npn, nvm, node) # Yarn - Project commands alias ys='yarn start' @@ -29,49 +29,56 @@ reinstall_modules () { if read -q "choice?Remove and reinstall all node_modules? (y/N)"; then echo project_dir=$(pwd) - if [ -d "$project_dir/node_modules" ]; then - echo -e "\e[35mRemoveing node_modules...\e[0m" - rm -rf "$project_dir/node_modules" - fi - if [ -f "$project_dir/yarn.lock" ]; then - echo -e "\e[35mRemoveing yarn.lock...\e[0m" - rm "$project_dir/yarn.lock" - fi - if [ -f "$project_dir/package-lock.json" ]; then - echo -e "\e[35mRemoveing package-lock.json..." - rm "$project_dir/package-lock.json" - fi + # Check file exists, remove it and print message + check-and-remove() { + if [ -d "$project_dir/$1" ]; then + echo -e "\e[35mRemoveing $1...\e[0m" + rm -rf "$project_dir/$1" + fi + } + # Delete node_modules and lock files + check-and-remove 'node_modules' + check-and-remove 'yarn.lock' + check-and-remove 'package-lock.json' + # Reinstall with yarn (or NPM) if hash 'yarn' 2> /dev/null; then echo -e "\e[35mReinstalling with yarn...\e[0m" yarn echo -e "\e[35mCleaning Up...\e[0m" yarn autoclean - else + elif hash 'npm' 2> /dev/null; then echo -e "\e[35mReinstalling with NPM...\e[0m" npm install + else + echo -e "🚫\033[0;91m Unable to proceed, yarn/ npm not installed\e[0m" fi else - echo -e "\n\e[35mAborting...\e[0m" + # Cancelled by user + echo -e "\n\033[0;91mAborting...\e[0m" fi } -alias yn='reinstall_modules' +alias yarn-nuke='reinstall_modules' # Prints out versions of core Node.js packages print_node_versions () { - nreset='\033[0m' versions='' get_version () { - if hash $1 2> /dev/null; then - versions="$versions\e[36m\e[1m $2: \033[0m\033[3m$($1 --version)\n" + if hash $1 2> /dev/null || command -v $1 >/dev/null; then + versions="$versions\e[36m\e[1m $2: \033[0m$($1 --version)\n\033[0m" else - versions="$versions\e[33m\e[1m $2: \033[0m\033[3m Not installed\n" + versions="$versions\e[33m\e[1m $2: \033[0m\033[3m Not installed\n\033[0m" fi } + # Source NVM if not yet done + if typeset -f source_nvm > /dev/null && source_nvm + + # Print versions of core Node things get_version 'node' 'Node.js' get_version 'npm' 'NPM' get_version 'yarn' 'Yarn' get_version 'nvm' 'NVM' + get_version 'git' 'Git' echo -e $versions } @@ -90,10 +97,17 @@ alias npmp='npm publish' # Location of NVM, will inherit from .zshenv if set NVM_DIR=${NVM_DIR:-$XDG_DATA_HOME/nvm} -# If NVM present, then import it -if [ -f "$nvm_location/nvm.sh" ]; then - source "${NVM_DIR}/nvm.sh" -fi + +# On first time using Node command, import NVM if present and not yet sourced +function source_nvm node npm yarn $NVM_LAZY_CMD { + if [ -f "$NVM_DIR/nvm.sh" ] && ! which nvm &> /dev/null; then + echo -e "\033[1;93mInitialising NVM...\033[0m" + source "${NVM_DIR}/nvm.sh" + nvm use default + fi + unfunction node npm yarn source_nvm $NVM_LAZY_CMD + hash "$0" 2> /dev/null && command "$0" "$@" +} # Helper function to install NVM install_nvm () { @@ -125,3 +139,43 @@ alias nvmlr='nvm ls-remote' alias nvmlts='nvm install --lts && nvm use --lts' alias nvmlatest='nvm install node --latest-npm && nvm use node' alias nvmsetup='install_nvm' + +# Helper function that gets supported open method for system +launch-url() { + if hash open 2> /dev/null; then + open_command=open + elif hash xdg-open 2> /dev/null; then + open_command=xdg-open + elif hash lynx 2> /dev/null; then + open_command=lynx + else + echo -e "\033[0;33mUnable to launch browser, open manually instead" + echo -e "\033[1;96m🌐 URL: \033[0;96m\e[4m$1\e[0m" + return; + fi + echo $open_command +} + +# Open Node.js docs, either specific page or show all +function node-docs { + local section=${1:-all} + launch-url "https://nodejs.org/docs/$(node --version)/api/$section.html" +} + +# Launches npmjs.com on the page of a specific module +open-npm () { + npm_base_url=https://www.npmjs.com + if [ $# -ne 0 ]; then + # Get NPM module name from user input + npm_url=$npm_base_url/package/$@ + else + # Unable to get NPM module name, default to homepage + npm_url=$npm_base_url + fi + # Print messages + echo -e "\033[1;96m📦 Opening in browser: \033[0;96m\e[4m$npm_url\e[0m" + # And launch! + launch-url $npm_url +} + +alias npmo='open-npm' diff --git a/zsh/helpers/misc-stuff.zsh b/zsh/helpers/misc-stuff.zsh index 6c0eb23..bc00230 100644 --- a/zsh/helpers/misc-stuff.zsh +++ b/zsh/helpers/misc-stuff.zsh @@ -1,4 +1,9 @@ if hash thefuck 2> /dev/null; then; eval $(thefuck --alias) -fi \ No newline at end of file +fi + +# Fix default editor, for systems without nvim installed +if ! hash nvim 2> /dev/null; then + alias nvim='vim' +fi diff --git a/zsh/helpers/setup-antigen.zsh b/zsh/helpers/setup-antigen.zsh index 74b14db..3cad9fe 100644 --- a/zsh/helpers/setup-antigen.zsh +++ b/zsh/helpers/setup-antigen.zsh @@ -2,6 +2,7 @@ zsh_dir=${XDG_CONFIG_HOME:-$HOME/.config}/zsh antigen_dir=${ADOTDIR:-$XDG_DATA_HOME/zsh/antigen} +antigen_git="https://raw.githubusercontent.com/zsh-users/antigen/master/bin/antigen.zsh" antigen_bin="${ADOTDIR}/antigen.zsh" @@ -12,7 +13,7 @@ else if read -q "choice?Would you like to install Antigen now? (y/N)"; then echo mkdir -p $antigen_dir - curl -L git.io/antigen > $antigen_bin + curl -L $antigen_git > $antigen_bin source $antigen_bin fi fi diff --git a/zsh/lib/colors.zsh b/zsh/lib/colors.zsh index 92b38bb..84997d9 100644 --- a/zsh/lib/colors.zsh +++ b/zsh/lib/colors.zsh @@ -11,13 +11,160 @@ if type -p dircolors &>/dev/null; then eval "$(dircolors -b)" fi -# Colorize 'ls' command. Based on Oh My Zsh; -ls --color -d . &>/dev/null && alias ls='ls --color=tty' || { ls -G . &>/dev/null && alias ls='ls -G' } +# Add auto color to certain commands alias vdir="vdir --color=auto" # Colorize 'vdir' command. - alias grep="grep --color=auto -i" # Colorize 'grep' command and ignore case. alias fgrep="grep --color=auto -i" # Colorize 'fgrep' command and ignore case. alias egrep="grep --color=auto -i" # Colorize 'egrep' command and ignore case. - alias diff="diff --color=auto" # Colorize 'diff' command. + +# Color strings for basic file types +# FILE 01;34 # regular file +# RESET 0 # reset to "normal" color +# DIR 01;37 # directory +# LINK 01;36 # symbolic link +# MULTIHARDLINK 00 # reg file with more than one link +# FIFO 40;33 # pipe +# SOCK 01;35 # socket +# DOOR 01;35 # door +# BLK 40;33;01 # block device driver +# CHR 40;33;01 # character device driver +# ORPHAN 40;31;01 # symlink to nonexistent file, or non-stat'able file ... +# MISSING 00 # ... and the files they point to +# SETUID 37;41 # file that is setuid (u+s) +# SETGID 30;43 # file that is setgid (g+s) +# CAPABILITY 30;41 # file with capability +# STICKY_OTHER_WRITABLE 01;42 # dir that is sticky and other-writable (+t,o+w) +# OTHER_WRITABLE 01;32 # dir that is other-writable (o+w) and not sticky +# STICKY 37;44 # dir with the sticky bit set (+t) and not other-writable +# EXEC 01;32 # Files with execute permission + +# Executables +# .cmd 01;32 +# .exe 01;32 +# .com 01;32 +# .btm 01;32 +# .bat 01;32 +# .sh 01;32 +# .csh 01;32 + +# Archives +# .tar 01;31 +# .tgz 01;31 +# .arc 01;31 +# .arj 01;31 +# .taz 01;31 +# .lha 01;31 +# .lz4 01;31 +# .lzh 01;31 +# .lzma 01;31 +# .tlz 01;31 +# .txz 01;31 +# .tzo 01;31 +# .t7z 01;31 +# .zip 01;31 +# .z 01;31 +# .Z 01;31 +# .dz 01;31 +# .gz 01;31 +# .lrz 01;31 +# .lz 01;31 +# .lzo 01;31 +# .xz 01;31 +# .zst 01;31 +# .tzst 01;31 +# .bz2 01;31 +# .bz 01;31 +# .tbz 01;31 +# .tbz2 01;31 +# .tz 01;31 +# .deb 01;31 +# .rpm 01;31 +# .jar 01;31 +# .war 01;31 +# .ear 01;31 +# .sar 01;31 +# .rar 01;31 +# .alz 01;31 +# .ace 01;31 +# .zoo 01;31 +# .cpio 01;31 +# .7z 01;31 +# .rz 01;31 +# .cab 01;31 +# .wim 01;31 +# .swm 01;31 +# .dwm 01;31 +# .esd 01;31 + +# Images +# .jpg 01;35 +# .jpeg 01;35 +# .mjpg 01;35 +# .mjpeg 01;35 +# .gif 01;35 +# .bmp 01;35 +# .pbm 01;35 +# .pgm 01;35 +# .ppm 01;35 +# .tga 01;35 +# .xbm 01;35 +# .xpm 01;35 +# .tif 01;35 +# .tiff 01;35 +# .png 01;35 +# .svg 01;35 +# .svgz 01;35 +# .mng 01;35 +# .pcx 01;35 +# .mov 01;35 +# .mpg 01;35 +# .mpeg 01;35 +# .m2v 01;35 +# .mkv 01;35 +# .webm 01;35 +# .ogm 01;35 +# .mp4 01;35 +# .m4v 01;35 +# .mp4v 01;35 +# .vob 01;35 +# .qt 01;35 +# .nuv 01;35 +# .wmv 01;35 +# .asf 01;35 +# .rm 01;35 +# .rmvb 01;35 +# .flc 01;35 +# .avi 01;35 +# .fli 01;35 +# .flv 01;35 +# .gl 01;35 +# .dl 01;35 +# .xcf 01;35 +# .xwd 01;35 +# .yuv 01;35 +# .cgm 01;35 +# .emf 01;35 +# .ogv 01;35 +# .ogx 01;35 + +# Audio +# .aac 00;36 +# .au 00;36 +# .flac 00;36 +# .m4a 00;36 +# .mid 00;36 +# .go 00;33 +# .php 00;33 +# .midi 00;36 +# .mka 00;36 +# .mp3 00;36 +# .mpc 00;36 +# .ogg 00;36 +# .ra 00;36 +# .wav 00;36 +# .oga 00;36 +# .opus 00;36 +# .spx 00;36 +# .xspf 00;36 diff --git a/zsh/lib/completion.zsh b/zsh/lib/completion.zsh index 2ce2586..5daf1a4 100644 --- a/zsh/lib/completion.zsh +++ b/zsh/lib/completion.zsh @@ -46,6 +46,9 @@ autoload -Uz compinit # Enable extended globbing. setopt extendedglob +# Allow SSH tab completion for mosh hostnames +compdef mosh=ssh + # Location for completions zcompdump="${XDG_CACHE_HOME:-${HOME}/.cache}/zsh/.zcompdump" diff --git a/zsh/lib/expansions.zsh b/zsh/lib/expansions.zsh new file mode 100644 index 0000000..3486836 --- /dev/null +++ b/zsh/lib/expansions.zsh @@ -0,0 +1,28 @@ + +# Expands all glob expressions, subcommands and aliases (including global) +# Inspired by: https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/globalias + +globalias() { + # Get last word to the left of the cursor, and split into array + local word=${${(Az)LBUFFER}[-1]} + # If matching alias found, then expand + if [[ $GLOBALIAS_FILTER_VALUES[(Ie)$word] -eq 0 ]]; then + zle _expand_alias + zle expand-word + fi + zle self-insert +} + +# Make function availible +zle -N globalias + +# space expands all aliases, including global +bindkey -M emacs " " globalias +bindkey -M viins " " globalias + +# control-space to make a normal space +bindkey -M emacs "^ " magic-space +bindkey -M viins "^ " magic-space + +# normal space during searches +bindkey -M isearch " " magic-space