XFCE - Send window to other monitor on keystroke

23,013

Solution 1

This was posted a while ago and I am sure you have gotten your answer already, but for those who haven't.

Run these commands

sudo apt-get install xdotool
sudo apt-get install wmctrl

Then download the bash script from the following link (credit to jc00ke) https://github.com/jc00ke/move-to-next-monitor

Personally, I have a directory in my root where I store all my personal scripts. However, where you download it is really up to you. Change it to have permissions so you can execute. For example, save the scripts as move-to-next-monitor.sh and then execute the following

chmod 755 move-to-next-monitor.sh
  1. settings manager -> keyboard -> application shortcuts
  2. Click Add
  3. Click Open and direct it to your script
  4. Assign a keyboard shortcut to it and voilà!

You now have a keyboard shortcut to switch a window from 1 screen to another. I am not sure how it works with more than 2 screens.

Solution 2

I have made some changes to the above script mentioned, originally authored by jc00ke.

  • Mine is set up for three monitors.
  • It maintains whether the window was maximized or not.
  • It is used to move the window left or right with the usage script-name -l and script-name -r respectively.
  • If you have two monitors you would use script-name -a.
  • I added a fix where Chromium apps when minimized are very small and would not maximize again on the new monitor.
    I corresponded with jc00ke. While this works great on Xfce he said he had issues with his script in Unity. Of course other desktop environments such as Unity would not need this script because such options are built in to the window manager.
    To use the script make it executable chmod +x script-name and install the following two programs, sudo apt install xdotool wmctrl.
#!/bin/bash
#
# Move the current window to the next monitor.
#
# Also works only on one X screen (which is the most common case).
#
# Props to
# http://icyrock.com/blog/2012/05/xubuntu-moving-windows-between-monitors/
#
# Unfortunately, both "xdotool getwindowgeometry --shell $window_id" and
# checking "-geometry" of "xwininfo -id $window_id" are not sufficient, as
# the first command does not respect panel/decoration offsets and the second
# will sometimes give a "-0-0" geometry. This is why we resort to "xwininfo".

screen_width=$(xdpyinfo | awk -F" |x" '/dimensions:/ { print $7 }')
screen_height=$(xdpyinfo | awk -F" |x" '/dimensions:/ { print $8 }')
window_id=$(xdotool getactivewindow)

case $1 in
    -l )
        display_width=$((screen_width / 3 * 2))
        ;;
    -r )
        display_width=$((screen_width / 3))
        ;;
    -a )
        display_width=$((screen_width / 2))
        ;;
esac

# Remember if it was maximized.
window_state=$(xprop -id $window_id _NET_WM_STATE | awk '{ print $3 }')

# Un-maximize current window so that we can move it
wmctrl -ir $window_id -b remove,maximized_vert,maximized_horz

# Read window position
x=$(xwininfo -id $window_id | awk '/Absolute upper-left X:/ { print $4 }')
y=$(xwininfo -id $window_id | awk '/Absolute upper-left Y:/ { print $4 }')

# Subtract any offsets caused by window decorations and panels
x_offset=$(xwininfo -id $window_id | awk '/Relative upper-left X:/ { print $4 }')
y_offset=$(xwininfo -id $window_id | awk '/Relative upper-left Y:/ { print $4 }')
x=$((x - x_offset))
y=$((y - y_offset))

# Fix Chromium app view issue of small un-maximized size
width=$(xdotool getwindowgeometry $window_id | awk -F" |x" '/Geometry:/ { print $4 }')
if [ "$width" -lt "150" ]; then
  display_width=$((display_width + 150))
fi

# Compute new X position
new_x=$((x + display_width))
# Compute new Y position
new_y=$((y + screen_height))

# If we would move off the right-most monitor, we set it to the left one.
# We also respect the window's width here: moving a window off more than half its width won't happen.
if [ $((new_x + width / 2)) -gt $screen_width ]; then
  new_x=$((new_x - screen_width))
fi

height=$(xdotool getwindowgeometry $window_id | awk -F" |x" '/Geometry:/ { print $5 }')
if [ $((new_y + height / 2)) -gt $screen_height ]; then
  new_y=$((new_y - screen_height))
fi

# Don't move off the left side.
if [ $new_x -lt 0 ]; then
  new_x=0
fi

# Don't move off the bottom
if [ $new_y -lt 0 ]; then
  new_y=0
fi

# Move the window
xdotool windowmove $window_id $new_x $new_y

# Maintain if window was maximized or not
if [[ "${window_state}" == _NET_WM_STATE_MAXIMIZED* ]]; then
    wmctrl -ir $window_id -b add,maximized_vert,maximized_horz
fi

Solution 3

I also created my own python script to move windows across monitors.

https://github.com/calandoa/movescreen

Usage:

movescreen.py <up|down|left|right>

Interesting features:

  • handle the 4 directions
  • handle some special cases like windows overlapping on several monitors
  • restore independently fullscreen, maximized horizontally and vertically states
  • debugging or adding new features easy with python.

Solution 4

An other alternative which does not rely on any "binary" dependency (like xdotool or wmctrl): https://github.com/AlexisBRENON/ewmh_m2m

  • You can install it with pip (no need to copy it manually, make it executable, etc.)
  • It handle multiple screens with different layouts (horizontal, vertical, mixed)
  • Keep window/screen ratio when moving between screens of different sizes
  • Restore maximized states (horizontal, vertical)

Kind.

Share:
23,013

Related videos on Youtube

hjamali52
Author by

hjamali52

In pursuit of knowledge

Updated on September 18, 2022

Comments

  • hjamali52
    hjamali52 almost 2 years

    I am running Xubuntu 11.10 with a dual monitor setup. I am looking to create a keystroke (maybe CTRL + ALT + SPACE which will allow me send a selected window to the next monitor.

    In GNOME there is a package called swapmonitor which is capable of sending the window to the other monitor. Calling this program with a keystroke achieves the same effect.

    How is this done in XFCE/Xubuntu?

    • xrfang
      xrfang almost 12 years
      Do you not have access to swapmonitor under Xubuntu? Or are you asking how to set a keyboard shortcut for it?
    • hjamali52
      hjamali52 almost 12 years
      It doesnt seem to work on XFCE. Although I would like to know what options are out there.
    • Keith
      Keith almost 12 years
      How do you have the multiple monitors set up? As a large virtual screen, or separate X displays? Are you using nvidia twinview?
    • hjamali52
      hjamali52 almost 12 years
      It is TwinView. I have an old Nvidia GeForce card with 2 Dell 19" monitors
  • thejoshwolfe
    thejoshwolfe about 8 years
    The linked script had this issue for me: github.com/jc00ke/bin/issues/1
  • Dharmin Shah
    Dharmin Shah over 7 years
    @thejoshwolfe I have added my version of the script that solves the maximize issue for me.
  • Dharmin Shah
    Dharmin Shah over 7 years
    @ether_joe I have added my version of the script that may benefit you given you have three monitors.
  • Sava B.
    Sava B. almost 6 years
    I am in xfce, and this thing is evil. It will only move the window form the left monitor to the right, and it put my desktop in a really weird state once.
  • brett
    brett over 4 years
    does this work with full-screen aplications like games?
  • Dharmin Shah
    Dharmin Shah over 4 years
    I do not game, but I just put YouTube in full-screen and then xfce4-terminal. Neither worked. However, it seems the Python script by calandoa listed here works with full-screen. Also, wmctrl has a remove full-screen option that could be added to my script above.
  • kennyB
    kennyB over 3 years
    This one doesn't maintain the same relative position on the screen - I have two 1080p monitors
  • Dharmin Shah
    Dharmin Shah over 3 years
    @kennyB Thank you for bringing this to my attention. I had updated this recently with something that did not work, and I did not realize it. I just logged into Xfce, and I disabled my third monitor. Now script-name -a is working just fine for me (two 1080p monitors). If the window is maximized it is preserved and the same if it is not maximized. Please let me know if it still does not work on your end. Thanks!
  • loic17
    loic17 about 2 years
    Github script works great for me - thanks!