diff --git a/.zshrc b/.zshrc index 57a8c2d..6a3a46b 100644 --- a/.zshrc +++ b/.zshrc @@ -24,7 +24,8 @@ export ZINIT_HOME_DIR="" - SAVE_WORKING_DIR=true + AUTO_SAVE_WORKING_DIR=true + AUTO_LOAD_LAST_WORKING_DIR=false WORKING_DIR_SAVE_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/shell/last-dir.save" # (I know this shouldn't go here, but I needed it to happen early) @@ -215,6 +216,36 @@ fi } + # Loads the last working directory (or the one you're in) + alias lwd=load_working_dir + load_working_dir() { + # load the directory (and do validation to make sure it's actually a directory) + if test -f "$WORKING_DIR_SAVE_FILE"; then + local PREVIOUS_WORKING_DIR="$(cat "$WORKING_DIR_SAVE_FILE")" + if test -d "$(cat "$WORKING_DIR_SAVE_FILE")"; then + cd "$PREVIOUS_WORKING_DIR" || error $LINENO + else + echo "Invalid saved working directory ($PREVIOUS_WORKING_DIR)" + fi + else + echo "Invalid working directory save file ($WORKING_DIR_SAVE_FILE)" + fi + } + + # Saves the CWD to a file + alias swd=save_working_dir + save_working_dir() { + $SAVE_WORKING_DIR && pwd > "$WORKING_DIR_SAVE_FILE" + } + + # automatically save and resume working directory >>> + $AUTO_LOAD_LAST_WORKING_DIR && load_working_dir + if $AUTO_SAVE_WORKING_DIR; then + autoload -U add-zsh-hook + add-zsh-hook chpwd save_working_dir + fi + # <<< + # Simple extraction script. Taken from manjaro's .bashrc ex() { if [ -f $1 ] ; then @@ -250,10 +281,11 @@ echo "$FILEPATH" | grep -o 'site.*' | sed 's+^site+https://blakenorth.net+g' } + # for sending error messages from functions and whatnot error() { ERROR_CODE="$?" >&2 echo "An error occurred within a function in the .zshrc on line number ${1}" - exit $ERROR_CODE + return $ERROR_CODE } # prcolors(): Display all colors in a few different ways >>> @@ -268,7 +300,7 @@ printf "Values 30..37 are \e[33mforeground colors\e[m\n" printf "Values 40..47 are \e[43mbackground colors\e[m\n" printf "Value 1 gives a \e[1mbold-faced look\e[m\n\n" - + # foreground colors for fgc in {30..37}; do # background colors @@ -278,7 +310,7 @@ vals="${fgc:+$fgc;}${bgc}" vals=${vals%%;} - + seq0="${vals:+\e[${vals}m}" printf " %-9s" "${seq0:-(default)}" printf " ${seq0}TEXT\e[m" @@ -374,6 +406,7 @@ ls --color=auto } + # believe it or not, zsh makes for a fine calculator calc() { echo $(($*)) } @@ -543,27 +576,6 @@ bindkey -M vicmd 'k' history-substring-search-up bindkey -M vicmd 'j' history-substring-search-down # <<< -# automatically save and resume working directory >>> -if $SAVE_WORKING_DIR; then - # load the directory (and do validation to make sure it's actually a directory) - if test -f "$WORKING_DIR_SAVE_FILE"; then - local PREVIOUS_WORKING_DIR="$(cat "$WORKING_DIR_SAVE_FILE")" - if test -d "$(cat "$WORKING_DIR_SAVE_FILE")"; then - cd "$PREVIOUS_WORKING_DIR" || error $LINENO - else - echo "Invalid saved working directory ($PREVIOUS_WORKING_DIR)" - fi - else - echo "Invalid working directory save file ($WORKING_DIR_SAVE_FILE)" - fi - - save_working_dir() { - $SAVE_WORKING_DIR && pwd > "$WORKING_DIR_SAVE_FILE" - } - autoload -U add-zsh-hook - add-zsh-hook chpwd save_working_dir -fi -# <<< # Colors and compinit >>> export LESS_TERMCAP_mb=$'\E[01;32m' export LESS_TERMCAP_md=$'\E[01;32m'