diff --git a/home/programs/terminal/shell/zsh/default.nix b/home/programs/terminal/shell/zsh/default.nix index 308c9fc..85ae2b5 100644 --- a/home/programs/terminal/shell/zsh/default.nix +++ b/home/programs/terminal/shell/zsh/default.nix @@ -2,6 +2,7 @@ imports = [ ./plugins.nix ./aliases.nix + ./init.nix ]; config = { @@ -10,38 +11,13 @@ dotDir = ".config/zsh"; enableCompletion = true; enableAutosuggestions = true; - autocd = true; history = { - # share history across different running zsh session - share = true; - # don't clutter $HOME path = "${config.xdg.dataHome}/zsh/zsh_history"; - # save timestamps to histfile - extended = true; - save = 120000; size = 100000; - - # If the internal history needs to be trimmed to add the current command line, - # this will cause the oldest history event that has a duplicate to be lost before - # losing a unique event from the list. You should set the value of history size - # to a larger number than the save size in order to give some room for the duplicated - # events, otherwise this option will behave just like ignoreDups once history fills - # up with unique events. - expireDuplicatesFirst = true; - - # If a new command line being added to the history list duplicates an older one, - # the older command is removed from the list (even if it is not the previous event). - ignoreAllDups = true; - - # Don't track command lines in the history list when the first character on the line - # is a space, or when one of the expanded aliases contains a leading space. Note that - # the command lingers in the internal session history until the next command is entered - # before it vanishes, allowing you to briefly reuse or edit the line. - ignoreSpace = true; }; # dirhashes are easy aliases to commonly used directories diff --git a/home/programs/terminal/shell/zsh/init.nix b/home/programs/terminal/shell/zsh/init.nix new file mode 100644 index 0000000..ebd7626 --- /dev/null +++ b/home/programs/terminal/shell/zsh/init.nix @@ -0,0 +1,31 @@ +_: +let + inherit (builtins) readFile; +in +{ + config = { + programs.zsh = { + initExtra = '' + ${readFile ./rc/opts.zsh} + ${readFile ./rc/prompt.zsh} + ${readFile ./rc/keybinds.zsh} + ${readFile ./rc/fallback_term.zsh} + + ${readFile ./rc/aliases.zsh} + ${readFile ./rc/functions.zsh} + ''; + + completionInit = ''' + ${readFile ./rc/completion.zsh} + ''; + + profileExtra = '' + ${readFile ./rc/profile.zsh} + ''; + + envExtra = '' + ${readFile ./rc/environment.zsh} + ''; + }; + }; +} diff --git a/home/programs/terminal/shell/zsh/rc/aliases.zsh b/home/programs/terminal/shell/zsh/rc/aliases.zsh new file mode 100644 index 0000000..cba64fc --- /dev/null +++ b/home/programs/terminal/shell/zsh/rc/aliases.zsh @@ -0,0 +1,260 @@ +# This file contains all of my custom alias definitions +# I haven't yet finished moving all of these to nix config, so the rest +# still live in this file. Aliases that were defined in aliases.nix will +# be removed from this file. + +# Replacements (adding flags) +alias cp='cp -iv' # Ask before overwriting, verbose +alias mv='mv -iv' # Ask before overwriting, verbose +alias rmt='trash-put' # Use trash-cli instead of true removal +alias rm='rm -v' # Verbose rm (asking before deleting is too annoying here) +alias rmr='rm -r' +alias rmrf='rm -rf' +alias wget='wget -c' # Resume wget by default +alias df='df -H' # Show sizes as powers of 1000 + +# Files/Directories utilities +alias mkdir='mkdir -p' +alias md='mkdir' +alias fhere='find . -name' +alias rr='rmr -r' +alias rf='rmr -f' +alias rrf='rmr -rf' +alias vimdiff='nvim -d' + +# Use eza instead of ls, if it's available +# (the default aliases that use ls are set in aliases.nix) +if command -v eza > /dev/null; then + alias ls='eza' + alias l='eza -glah --classify' + alias ll='eza -glah --classify -s=size --group-directories-first -r' + alias ldir='eza -glahD' + alias tree='eza -Tlagh' + alias dotall='eza -hulad .[a-z]*' # Show both dotdirs and dotfiles + alias dotfiles='dotall | grep -v ^d' # Show all dotfiles + alias dotdirs='dotall | grep --color=never ^d' # Show all dotdirs +fi + +# Config access shortcuts +# (probably useless in nixos) +alias cfzsh='vim ~/.config/zsh/.zshrc' +alias cfalias='vim ~/.config/shell/aliases' +alias cffunctions='vim ~/.config/shell/functions' +alias cfprofile='vim ~/.config/shell/profile' +alias cfenvironment='vim ~/.config/shell/environment' +alias cfenv='cfenvironment' +alias cfhandlers='vim ~/.config/shell/handlers' +alias cfprompt='vim ~/.config/shell/prompt' +alias cfkeybinds='vim ~/.config/shell/keybinds' +alias cftodo='vim ~/Personal/vimwiki/todo.md' +alias cfnvim='vim ~/.config/nvim' +alias cfvim='cfnvim' +alias cfgit='vim ~/.config/git/config' +alias cfhypr='vim ~/.config/hypr' +alias cfsway='vim ~/.config/sway/config' + +# zoxide shortcuts +alias j='z' # for the sake of old habits from autojump +alias ji='zi' # interactive mode (fzf) + +# Fallbacks +command -v hd > /dev/null || alias hd='hexdump -C' # Cannonical hex dump; some systems have this symlinked +command -v md5sum > /dev/null || alias md5sum='md5' # Fallback from `md5sum` to `md5` +command -v sha1sum > /dev/null || alias sha1sum='shasum' # Fallback from `sha1sum` to `shasum` +command -v vim > /dev/null && alias vi='vim' # Let vim take precedence over vi +command -v nvim > /dev/null && alias vi='nvim' && alias vim='nvim' # Let nvim take precedence over vi/vim +command -v vimtutor > /dev/null || alias vimtutor='nvim -c Tutor' # Let vimtutor fallback to nvim's tutor + +if [ "$XDG_SESSION_TYPE" = "wayland" ]; then + # Wayland clipboard + alias pbcopy="wl-copy" + alias pbpaste="wl-paste" +else + # X11 clipboard (either using xclip or xsel, xsel takes precedence if both) + command -v xclip > /dev/null && alias pbcopy='xclip -selection clipboard' + command -v xclip > /dev/null && alias pbpaste='xclip -selection clipboard -o' + command -v xsel > /dev/null && alias pbcopy='xsel --clipboard --input' + command -v xsel > /dev/null && alias pbpaste='xsel --clipboard --output' +fi + +# Regular expressions +# (I know this is a *little* insane, but it's useful...) +alias reg_email='echo "[a-Z0-9._%-]+@[a-Z0-9.-]+\.[a-Z]{2,10}"' +alias reg_mac='echo "([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}"' +alias reg_ipv4='echo "([0-9]{1,3}\.){3}[0-9]{1,3}"' +alias reg_ipv6='echo "\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*"' # Also catches loopbacks (::1), (for valid matching, it needs to be this long...) +alias reg_ip='echo "(`reg_ipv4`|`reg_ipv6`)"' # Match both IPv4 and IPv6 + +# Grep aliases +alias grep_email='grep -E `reg_email`' +alias grep_ip='grep -E `reg_ip`' +alias grep_mac='grep -E `reg_mac`' +alias massgrep='grep -RHIni' + +# Network +alias ip-show='curl https://ifconfig.me' # Get global IP address +alias ips="ifconfig -a | grep -oE \"inet6? (addr:)?s?\`reg_ip\`\" | awk '{ sub(/inet6? (addr:)? ?/, \"\"); print }'" +alias lan-device-scan='nmap -T5 -sP 192.168.0.0-255' +alias lan-vuln-scan='nmap -sT -O --script vuln 192.168.0.0-255' +alias ports='netstat -tulanp' +alias listening-ports='netstat -vtlnp --listening' +alias ssh-list='ss | grep ssh' # List all SSH connections +alias serve='python -m http.server' # Serve current directorty as HTTP +alias reverse-dns='host' # It might be easier to just use `host` though +alias torify='source torsocks on' # Pass every command via torsocks +alias untorify='source torsocks off' # Stop passing commands via torsocks + +# Firewall aliases (IPTables/UFW) +alias ipt='iptables' # Shortcut +alias iptlist='iptables -L -n -v --line-numbers' # All rules +alias iptlistin='iptables -L INPUT -n -v --line-numbers' # IN rules +alias iptlistout='iptables -L OUTPUT -n -v --line-numbers' # OUT rules +alias iptlistfw='iptables -L FORWARD -n -v --line-numbers' # FORWARD rules +alias ufw-log='journalctl -f -n 100 -g ufw' # Show UFW log entries in system journal + +# Kernel actions +alias kernel-recompile='cd /usr/src/linux && make -j7 && make -j7 modules_install && make install' +alias kernel-oldconfig='cd /usr/src/linux && make oldconfig' +alias kernel-configure='cd /usr/src/linux && make menuconfig' + +# System actions +alias sv='systemctl' +alias pacnew="find / -name '*.pacnew' 2>/dev/null" # Search for all new configurations after pacman update +alias backup="rsync -avHAXS --delete --filter='dir-merge /.rsync-filter'" # Make full rsync backup, respecting .rsync-filter files for exclusions +#alias upload='curl -F "f:1=<-" ix.io' # ix.io was offline for about 10 months now... +#alias upload='curl -F "file=@-" https://hardfiles.org' # hardfiles.org works, but has 24-hour expiry +alias upload='curl -F "sprunge=<-" http://sprunge.us' +alias upload-journal='sudo journalctl -b -1 | upload' # Upload journalctl from last boot to ix.io +alias auth-log='journalctl SYSLOG_FACILITY=10 -r' +alias cpu-stress='for i in $(seq $(getconf _NPROCESSORS_ONLN)); do yes > /dev/null & done' # Run `yes > /dev/null` on all cores as stress test +alias cpu-power='sudo turbostat --Summary --quiet --show PkgWatt --interval 1' +alias nvidia='__NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia' # Run app with nvidia (on hybrid mode with optimus) +alias swapout='sudo swapoff -a; sudo swapon -a' # Reset swap (move everything to RAM) +alias mount-ram='mount -t tmpfs tmpfs' # Mount RAM disk for fast filesystem +alias screenlock='xset s activate' # Use DPMS to trigger xss-lock and handle screen locking + +# System info +alias meminfo='free -m -l -t' +alias cpuinfo='lscpu' +alias batinfo='sudo watch -d -n 2 tlp-stat -b' +alias battery='cat /sys/class/power_supply/BAT0/capacity' +alias gpumeminfo='frep -i --color memory /var/log/Xorg.0.log' +alias journalerr='sudo journalctl -p 3 -xb' +alias distro='cat /etc/*-release' +alias diskspace_report="df -P -kHl" +alias kernel='uname -r' + +# System processes +alias psmem='ps auxf | sort -nr -k 4' # Top memory eaters +alias psmem10='psmem | head -10' # Top 10 memory eaters +alias pscpu='ps auxf | sort -nr -k 3' # Top cpu eaters +alias pscpu10='pscpu | head -10' # Top 10 cpu eaters +alias psg='ps aux | grep -v grep | grep -i -e VSZ -e' # Get searchable process with nice output + +# Time info +alias now='date +"%T"' +alias nowtime='now' +alias nowdate='date +"%d-%m-%Y"' +alias week='date +%V' + +# Cleanup +alias clean-trash='rm -rf ~/.local/share/Trash/* || echo "Trash already empty"' +alias clean-downloads='rm -rf ~/Downloads/* || echo "Downloads directory is already empty"' +alias clean-journal='journalctl --vacuum-size=200M || echo "You have to be root to clean journal"' +alias clean-pacman='pacman -Sc || echo "You have to be root to clean pacman cache"' +alias cleanup='clean-trash && clean-down && clean-journal && clean-pacman' + +# Git aliases +alias g='git' +alias gp='git push' +alias gpl='git pull' +alias gf='git fetch' +alias gs='git status --short --branch' +alias gss='git status' +alias ga='git add' +alias gap='git add --patch' +alias gc='git commit' +alias gcm='git commit --message' +alias gca="git commit --amned" +alias gb='git branch' +alias gch='git checkout' +alias gchb='git checkout -b' +alias gd='git diff' +alias gdc='git diff --cached' +alias gundo='git reset --soft HEAD~' +alias gredo="git reset 'HEAD@{1}'" + +# Youtube-dl aliases +alias ytv-best='youtube-dl -f bestvideo+bestaudio' +alias yta-best='youtube-dl --extract-audio --audio-format best' +alias yta-mp3='youtube-dl --extract-audio --audio-format mp3' +alias yta-wav='youtube-dl --extract-audio --audio-format wav' + +# Hyprland aliases +alias hyprlog-cur-path='echo "/tmp/hypr/$(exa --sort created --group-directories-first --reverse --oneline --color never --no-icons /tmp/hypr | head -n 1)/hyprland.log"' +alias hyprlog-last-path='echo "/tmp/hypr/$(exa --sort created --group-directories-first --reverse --oneline --color never --no-icons /tmp/hypr | head -n 2 | tail -n 1)/hyprland.log"' +alias hyprlog-cur='bat $(hyprlog-cur-path)' +alias hyprlog-last='bat $(hyprlog-last-path)' + +# Terminal vim-like exits, in case I think the terminal is vim +alias :q='exit' +alias :q!='exit' +alias :wq='exit' +alias :wq!='exit' + +# Shell aliases +alias reload="exec \$SHELL" # Reload the shell (i.e. invoke as a login shell) +alias path='echo -e ${PATH//:/\\n}' # Print each PATH entry on a separate line +alias unsudo='sudo -k' # Reset sudo timeout (sudo will require password) +alias vimwiki='vim -c VimwikiIndex' # Open vimwiki index +alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' +alias tty-clock='tty-clock -Ssc' # Terminal clock screensaver +alias rick='curl -s -L https://raw.githubusercontent.com/ItsDrike/rickrollrc/master/roll.sh| bash' # Terminal rickroll +alias nf='neofetch' +alias hist='fc -lt "$HISTTIMEFORMAT" 1' +alias sudovim='sudoedit' +alias cls='clear' +alias wh='which' + +# Kitty terminal kitten shorthands +if [ "$TERM" = "xterm=kitty" ]; then + alias hls='\ls --hyperlink=auto' + alias hg='kitty +kitten hyperlinked_grep' + alias transfer='kitty +kitten transfer' + alias icat='kitty +kitten icat' +fi + +# If user is not root, pass all commands via sudo/doas +if [ "$(id -u)" -ne 0 ]; then + # Enable aliases to be sudoed/doased + # with doas having precedence over sudo if found + + ## Uncomment if you are using autocompletion (is ZSH) + #command -v /usr/bin/sudo > /dev/null && alias doas='nocorrect sudo ' && alias sudo='nocorrect sudo ' + #command -v /usr/bin/doas > /dev/null && alias doas='nocorrect doas ' && alias sudo='nocorrect doas ' + + ## if the above is uncommented, comment this + command -v /usr/bin/sudo > /dev/null && alias doas='sudo ' && alias sudo='sudo ' + command -v /usr/bin/doas > /dev/null && alias doas='doas ' && alias sudo='doas ' +fi + +# XDG Base Directory fixes +alias nvidia-settings='nvidia-settings --config=$XDG_CONFIG_HOME/nvidia/settings' + +# enable color support +if [ -x /usr/bin/dircolors ]; then + (test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)") || eval "$(dircolors -b)" + alias dir='dir --color=auto' + alias vdir='vdir --color=auto' + + alias grep='grep --color=auto' + alias cgrep='grep --color=always' + alias fgrep='fgrep --color=auto' + alias egrep='egrep --color=auto' + + alias diff='diff --color=auto' + alias ip='ip --color=auto' + + # Take advantage of $LS_COLORS for completion as well + zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}" +fi diff --git a/home/programs/terminal/shell/zsh/rc/completion.zsh b/home/programs/terminal/shell/zsh/rc/completion.zsh new file mode 100644 index 0000000..d379bd6 --- /dev/null +++ b/home/programs/terminal/shell/zsh/rc/completion.zsh @@ -0,0 +1,8 @@ +# Autocompletion behavior of ZSH + +autoload -Uz compinit +zmodload -i zsh/complist # load completion list +compinit -d $ZSH_COMPDUMP # Specify compdump file +zstyle ':completion:*' menu select # select completions with arrow keys +zstyle ':completion:*' group-name '' # group results by category +zstyle ':completion:::::' completer _expand _complete _ignored _approximate #enable approximate matches for completion diff --git a/home/programs/terminal/shell/zsh/rc/environment.zsh b/home/programs/terminal/shell/zsh/rc/environment.zsh new file mode 100644 index 0000000..1e3f158 --- /dev/null +++ b/home/programs/terminal/shell/zsh/rc/environment.zsh @@ -0,0 +1,98 @@ +# Environmental variable definitions. +# TODO: These really need to be moved to nix configuration, this is only +# temporary. + +# This file is only sourced once after login, unlike .zshrc/.bashrc +# +# NOTE: This file shouldn't be defined for root account. Sudo +# will not source it (and neither will it source .zshrc/.zprofile), +# which means the XDG definitions will be ignored anyway, and +# defining them may break programs when root is actually logged in. + +# Define some variables for POSIX compatibility +uid="$(id -u)" + +# Default programs +export EDITOR="nvim" +export BROWSER="firefox" +export TERMINAL="alacritty" +export DIFFPROG="nvim -d" +export FILEMANAGER="pcmanfm" + +# XDG Standard paths +export XDG_CONFIG_HOME="$HOME/.config" +export XDG_CACHE_HOME="$HOME/.cache" +export XDG_DATA_HOME="$HOME/.local/share" +export XDG_RUNTIME_DIR="/run/user/$uid" +export XDG_STATE_HOME="$HOME/.local/state" + +# Per-Application XDG settings +export ZDOTDIR="$XDG_CONFIG_HOME/zsh" +export ZSH_CACHE="$HOME/.cache/zsh" +export HISTFILE="$ZSH_CACHE/history" +export ZSH_COMPDUMP="$ZSH_CACHE/zcompdump-$ZSH_VERSION" +export XINITRC="$XDG_CONFIG_HOME/x11/xinitrc" +#export XAUTHORITY="$XDG_RUNTIME_DIR/Xauthority" # This line will break some DMs. +export LESSHISTFILE="-" +export GTK2_RC_FILES="$XDG_CONFIG_HOME/gtk-2.0/gtkrc" +export WGETRC="$XDG_CONFIG_HOME/wget/wgetrc" +export GNUPGHOME="$XDG_DATA_HOME/gnupg" +export GOPATH="$XDG_DATA_HOME/go" +export CARGO_HOME="$XDG_DATA_HOME/cargo" +export RUSTUP_HOME="$XDG_DATA_HOME/rustup" +export GRADLE_USER_HOME="$XDG_DATA_HOME/gradle" +export _JAVA_OPTIONS="-Djava.util.prefs.userRoot=$XDG_CONFIG_HOME/java" +export NPM_CONFIG_USERCONFIG="$XDG_CONFIG_HOME/npm/npmrc" +export NPM_CONFIG_CACHE="$XDG_CACHE_HOME/npm" +export NPM_CONFIG_PREFIX="$XDG_DATA_HOME/npm" +export NUGET_PACKAGES="$XDG_CACHE_HOME/NuGetPackages" +export PARALLEL_HOME="$XDG_CONFIG_HOME/parallel" +export RANDFILE="$XDG_CACHE_HOME/rnd" +export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/pythonrc.py" +export PYTHONPYCACHEPREFIX="$XDG_CACHE_HOME/python" +export PYTHONUSERBASE="$XDG_DATA_HOME/python" +# Less commonly used applications +export _ZL_DATA="$XDG_DATA_HOME/zlua" +export SQLITE_HISTORY="$XDG_DATA_HOME/sqlite_history" +export WAKATIME_HOME="$XDG_CONFIG_HOME/wakatime" +export IPYTHONDIR="$XDG_CONFIG_HOME/ipython" +export PYENV_ROOT="$XDG_DATA_HOME/pyenv" +export MYPY_CACHE_DIR="$XDG_CACHE_HOME/mypy" +export JUPYTER_CONFIG_DIR="$XDG_CONFIG_HOME/jupyter" +#export PYLINTHOME="$XDG_CACHE_HOME/pylint" +#export DOCKER_CONFIG="$XDG_CONFIG_HOME/docker" +#export CUDA_CACHE_PATH="$XDG_CACHE_HOME/nv" + +# Colorful man pages! +# Use neovim to open man pages +# shellcheck disable=SC2155 +if command -v nvim >/dev/null; then + export MANPAGER="nvim +Man!" +# If neovim isn't installed, but bat is, use it as a manpager +# (has color support) +elif command -v bat >/dev/null; then + export MANPAGER="sh -c 'col -bx | bat -l man -p'" +# Export color definitions for less (default manpager) otherwise +else + export LESS_TERMCAP_mb="$(printf '%b' '[1;31m')" + export LESS_TERMCAP_md="$(printf '%b' '[1;36m')" + export LESS_TERMCAP_me="$(printf '%b' '[0m')" + export LESS_TERMCAP_so="$(printf '%b' '[01;44;33m')" + export LESS_TERMCAP_se="$(printf '%b' '[0m')" + export LESS_TERMCAP_us="$(printf '%b' '[1;32m')" + export LESS_TERMCAP_ue="$(printf '%b' '[0m')" +fi + +# Other program settings +export HISTTIMEFORMAT="%Y-%m-%d %T " +export SUDO_ASKPASS="$HOME/.local/bin/scripts/gui/prompt/menupass" +export PIPENV_VENV_IN_PROJECT=1 # Force pipenv to create new environments within projects ./.venv +export XSECURELOCK_SHOW_HOSTNAME=0 # Don't show hostname in xsecurelock +export XSECURELOCK_SHOW_DATETIME=1 # Show current date and time in xsecurelock +export QT_QPA_PLATFORMTHEME="qt5ct" # Have QT use theme from qt5ct. +#export QT_STYLE_OVERRIDE="gtk2" # Have QT use the gtk2 theme (needs aur/qt5-styleplugins) +#export QSG_RENDER_LOOP=basic # Fix Stremio freezing + +# Remove irrelevant variables added for posix compatibility +unset posix + diff --git a/home/programs/terminal/shell/zsh/rc/fallback_term.zsh b/home/programs/terminal/shell/zsh/rc/fallback_term.zsh new file mode 100644 index 0000000..7d2e757 --- /dev/null +++ b/home/programs/terminal/shell/zsh/rc/fallback_term.zsh @@ -0,0 +1,9 @@ +# If the set $TERM variable doesn't match any configured terminfo entries +# fall back to xterm. This fixes SSH connections from unknown terminals + +if [ -z "$TERM" ]; then + export TERM=xterm +elif ! infocmp "$TERM" &>/dev/null; then + export TERM=xterm + echo "TERM set to xterm due to missing terminfo entry." +end diff --git a/home/programs/terminal/shell/zsh/rc/functions.zsh b/home/programs/terminal/shell/zsh/rc/functions.zsh new file mode 100644 index 0000000..f1aa829 --- /dev/null +++ b/home/programs/terminal/shell/zsh/rc/functions.zsh @@ -0,0 +1,288 @@ +# Nix configuration doesn't support defining function, so do it here + +# Show application listening on given port +port() { + sudo netstat -pln | grep "$1" | awk '{print $NF}' +} + +# Create a new directory and enter it +mkd() { + # shellcheck disable=SC2164 + mkdir -p "$1" && cd "$1"; +} + +# `o` with no arguments opens the current directory, otherwise opens the given +# location +o() { + if [ $# -eq 0 ]; then + open .; + else + open "$@"; + fi; +} + +# Use bat for nicer git diffs +batdiff() { + git diff --name-only --diff-filter=d | xargs bat --diff +} + +# Determine size of a file or total size of a directory +dirsize() { + if du -b /dev/null > /dev/null 2>&1; then + arg=-sbh; + else + arg=-sh; + fi + if [[ -n "$*" ]]; then + \du $arg -- "$@"; + else + \du $arg .[^.]* ./*; + fi; +} + +randmac() { + sudo ip link set dev "$1" down + sudo macchanger -A "$1" + sudo ip link set dev "$1" up +} + +# Go to the root of a git tree +cdgit () { + if [ "$(git rev-parse --is-inside-work-tree > /dev/null 2>&1)" -eq 0 ]; then + while ! [ -d .git ]; do + cd .. + done + return 0 + else + echo "Not a git directory" + return 1 + fi +} + +# Create a data URL from a file +dataurl() { + mimeType="$(file -b --mime-type "$1")" + if echo "$mimeType" | grep -e "^text/.*$" >/dev/null; then + mimeType="${mimeType};charset=utf-8" + fi + echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')"; +} + +# `tre` is a shorthand for `tree` with hidden files and color enabled, ignoring +# the `.git` directory, listing directories first. The output gets piped into +# `less` with options to preserve color and line numbers, unless the output is +# small enough for one screen. +tre() { + tree -I '.git|node_modules|bower_components' --group-directories-first "$@" | less -FRNX; +} + + + +# Show all the names (CNs and SANs) listed in the SSL certificate +# for a given domain +getcertnames() { + if [ -z "${1}" ]; then + echo "ERROR: No domain specified."; + return 1; + fi; + + domain="${1}"; + echo "Testing ${domain}…"; + echo ""; # newline + + tmp=$(echo -e "GET / HTTP/1.0\nEOT" \ + | openssl s_client -connect "${domain}:443" -servername "${domain}" 2>&1); + + if [[ "${tmp}" = *"-----BEGIN CERTIFICATE-----"* ]]; then + certText=$(echo "${tmp}" \ + | openssl x509 -text -certopt "no_aux, no_header, no_issuer, no_pubkey, \ + no_serial, no_sigdump, no_signame, no_validity, no_version"); + echo "Common Name:"; + echo ""; # newline + echo "${certText}" | grep "Subject:" | sed -e "s/^.*CN=//" | sed -e "s/\/emailAddress=.*//"; + echo ""; # newline + echo "Subject Alternative Name(s):"; + echo ""; # newline + echo "${certText}" | grep -A 1 "Subject Alternative Name:" \ + | sed -e "2s/DNS://g" -e "s/ //g" | tr "," "\n" | tail -n +2; + return 0; + else + echo "ERROR: Certificate not found."; + return 1; + fi; +} + +# Compare original and gzipped file size +gz_compare() { + origsize=$(wc -c < "$1"); + gzipsize=$(gzip -c "$1" | wc -c); + ratio=$(echo "$gzipsize * 100 / $origsize" | bc -l); + printf "orig: %d bytes\n" "$origsize"; + printf "gzip: %d bytes (%2.2f%%)\n" "$gzipsize" "$ratio"; +} + +# Extract almost any archive +extract() { + if [ -z "$1" ]; then + # display usage if no parameters given + echo "Usage: extract ." + echo " extract [path/file_name_2.ext] [path/file_name_3.ext]" + return 1 + else + for n in "$@" + do + if [ -f "$n" ] ; then + case "${n%,}" in + *.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar) + tar xvf "$n" ;; + *.lzma) unlzma ./"$n" ;; + *.bz2) bunzip2 ./"$n" ;; + *.rar) unrar x -ad ./"$n" ;; + *.gz) gunzip ./"$n" ;; + *.zip) unzip ./"$n" ;; + *.z) uncompress ./"$n" ;; + *.7z|*.arj|*.cab|*.chm|*.deb|*.dmg|*.iso|*.lzh|*.msi|*.rpm|*.udf|*.wim|*.xar) + 7z x ./"$n" ;; + *.xz) unxz ./"$n" ;; + *.exe) cabextract ./"$n" ;; + *) + echo "extract: '$n' - unknown archive method" + return 1 + ;; + esac + else + echo "'$n' - file does not exist" + return 1 + fi + done +fi +} + +# Create a .tar.gz archive, using `zopfli`, `pigz` or `gzip` for compression +targz() { + # Combine given names spearated with spaces as the filename + tmpFile="${*%/}.tar" + + tar -cvf "${tmpFile}" "${@}" || return 1 + + size=$( + stat -f"%z" "${tmpFile}" 2> /dev/null; # macOS `stat` + stat -c"%s" "${tmpFile}" 2> /dev/null; # GNU `stat` + ); + + cmd=""; + if (( size < 52428800 )) && hash zopfli 2> /dev/null; then + # the .tar file is smaller than 50 MB and Zopfli is available; use it + cmd="zopfli"; + else + if hash pigz 2> /dev/null; then + cmd="pigz"; + else + cmd="gzip"; + fi; + fi; + + echo "Compressing .tar ($((size / 1000)) kB) using \`${cmd}\`…"; + "${cmd}" -v "${tmpFile}" || return 1; + [ -f "${tmpFile}" ] && rm "${tmpFile}"; + + zippedSize=$( + stat -f"%z" "${tmpFile}.gz" 2> /dev/null; # macOS `stat` + stat -c"%s" "${tmpFile}.gz" 2> /dev/null; # GNU `stat` + ); + + echo "${tmpFile}.gz ($((zippedSize / 1000)) kB) created successfully."; +} + +anonymize() { + # Reset the prompt on initial run to allow this script + # to be ran multiple times without user having to reload + # PS1 manually + # shellcheck source=/home/itsdrike/.config/shell/prompt + . "${XDG_CONFIG_DIR:-$HOME/.config}/shell/prompt" + + # Regular expression to match 0-255 numbers (color) + color_int_re='^(0+)?([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$' + + # Defaults + NAME="%n" + MACHINE="" + #NAME_COLOR="%F{047}" + NAME_COLOR="%F{172}" + #DIR_COLOR="%F{027}" + DIR_COLOR="%F{158}" + AT_COLOR="%F{004}" + MACHINE_COLOR="%F{070}" + + while [ $# -gt 0 ]; do + key=$1 + + case $key in + -n|--name) + NAME=$2 + shift + shift + ;; + -m|--machine) + MACHINE=$2 + shift + shift + ;; + -nc|--name-color) + if [[ $2 =~ $color_int_re ]]; then + NAME_COLOR="%F{$2}" + else + NAME_COLOR=$2 + fi + shift + shift + ;; + -dc|--dir-color) + if [[ $2 =~ $color_int_re ]]; then + DIR_COLOR="%F{$2}" + else + DIR_COLOR=$2 + fi + shift + shift + ;; + -mc|--machine-color) + if [[ $2 =~ $color_int_re ]]; then + MACHINE_COLOR="%F{$2}" + else + MACHINE_COLOR=$2 + fi + shift + shift + ;; + -ac|--at-color) + if [[ $2 =~ $color_int_re ]]; then + AT_COLOR="%F{$2}" + else + AT_COLOR=$2 + fi + shift + shift + ;; + *) + echo "Unrecognized argument: $key" + echo "Arguments: -n|--name, -m|--machine, -nc|--name-color, -dc|--dir-color, -mc|--machine-color, -ac|--at-color" + return 1 + ;; + esac + done + + + OLD_LINE="%F{047}%n%f %F{027}" + + [ -n "$NAME" ] && NEW_LINE="${NAME_COLOR}${NAME}" + [ -n "$NAME" ] && [ -n "$MACHINE" ] && NEW_LINE="${NEW_LINE}${AT_COLOR}@" + [ -n "$MACHINE" ] && NEW_LINE="${NEW_LINE}${MACHINE_COLOR}${MACHINE}" + [ -n "$NAME" ] || [ -n "$MACHINE" ] && NEW_LINE="${NEW_LINE}%f " + NEW_LINE="${NEW_LINE}${DIR_COLOR}" + + # Use new anonymized name, machine and colors in PS1 + PS1=${PS1/"$OLD_LINE"/"$NEW_LINE"} +} + + diff --git a/home/programs/terminal/shell/zsh/rc/keybinds.zsh b/home/programs/terminal/shell/zsh/rc/keybinds.zsh new file mode 100644 index 0000000..212cdad --- /dev/null +++ b/home/programs/terminal/shell/zsh/rc/keybinds.zsh @@ -0,0 +1,66 @@ +# Set various more or less standard zsh keybinds +# (these are mostly copied from oh-my-zsh) + +# shellcheck disable=SC2030,SC2031,SC2015 + +# Make sure that the terminal is in application mode when zle is active, since +# only then values from $terminfo are valid +if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then + function zle-line-init() { + echoti smkx + } + function zle-line-finish() { + echoti rmkx + } + zle -N zle-line-init + zle -N zle-line-finish +fi + +# Use emacs keybindings +bindkey -e + +# Start typing + [Up-Arrow] - fuzzy find history forward +if [ -n "${terminfo[kcuu1]}" ]; then + autoload -U up-line-or-beginning-search + zle -N up-line-or-beginning-search + bindkey "${terminfo[kcuu1]}" up-line-or-beginning-search +fi +# Start typing + [Down-Arrow] - fuzzy find history backward +if [ -n "${terminfo[kcud1]}" ]; then + autoload -U down-line-or-beginning-search + zle -N down-line-or-beginning-search + bindkey "${terminfo[kcud1]}" down-line-or-beginning-search +fi + +# [Home] - Go to beginning of line +[ -n "${terminfo[khome]}" ] && bindkey "${terminfo[khome]}" beginning-of-line || bindkey "^[[H" beginning-of-line +# [End] - Go to end of line +[ -n "${terminfo[kend]}" ] && bindkey "${terminfo[kend]}" end-of-line || bindkey "^[[F" end-of-line + +# [Shift-Tab] - move through the completion menu backwards +[ -n "${terminfo[kcbt]}" ] && bindkey "${terminfo[kcbt]}" reverse-menu-complete + +# [Backspace] - delete backward +bindkey '^?' backward-delete-char +# [Delete] - delete forward +[ -n "${terminfo[kdch1]}" ] && bindkey "${terminfo[kdch1]}" delete-char || bindkey "^[[3~" delete-char +# [Ctrl-Delete] - delete whole forward-word +bindkey '^[[3;5~' kill-word + +# [Ctrl-RightArrow] - move forward one word +bindkey '^[[1;5C' forward-word +# [Ctrl-LeftArrow] - move backward one word +bindkey '^[[1;5D' backward-word + +# [Ctrl-r] - Search backward incrementally for a specified string. The string may begin with ^ to anchor the search to the beginning of the line. +bindkey '^r' history-incremental-search-backward +# [PageUp] - Up a line of history +[ -n "${terminfo[kpp]}" ] && bindkey "${terminfo[kpp]}" up-line-or-history +# [PageDown] - Down a line of history +[ -n "${terminfo[knp]}" ] && bindkey "${terminfo[knp]}" down-line-or-history + +# [Space] - do history expansion on space +bindkey ' ' magic-space + +# [ctrl+space] Accept suggestion from zsh-autosuggestions plugin +bindkey '^ ' autosuggest-accept diff --git a/home/programs/terminal/shell/zsh/rc/opts.zsh b/home/programs/terminal/shell/zsh/rc/opts.zsh new file mode 100644 index 0000000..9d6ed8f --- /dev/null +++ b/home/programs/terminal/shell/zsh/rc/opts.zsh @@ -0,0 +1,90 @@ +# Set or unset various zsh options. +# You can read more about what options are available and what these do in +# the ZSH manual: +# +# Some of these are also controllable through nix configuration, however +# not all of them are, and I find it cleaner to have all of these groupped +# together in a single file, even if there are nix options for some of these +# I'm instead setting them manually here. + +######################### +# General/Other options # +######################### + +setopt AUTO_CD # cd by typing directory name if it's not a command +setopt AUTO_LIST # automatically list choices on ambiguous completion +setopt AUTO_MENU # automatically use menu completion +setopt MENU_COMPLETE # insert first match immediately on ambiguous completion +setopt ALWAYS_TO_END # move cursor to end if word had one match +setopt INTERACTIVE_COMMENTS # allow comments in interactive mode +setopt MAGIC_EQUAL_SUBST # enable filename expansion for arguments of form `x=expression` +setopt NOTIFY # report the status of background jobs immediately +setopt NUMERIC_GLOB_SORT # sort filenames numerically when it makes sense +setopt GLOB_DOTS # Match files starting with . without specifying it (cd ) + + +###################### +# Auto pushd options # +###################### + +setopt AUTO_PUSHD # Make cd push the old directory onto the directory stack +setopt PUSHD_IGNORE_DUPS # don't push multiple copies of the same directory +setopt PUSHD_TO_HOME # have pushd with no arguments act like `pushd $HOME` +setopt PUSHD_SILENT # do not print the directory stack + + +######################### +# History Configuration # +######################### + +# If the internal history needs to be trimmed to add the current command line, setting this +# option will cause the oldest history event that has a duplicate to be lost before losing a +# unique event from the list. You should be sure to set the value of HISTSIZE to a larger +# number than SAVEHIST in order to give you some room for the duplicated events, otherwise +# this option will behave just like HIST_IGNORE_ALL_DUPS once the history fills up with unique +# events. +setopt HIST_EXPIRE_DUPS_FIRST + +# When searching for history entries in the line editor, do not display duplicates of a line +# previously found, even if the duplicates are not contiguous. +setopt HIST_FIND_NO_DUPS + +# If a new command line being added to the history list duplicates an older one, the older +# command is removed from the list (even if it is not the previous event). +setopt HIST_IGNORE_ALL_DUPS + +# Remove command lines from the history list when the first character on the line is a space, +# or when one of the expanded aliases contains a leading space. Only normal aliases (not +# global or suffix aliases) have this behaviour. Note that the command lingers in the internal +# history until the next command is entered before it vanishes, allowing you to briefly reuse +# or edit the line. If you want to make it vanish right away without entering another command, +# type a space and press return. +setopt HIST_IGNORE_SPACE + +# When writing out the history file, older commands that duplicate newer ones are omitted. +setopt HIST_SAVE_NO_DUPS + +# This option works like APPEND_HISTORY except that new history lines are added to the $HISTFILE +# incrementally (as soon as they are entered), rather than waiting until the shell exits. +setopt INC_APPEND_HISTORY + +# When using history expansion (such as with sudo !!), on enter, first show the expanded command +# and only run it after confirmation (another enter press) +setopt HIST_VERIFY + +# Remove superfluous blanks from each command line being added to the history list +setopt HIST_REDUCE_BLANKS + +# When writing out the history file, by default zsh uses ad-hoc file locking to avoid known +# problems with locking on some operating systems. With this option, locking is done by means +# of the `fcntl` system call, where this method is available. This can improve performance on +# recent operating systems, and is better at avoiding history corruption when files are stored +# on NFS. +setopt HIST_FCNTL_LOCK + +# Save each command's beginning time (unix timestamp) and the duration (in seconds) to the +# history file. +setopt EXTENDED_HISTORY + +# beep in ZLE when a widget attempts to access a history entry which isn’t there +unsetopt HIST_BEEP diff --git a/home/programs/terminal/shell/zsh/rc/profile.zsh b/home/programs/terminal/shell/zsh/rc/profile.zsh new file mode 100644 index 0000000..e13ea2b --- /dev/null +++ b/home/programs/terminal/shell/zsh/rc/profile.zsh @@ -0,0 +1,20 @@ +# User .profile definition. +# This file is only sourced once, after login, Unlike +# .zshrc/.bashrc, which will run whenever a new terminal is opened. + +# Add all folders in ~/.local/bin into PATH +# Some window managers require this line to be in profile +# not in .zshenv +if [ -d "$HOME/.local/bin" ]; then + PATH+=":${$(find -L ~/.local/bin -type d | tr '\n' ':')%%:}" +fi + +# Start graphical session automatically on tty1 if Hyprland or startx is available +if [ "$(tty)" = "/dev/tty1" ] && [ "$UID" != 0 ]; then + if command -v Hyprland >/dev/null; then + ! pidof -s Hyprland >/dev/null 2>&1 && launch-hypr + elif command -v startx >/dev/null; then + ! pidof -s Xorg >/dev/null 2>&1 && exec startx "$XINITRC" + fi +fi + diff --git a/home/programs/terminal/shell/zsh/rc/prompt.zsh b/home/programs/terminal/shell/zsh/rc/prompt.zsh new file mode 100644 index 0000000..0aea665 --- /dev/null +++ b/home/programs/terminal/shell/zsh/rc/prompt.zsh @@ -0,0 +1,188 @@ +# I use a fully custom ZSH prompt. I didn't like any of the starship things. +# shellcheck disable=SC2155 + +# Configuration variables: + +# Once we are too deep in the filestructure, we can usually afford to shorten +# the whole working directory and only print something like ~/.../dir3/dir4/dir5 +# instead of ~/dir1/dir2/dir3/dir4/dir5. If this isn't desired, set this to 0 +USE_SHORTENED_WORKDIR=1 + +# Show how much time it took to run a command +CMD_TIME_SHOW=1 +# Minimum units to show the time precision, if +# we use "s" (seconds), and the output took 0s, +# we don't print the output at all to avoid clutter. +# Same goes for any other units, however with "ms" +# (miliseconds), this is very unlikely +# Valid options: ms/s/m/h/d +CMD_TIME_PRECISION="s" +# Minimum time in miliseconds, to print the time took, +# if the command takes less than this amount of miliseconds, +# don't bother printing the time took, this is nice if you +# don't need to see how long commands like 'echo' took +# Setting this to 0 will always print the time taken +CMD_TIME_MINIMUM=100 + +# hide EOL sign ('%') +export PROMPT_EOL_MARK="" + +# TTY (pure linux) terminal only has 8-bit color support +# (unless you change it in kernel), respect this and downgrade +# the color scheme accordingly (it won't look best, but it's +# still better than no colors) +if [ "$TERM" = "linux" ]; then + GREEN="%F{002}" + RED="%F{001}" + ORANGE="%F{003}" + BLUE="%F{004}" + LBLUE="%F{006}" + PURPLE="%F{005}" +else + GREEN="%F{047}" + RED="%F{196}" + ORANGE="%F{214}" + BLUE="%F{027}" + LBLUE="%F{075}" + PURPLE="%F{105}" +fi +RESET="%f" + +# Signals git status of CWD repository (if any) +git_prompt() { + ref=$(command git symbolic-ref HEAD 2> /dev/null) || ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0 + echo -n " $ORANGE${ref#refs/heads/}" + + if [ -n "$(git status --short 2>/dev/null)" ]; then + echo "$RED+" + fi +} + +# Adds @chroot or @ssh +foreign_prompt() { + if [ "$(awk '$5=="/" {print $1}'