Skip to content

Instantly share code, notes, and snippets.

@rehhouari
Last active April 29, 2024 17:04
Show Gist options
  • Save rehhouari/a287b22b468fb295273b782297142bed to your computer and use it in GitHub Desktop.
Save rehhouari/a287b22b468fb295273b782297142bed to your computer and use it in GitHub Desktop.
Automatically lock Zellij when a certain command is executed
# Define an array of commands in ENV that will lock Zellij when ran
# export ZELLIJ_LOCK=("micro" "nano" "vim")
# Store the last command we ran, this is to be used in precmd() after it exists
command=''
# Run before a command is executed
function preexec() {
# Extract the command from the input string
local cmd=${1%% *}
command=$cmd
if [[ " ${ZELLIJ_LOCK[@]} " =~ " $cmd " ]]; then
zellij ac switch-mode locked
fi
}
# Run after a command exits
function precmd() {
if [[ " ${ZELLIJ_LOCK[@]} " =~ " $command " ]]; then
zellij ac switch-mode normal
fi
}
@rehhouari
Copy link
Author

Made it use env variable rather than local variable to allow more flexibility, for example defining ZELLIJ_LOCK inside another file like ~/.zshenv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment