Skip to content

Instantly share code, notes, and snippets.

@marchrius
Created November 7, 2025 10:10
Show Gist options
  • Select an option

  • Save marchrius/324030eb49c18e33bc9f79fcd17f9669 to your computer and use it in GitHub Desktop.

Select an option

Save marchrius/324030eb49c18e33bc9f79fcd17f9669 to your computer and use it in GitHub Desktop.
Custom Alias for ZSH to change defaul terminal in GIO / XDG
# Custom Alias for ZSH to change defaul terminal in GIO / XDG
function __default_terminal() {
## Default values
TERMINAL_ARGS=new-window
TERMINAL=com.gexperts.Tilix
system=false
check_only=False
## Manage arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--check|-c)
check_only=true
;;
--system|-s)
system=true
;;
--user|-u)
system=false
;;
--terminal-args|-ta)
shift
TERMINAL_ARGS="$1"
;;
--terminal|-t)
shift
TERMINAL="$1"
;;
--help|-h)
echo "Usage: $0 [--system | --user] [--terminal TERMINAL] --terminal-args 'ARGS' [--help]"
echo ""
echo " --system | -s Modify system-wide xdg-terminal-exec files (requires sudo)"
echo " --user | -u Modify user-specific xdg-terminal-exec files (default)"
echo " --terminal | -t Specify the terminal to set as default (default: com.gexperts.Tilix)"
echo " --terminal-args | -ta 'ARGS' Specify additional arguments to pass to the terminal"
echo " --check | -c Check current default terminal settings"
echo " --help | -h Show this help message"
return 0
;;
esac
shift
done
LINE="$TERMINAL.desktop"
[[ -n "$TERMINAL_ARGS" ]] && LINE="$LINE:$TERMINAL_ARGS"
## Declare arrays
typeset -a USER_CONFIG_FILES SYSTEM_CONFIG_FILES
USER_CONFIG_FILES=(
$HOME/.config/ubuntu-xdg-terminals.list
$HOME/.config/gnome-xdg-terminals.list
$HOME/.config/xdg-terminals.list
$HOME/.local/share/flatpak/exports/share/xdg-terminal-exec/ubuntu-xdg-terminals.list
$HOME/.local/share/flatpak/exports/share/xdg-terminal-exec/gnome-xdg-terminals.list
$HOME/.local/share/flatpak/exports/share/xdg-terminal-exec/xdg-terminals.list
)
SYSTEM_CONFIG_FILES=(
/etc/xdg/ubuntu-xdg-terminals.list
/etc/xdg/gnome-xdg-terminals.list
/etc/xdg/xdg-terminals.list
/var/lib/flatpak/exports/share/xdg-terminal-exec/ubuntu-xdg-terminals.list
/var/lib/flatpak/exports/share/xdg-terminal-exec/gnome-xdg-terminals.list
/var/lib/flatpak/exports/share/xdg-terminal-exec/xdg-terminals.list
/usr/local/share/xdg-terminal-exec/ubuntu-xdg-terminals.list
/usr/local/share/xdg-terminal-exec/gnome-xdg-terminals.list
/usr/local/share/xdg-terminal-exec/xdg-terminals.list
/usr/share/xdg-terminal-exec/ubuntu-xdg-terminals.list
/usr/share/xdg-terminal-exec/gnome-xdg-terminals.list
/usr/share/xdg-terminal-exec/xdg-terminals.list
/var/lib/snapd/desktop/xdg-terminal-exec/ubuntu-xdg-terminals.list
/var/lib/snapd/desktop/xdg-terminal-exec/gnome-xdg-terminals.list
/var/lib/snapd/desktop/xdg-terminal-exec/xdg-terminals.list
)
if [[ "$system" == false ]]; then
for current_file in "${USER_CONFIG_FILES[@]}"; do
if [[ -f "$current_file" ]]; then
if [[ "$check_only" == true ]]; then
echo ">>>> Begin of $current_file <<<<"
current=$(<"$current_file")
echo -n "$current"
echo ""
echo ">>>> End of $current_file <<<<"
echo ""
continue
fi
sed -i "/$LINE/c\\" "$current_file"
current=$(<"$current_file")
{
echo "$LINE"
echo "$current"
} >| "$current_file"
fi
done
elif [[ "$system" == true ]]; then
for current_file in "${SYSTEM_CONFIG_FILES[@]}"; do
if [[ -f "$current_file" ]]; then
if [[ "$check_only" == true ]]; then
echo ">>>> Begin of $current_file <<<<"
current="$(sudo cat "$current_file")"
echo -n "$current"
echo ""
echo ">>>> End of $current_file <<<<"
echo ""
continue
fi
sudo sed -i "/$LINE/c\\" "$current_file"
current="$(sudo cat "$current_file")"
{
echo "$LINE"
echo "$current"
} | sudo tee "$current_file" > /dev/null
fi
done
fi
}
alias default_terminal=__default_terminal
alias dt=__default_terminal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment