How can I disable Alt-Tab application switching in Cinnamon when a certain program is open?

I use emacs and have fill commands set to Alt-Tab in certain modes and, as a creature of habit, would like to keep it that way.

Is there an easy way to disable application switching via Alt-Tab when emacs is open?


Solution 1:

I have a solution. All what you need is, to start this watcher script:

#!/bin/bash

keySwitchApplication="switch-applications"
keySwitchApplicationBackward="switch-applications-backward"

backupSwitchApplications="$(gsettings get org.gnome.desktop.wm.keybindings "$keySwitchApplication")"
disableSwitchApplications="$(gsettings get org.gnome.desktop.wm.keybindings "$keySwitchApplication" | sed "s/\,*\s*'<Alt>Tab'//")"

backupSwitchApplicationsBackward="$(gsettings get org.gnome.desktop.wm.keybindings "$keySwitchApplicationBackward")"
disableSwitchApplicationsBackward="$(gsettings get org.gnome.desktop.wm.keybindings "$keySwitchApplicationBackward" | sed "s/\,*\s*'<Shift><Alt>Tab'//")"

disabled="0"

while true; do
  isActive=$(wmctrl -lx | awk -v search=$(printf 0x0%x $(xdotool getactivewindow)) -v wm_class="$wm_class" '{ if($1 ~ search && $3 ~ /emacs/) print $3 }')

  if [[ "$isActive" != "" ]]; then
    # echo "active"
    if [[ "$disabled" == "0" ]]; then
      # echo "disable shortcut"
      gsettings set org.gnome.desktop.wm.keybindings "$keySwitchApplication" "$disableSwitchApplications"
      gsettings set org.gnome.desktop.wm.keybindings "$keySwitchApplicationBackward" "$disableSwitchApplicationsBackward"
      disabled="1";
    fi
  else
    # echo "not active"
    if [[ "$disabled" == "1" ]]; then
      # echo "enable shortcut"
      gsettings set org.gnome.desktop.wm.keybindings "$keySwitchApplication" "$backupSwitchApplications"
      gsettings set org.gnome.desktop.wm.keybindings "$keySwitchApplicationBackward" "$backupSwitchApplicationsBackward"
      disabled="0"
    fi;
  fi;
  sleep 1
done

The script checks in a endless loop the window class emacs and disables/enables Alt+Tab and Shift+Alt+Tab


If anything goes wrong, then you can reset the entry to the default settings:

gsettings reset org.gnome.desktop.wm.keybindings switch-applications
gsettings reset org.gnome.desktop.wm.keybindings switch-applications-backward

In my case:

% gsettings reset org.gnome.desktop.wm.keybindings switch-applications  
% gsettings get org.gnome.desktop.wm.keybindings switch-applications   
['<Super>Tab', '<Alt>Tab']

% gsettings reset org.gnome.desktop.wm.keybindings switch-applications-backward
% gsettings get org.gnome.desktop.wm.keybindings switch-applications-backward  
['<Shift><Super>Tab', '<Shift><Alt>Tab']

Credits

@Serg and his answer How to disable input language switching in terminal
@JacobVlijm for his comments

Solution 2:

One ugly hackish way comes to mind...

Keyboard setting should be stored in: ~/.config/dconf/user So if you have two files, one where Alt+Tab is system wide enabled, and the other where Alt+Tab is globally disabled, you could theoretically swap them.

Simple script which would:

  1. Backup Alt+Tab Enabled
  2. Copy Disabled Alt+Tab over Enabled Alt+Tab
  3. Run Emacs
  4. After Emacs is closed copy files back as they are supposed to

That script would be used to open/run Emacs

I have no idea if it would work, for safety I would try it in VB first and now I don't have time to try unfortunatelly...