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="" export ZINIT_HOME_DIR=""
AUTO_SAVE_WORKING_DIR=true WORKING_DIR_SAVE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/zsh/last-working-dir"
AUTO_LOAD_LAST_WORKING_DIR=false # make the dir for the file if needed
WORKING_DIR_SAVE_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/shell/last-dir.save" ! 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) # (I know this shouldn't go here, but I needed it to happen early)
# change cursor to beam by default # change cursor to beam by default
@ -79,6 +79,7 @@
# <<< # <<<
## Program improvements (ex: ls = ls -h) >>> ## Program improvements (ex: ls = ls -h) >>>
alias sudo="sudo " # allow using aliases in sudo commands
alias df='df -h' # Human-readable sizes alias df='df -h' # Human-readable sizes
alias free='free -m' # Show sizes in MB alias free='free -m' # Show sizes in MB
alias bc="bc -ql" # Make bc usable for fast math alias bc="bc -ql" # Make bc usable for fast math
@ -88,7 +89,6 @@
ls="ls -hN --color=auto --group-directories-first" \ ls="ls -hN --color=auto --group-directories-first" \
grep="grep --color=auto" \ grep="grep --color=auto" \
diff="diff --color=auto" \ diff="diff --color=auto" \
ccat="highlight --out-format=ansi" \
pacman="pacman --color=auto" \ pacman="pacman --color=auto" \
paru="paru --color=auto --sudoloop --newsonupgrade --pgpfetch --upgrademenu --bottomup --skipreview" \ paru="paru --color=auto --sudoloop --newsonupgrade --pgpfetch --upgrademenu --bottomup --skipreview" \
@ -228,36 +228,6 @@
fi 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 # Simple extraction script. Taken from manjaro's .bashrc
ex() { ex() {
if [ -f $1 ] ; then if [ -f $1 ] ; then
@ -503,7 +473,7 @@
[ -z "$HISTFILE" ] && HISTFILE="$HOME/.zsh_history" [ -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 correct # Auto correct mistakes
setopt extendedglob # Extended globbing. Allows using regular expressions with * 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 precmd mzc_termsupport_precmd
add-zsh-hook preexec mzc_termsupport_preexec 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 load_working_dir
cd - > /dev/null cd - > /dev/null
# <<< # <<<