Ubuntu: Move active window to another monitor

If your like me, I have have 2 monitors attached to my Ubuntu workstation.  When using Windows you could always get programs like Multimon or Ultramon that allowed with the click of one button to move the currently active window to the other monitor and vice versa, but there has never been anything like that available for Linux.  Until now.  By utilizing a xdotool and a small bash script and keyboard shortcuts, you can accomplish the exact same thing as those programs do on Windows within your Linux desktop.  I will provide the directions on how I accomplished this on my system.

Step 1:

Check for xdo and install if required.  To check if you have it from a console or shell type:
xdotool windowmove `xdotool getwindowfocus` 100 100

The shell window should move a bit. If it does not then you need to type:
sudo apt-get install xdotool

Step 2:

Locate your screen information using the Nvidia Display Server settings.  From the terminal type:
nvidia-settings

Click on the right most display and then look at the position data on the right next to the Position section.  It will say something like +1680+0
You want to note those numbers.

Step 3:
Fire up your favorite text editor and make the bash script for movement.  Use the following code:

#!/bin/bash

if [ $1 -eq 2 ]
then
POS=”0 0″
else
POS=”1680 0″
fi

/usr/bin/xdotool windowmove `/usr/bin/xdotool getwindowfocus` $POS

exit 0

Within the second POS field enter the 2 numbers that you made a note of from the position section. In my case it was 1680 0

Enter exactly as 1680 0 without the + symbols

Save the file as a .sh
Make sure you make a note of what you call the file. I called mine movewindow.sh

Step 4:
Make the file executable. Type the following into your shell or terminal window.

sudo chmod +x /home/username/scripts/movewindow.sh
Use whatever the name of the file you saved

Step 5:

Create a keyboard shortcut for the bash file to move the window left and right. Go to System tab, preferences, then Keyboard shortcuts.
Click the add button at the bottom.
Make the name: Move window right
Make the command: /home/userloginname/movewindow.sh 1
The path is going to be the path to where you saved the file.  After the name of the file you want to put a number 1 at the end denoting that its the 1 monitor.
Press ok.

Click the add button at the bottom again and call it: Move window left
Make the command: /home/userloginname/movewindow.sh 2
The path is going to be the path to where you saved the file.  After the name of the file you want to put a number 2 at the end denoting that its the 2 monitor.
Press ok

Once both of those are created you can assign the keyboard command to be which ever combination you like. I used the window key and right and left arrow.

Now you can easily move the windows around your desktop.

This article has 10 Comments

  1. Great example!

    I extended it a bit to have “move to other monitor” functionality so that you don’t have to decide between left and right when you only have two monitors.

    It seems that that xwininfo gives you the absolute position of the window excluding the title bar and window border. On my gnome configuration this meant it was off by 1 horizontally and off by 27 vertically. To compensate for this (because I’m too lazy to figure out how to determine these values programmatically, please let me know if you know how!) I made two configuration variables $title_width and $title_height that you should modify as necessary.

    You can determine these values by doing this:

    $ xwininfo -id `xdotool getwindowfocus` | grep “Absolute”
    Absolute upper-left X: 53
    Absolute upper-left Y: 78
    $ xdotool windowmove `xdotool getwindowfocus` 53 78
    $ xwininfo -id `xdotool getwindowfocus` | grep “Absolute”
    Absolute upper-left X: 54
    Absolute upper-left Y: 105

    title_width should then be set to 54 – 53 == 1
    tilt_height should be set to 105 – 78 = 27

    Also, set $next_monitor_x to the first monitor’s width as described in the original post.

    Here is the script:

    #!/bin/bash

    title_height=27
    title_width=1
    next_monitor_x=1680

    window=`xdotool getwindowfocus`
    x=`xwininfo -id $window | grep “Absolute upper-left X” | awk ‘{print $4}’`
    y=`xwininfo -id $window | grep “Absolute upper-left Y” | awk ‘{print $4}’`

    if [ “$x” -ge $next_monitor_x ]
    then
    new_x=$(($x-$next_monitor_x))
    else
    new_x=$(($x+$next_monitor_x))
    fi

    xdotool windowmove $window $(($new_x-$title_width)) $(($y-$title_height))

  2. You can also do this with Compiz:
    1. Activate “Put” in the “Window Management”-Section
    2. In there, define a Hotkey for “Put To Next Output”
    … done 🙂

  3. Yay! I love you guys! This is something I sorely missed migrating from where I could use the tight little program, MultiMon.

    I tried using the xdotool and bash file, but my meager skills precluded it working. Enabling Put in Compiz did it, though. Thanks to you all for exploring the issue.

  4. I took this and made the following modification to support more than two monitors and not to hardcode any values. This assumes that the display have all the same resolution.

    1. use xdotool to query the “Desktop” Window to get the dimensions of the desktop
    2. use xdotool to query the Display size (using getdisplaygeometry — odd this isn’t in the man pages — I am on Ubuntu 13.04)
    3. support “left” and “right” parameters.

    https://gist.github.com/chrisvire/5571470

    1. Chris,
      Excellent comment. Thank you for adding it.

      Its interesting that I have not worked on this section for a while but it still seems to be helping people. That makes what I did worth it.

  5. Thank you so much for that script!
    Missing winsplit like crazy when in gnome, but this really did the trick for me 🙂

  6. I was in foreign countries as a dependent and then, active duty, 20 years. With work days of up to 18 hours, but usually 12 hours, and with much of it being at night, a 24 hour clock is so easy to use.

    80% of the world runs on “military time”. Scientists and computer networks all run on 24 hour time.

    So, it is just easier, in a lot of ways, even in the conversion of time for multiple locations in multiple time zones. Using the 24 hour clock has probably saved me a few months total of mis-calculations in my very busy 68 years!

Leave a Reply

Your email address will not be published. Required fields are marked *