zsh: better save/load working directory behavior

This commit is contained in:
PowerUser64 2021-10-17 01:20:04 -07:00
parent 15841febbb
commit a6070c8348

64
.zshrc
View file

@ -24,9 +24,9 @@
export ZINIT_HOME_DIR=""
AUTO_SAVE_WORKING_DIR=true
AUTO_LOAD_LAST_WORKING_DIR=false
WORKING_DIR_SAVE_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/shell/last-dir.save"
WORKING_DIR_SAVE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/zsh/last-working-dir"
# make the dir for the file if needed
! test -d "$(dirname "$WORKING_DIR_SAVE_FILE")" && mkdir -p "$(dirname "$WORKING_DIR_SAVE_FILE")"
# (I know this shouldn't go here, but I needed it to happen early)
# change cursor to beam by default
@ -79,6 +79,7 @@
# <<<
## Program improvements (ex: ls = ls -h) >>>
alias sudo="sudo " # allow using aliases in sudo commands
alias df='df -h' # Human-readable sizes
alias free='free -m' # Show sizes in MB
alias bc="bc -ql" # Make bc usable for fast math
@ -88,7 +89,6 @@
ls="ls -hN --color=auto --group-directories-first" \
grep="grep --color=auto" \
diff="diff --color=auto" \
ccat="highlight --out-format=ansi" \
pacman="pacman --color=auto" \
paru="paru --color=auto --sudoloop --newsonupgrade --pgpfetch --upgrademenu --bottomup --skipreview" \
@ -228,36 +228,6 @@
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
@ -503,7 +473,7 @@
[ -z "$HISTFILE" ] && HISTFILE="$HOME/.zsh_history"
## ZSH Options (opening this fold causes lag in vim for some reason) >>>
## ZSH Options >>>
{
# setopt correct # Auto correct mistakes
setopt extendedglob # Extended globbing. Allows using regular expressions with *
@ -704,7 +674,29 @@ autoload -U add-zsh-hook
add-zsh-hook precmd mzc_termsupport_precmd
add-zsh-hook preexec mzc_termsupport_preexec
# <<<
# allow `cd -` on loading zsh >>>
# auto save and load working dir (allow `cd -` on start) >>>
# Load the last working directory
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
}
save_working_dir() {
pwd > "$WORKING_DIR_SAVE_FILE"
}
autoload -U add-zsh-hook
add-zsh-hook chpwd save_working_dir
load_working_dir
cd - > /dev/null
# <<<