Skip to content

Instantly share code, notes, and snippets.

@miblodelcarpio
Created April 26, 2024 15:35
Show Gist options
  • Save miblodelcarpio/4a99dc492445f28c9d0d29cd6e3581b1 to your computer and use it in GitHub Desktop.
Save miblodelcarpio/4a99dc492445f28c9d0d29cd6e3581b1 to your computer and use it in GitHub Desktop.
zsh script to run ranger in "single window" mode, opening only the tabs saved by "save_tabs_on_exit", and suppressing the "$PWD" if it wasn't one of them
#!/bin/zsh
RangerTabsFilePath="$XDG_DATA_HOME/ranger/tabs"
RangerTabCount=0
WouldOpenPWD=0
if [ -f $RangerTabsFilePath ]; then
# Split on newline from: https://unix.stackexchange.com/questions/29724/how-to-properly-collect-an-array-of-lines-in-zsh#29748
RangerTabs=("${(@f)$(tr -s '\n\0' '\0\n' < $RangerTabsFilePath)}")
RangerTabCount=${#RangerTabs[@]}
# https://zsh.sourceforge.io/Doc/Release/Arithmetic-Evaluation.html#Arithmetic-Evaluation
# https://unix.stackexchange.com/questions/411304/how-do-i-check-whether-a-zsh-array-contains-a-given-value#411331
# Reverse subscripting: https://zsh.sourceforge.io/Doc/Release/Parameters.html#Array-Parameters
if [[ ! -n "$RangerTabs[(r)$PWD]" ]]; then
WouldOpenPWD=1
fi
fi
if (( WouldOpenPWD == 1 )); then
# Tab shifting / closing workaround from @[email protected]
# https://github.com/ranger/ranger/issues/1590#issuecomment-705339499
/sbin/ranger --cmd="tab_shift $RangerTabCount" --cmd="tab_close"
else
/sbin/ranger
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment