17

I have 2 displays on my PC - an IDE is open fullscreen in one display and Firefox is open fullscreen in another display.

Since I mostly use the keyboard, it's annoying to have to grab the mouse to switch the focus to Firefox and back to the IDE all the time.

Is there a shortcut I could use to switch the focus to "largest window" on display 2 if focus is somewhere in display 1 and vice versa?

1
  • Now there is a shortcut built-in! Hurray!
    – Sherry869
    Feb 18, 2021 at 11:31

4 Answers 4

15

Today I got an upvote for this question, so I'm posting my solution I've been all the time using for more than a year and am quite happy with.

Step 1: make bash script (e.g. write it to ~/swap.sh and make it executable) to set focus to a window that is in the middle of the other display:

#!/bin/bash

getwindowat() {
    # move mouse to coordinates provided, get window id beneath it, move mouse back
    eval `xdotool mousemove $1 $2 getmouselocation --shell mousemove restore`
    echo $WINDOW
}

# get active app
active=`xdotool getactivewindow`
# get coordinates of an active app
eval `xdotool getwindowgeometry --shell $active`

# if left border of an app is less than display width
# (e.g. one display is 1920px wide, app has x = 200 - means it's 200px to the right from the left border of left monitor
# if it has x = 1920 or more, it's on the right window), it's on screen 0, and we need to focus to screen 1, otherwise to screen 0
(( $X >= $WIDTH )) && focustoscreen=0 || focustoscreen=1;

# get coordinates of the middle of the screen we want to switch
searchx=$[ ($WIDTH / 2) + $focustoscreen * $WIDTH ]
searchy=$[ $HEIGHT / 2 ]

# get window in that position
window=`getwindowat $searchx $searchy`
# activate it
xdotool windowactivate $window

Step 2: add a keyboard shortcut to call this script, I put mine to Super-Tab

Step 3: use shortcut to switch displays like a boss

5
  • This helps a lot however the mouse is not moved to the other screen which, it would be great if it does.
    – samarth
    Mar 7, 2017 at 14:05
  • 3
    @samarth You can achieve that by removing the mousemove restore from the eval, so that it is "eval `xdotool mousemove $1 $2 getmouselocation --shell`"
    – Fluffy
    Mar 8, 2017 at 9:57
  • I've tried step 1, then running sh swap.sh, I get error message: swap.sh: 17: swap.sh: 288: not found swap.sh: 20: swap.sh: Syntax error: "(" unexpected, creating a text file with = as title and nothing Any idea? Thanks!
    – Matifou
    Jul 13, 2017 at 18:05
  • @Matifou try bash swap.sh
    – Fluffy
    Oct 30, 2017 at 14:09
  • This is helpful; is there a way to detect when a display is rotated? I have my left monitor rotated (with xrandr --rotate) but xdotool does not use the rotation and I don't know how to detect. If the focus is in the rotated display, i need to use the keystroke twice instead of once.
    – GTK
    Mar 24, 2018 at 8:23
1

this repository may help you

https://github.com/Eitol/screen_focus_changer

You place the focus_changer.py left script in a fixed place (/ opt for example) And then add the keybinding / shortcut / hotkey in your settings

python3 /opt/focus_changer.py left # Focus to left

python3 /opt/focus_changer.py right # Focus to right

1
  • Thank you! This works nicely in Gnome 3.36 even using Wayland. Aug 18, 2020 at 20:15
0

You can use AltTab to switch between windows.

AltTab also remebers which two window you last switched between. If you switch to one window (navigate with the arrow keys), then switch back, just pressing AltTab will allow you to jump between them, without any further navigation.

1
  • 3
    I want to switch displays in dual-monitor setup, not windows. I have multiple applications open, and I don't want to press alt-tab a lot of times until I reach the one on the other display.
    – Fluffy
    Sep 24, 2014 at 11:20
0

If you like me googled "keyboard shortcut switch monitors linux" and found this link... the above answers are not the easiest answers, if you are on a GNOME-based environment.

The keyboard shortcut Shift + Super + 🠖 or Shift + Super + 🠔 will switch to the monitor in that direction from the currently focused program.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .