Skip to main content
added 1446 characters in body
Source Link
slm
  • 379.8k
  • 127
  • 793
  • 897

xdotool

Rough idea but you could achieve what you want by creating a couple of commands using xdotool. Then you could run them accordingly when you have 1 or 2 monitors connected.

There's a pretty good example of how you could do this in this articled titled: Xubuntu – moving windows between monitors.

excerpt from section: Moving the active window to the other monitor (finally!)

Here’s what we need to do:

  • Find the active window
  • Get its maximized state and remember it
  • Remove maximization
  • Get its geometry
  • Calculate the new position
  • Move it
  • Maximize based on the previous state
  • Raise it

Here’s a script that does that:

wid=`xdotool getactivewindow`
max_state=`xprop -id $wid _NET_WM_STATE`
 
wmctrl -ir $wid -b remove,maximized_vert,maximized_horz
eval `xdotool getwindowgeometry --shell $wid`
 
new_x=1600
if [[ "$X" -ge "$new_x" ]]; then
  new_x=0
fi
 
xdotool windowmove $wid $new_x $Y
if [ -z "${max_state/*_NET_WM_STATE_MAXIMIZED_*/}" ]; then
  wmctrl -ir $wid -b add,maximized_vert,maximized_horz
fi
 
xdotool windowraise $wid

More interactive method

I also found another approach that also made use of xdotool but wrapped it in a shell script that you could then associate with a shortcut key. Using this method you could select a window so that it was raised and had focus and by hitting the shortcut key combination, would send the application to another window. The article is titled: Move Windows Between Monitors.

The method provides the following script, windowmove.sh:

#!/bin/bash
if [ $1 -eq 2 ]
then
POS1=`xrandr --current | head -2 | tail -1 | cut -d 'x' -f1 | cut -d ' ' -f3`
POS2=0
else
POS1=0
POS2=0
fi
/usr/bin/xdotool windowmove `/usr/bin/xdotool getwindowfocus` $POS1 $POS2
exit 0

POS1 calculates the width of your main screen by using the output of xrandr. If you find that the script can't move windows right, but it can move them left, then try replacing that line with POS1=1920, and replace 1920 with the width in pixels of your main monitor.

Then run the Keyboard Bindings applet:

$ gnome-keybinding-properties

NOTE: This is runable from different places on different distros via the GUI.

Create 2 keybindings using these 2 application launches:

  • binding #1's command: ./Scripts/windowmove.sh 1
  • binding #2's command: ./Scripts/windowmove.sh 2

Rough idea but you could achieve what you want by creating a couple of commands using xdotool. Then you could run them accordingly when you have 1 or 2 monitors connected.

There's a pretty good example of how you could do this in this articled titled: Xubuntu – moving windows between monitors.

excerpt from section: Moving the active window to the other monitor (finally!)

Here’s what we need to do:

  • Find the active window
  • Get its maximized state and remember it
  • Remove maximization
  • Get its geometry
  • Calculate the new position
  • Move it
  • Maximize based on the previous state
  • Raise it

Here’s a script that does that:

wid=`xdotool getactivewindow`
max_state=`xprop -id $wid _NET_WM_STATE`
 
wmctrl -ir $wid -b remove,maximized_vert,maximized_horz
eval `xdotool getwindowgeometry --shell $wid`
 
new_x=1600
if [[ "$X" -ge "$new_x" ]]; then
  new_x=0
fi
 
xdotool windowmove $wid $new_x $Y
if [ -z "${max_state/*_NET_WM_STATE_MAXIMIZED_*/}" ]; then
  wmctrl -ir $wid -b add,maximized_vert,maximized_horz
fi
 
xdotool windowraise $wid

xdotool

Rough idea but you could achieve what you want by creating a couple of commands using xdotool. Then you could run them accordingly when you have 1 or 2 monitors connected.

There's a pretty good example of how you could do this in this articled titled: Xubuntu – moving windows between monitors.

excerpt from section: Moving the active window to the other monitor (finally!)

Here’s what we need to do:

  • Find the active window
  • Get its maximized state and remember it
  • Remove maximization
  • Get its geometry
  • Calculate the new position
  • Move it
  • Maximize based on the previous state
  • Raise it

Here’s a script that does that:

wid=`xdotool getactivewindow`
max_state=`xprop -id $wid _NET_WM_STATE`
 
wmctrl -ir $wid -b remove,maximized_vert,maximized_horz
eval `xdotool getwindowgeometry --shell $wid`
 
new_x=1600
if [[ "$X" -ge "$new_x" ]]; then
  new_x=0
fi
 
xdotool windowmove $wid $new_x $Y
if [ -z "${max_state/*_NET_WM_STATE_MAXIMIZED_*/}" ]; then
  wmctrl -ir $wid -b add,maximized_vert,maximized_horz
fi
 
xdotool windowraise $wid

More interactive method

I also found another approach that also made use of xdotool but wrapped it in a shell script that you could then associate with a shortcut key. Using this method you could select a window so that it was raised and had focus and by hitting the shortcut key combination, would send the application to another window. The article is titled: Move Windows Between Monitors.

The method provides the following script, windowmove.sh:

#!/bin/bash
if [ $1 -eq 2 ]
then
POS1=`xrandr --current | head -2 | tail -1 | cut -d 'x' -f1 | cut -d ' ' -f3`
POS2=0
else
POS1=0
POS2=0
fi
/usr/bin/xdotool windowmove `/usr/bin/xdotool getwindowfocus` $POS1 $POS2
exit 0

POS1 calculates the width of your main screen by using the output of xrandr. If you find that the script can't move windows right, but it can move them left, then try replacing that line with POS1=1920, and replace 1920 with the width in pixels of your main monitor.

Then run the Keyboard Bindings applet:

$ gnome-keybinding-properties

NOTE: This is runable from different places on different distros via the GUI.

Create 2 keybindings using these 2 application launches:

  • binding #1's command: ./Scripts/windowmove.sh 1
  • binding #2's command: ./Scripts/windowmove.sh 2
Source Link
slm
  • 379.8k
  • 127
  • 793
  • 897

Rough idea but you could achieve what you want by creating a couple of commands using xdotool. Then you could run them accordingly when you have 1 or 2 monitors connected.

There's a pretty good example of how you could do this in this articled titled: Xubuntu – moving windows between monitors.

excerpt from section: Moving the active window to the other monitor (finally!)

Here’s what we need to do:

  • Find the active window
  • Get its maximized state and remember it
  • Remove maximization
  • Get its geometry
  • Calculate the new position
  • Move it
  • Maximize based on the previous state
  • Raise it

Here’s a script that does that:

wid=`xdotool getactivewindow`
max_state=`xprop -id $wid _NET_WM_STATE`
 
wmctrl -ir $wid -b remove,maximized_vert,maximized_horz
eval `xdotool getwindowgeometry --shell $wid`
 
new_x=1600
if [[ "$X" -ge "$new_x" ]]; then
  new_x=0
fi
 
xdotool windowmove $wid $new_x $Y
if [ -z "${max_state/*_NET_WM_STATE_MAXIMIZED_*/}" ]; then
  wmctrl -ir $wid -b add,maximized_vert,maximized_horz
fi
 
xdotool windowraise $wid