How can I open a window on a different monitor?

24,768

Solution 1

No, you can't do that directly. In the X architecture, the two physical monitors you see are not discernible by applications. You can only work your way around this abstraction, by determining which areas of the visible workspace correspond to which monitor and then place windows at the correct offset.

Sensible toolkits support a -geometry option, which allows your application to request being placed at a specific position. If you don't have this option available, you can use wmctrl if you have an EWMH/NetWM compatible X Window Manager to move and resize already existing windows (see wmctrl (1), you need options -r and -e). I use devilspie to force placement of some windows and of course your window manager of choice may have its own facility to force window placement.

devilspie allows to apply certain actions on X windows and can use X window properties to discern different windows and applications. The key rule for you would be geometry "<width>x<height>+<xoffset>+<yoffset>".

Yet, you always need to do the calculations of when and where another physical monitor begins yourself. But this is not too hard and xrandr does tell you how and where it uses monitors in the form of "<width>x<height>+<xoffset>+<yoffset> on each line staring with an output name.

There is not the foolproof-it-will-just-work way, because implementations may (and do!) vary. wmctrl usually works on the window title to identify the target. devilspie can also refer to its class, yet, I am not aware of any toolkit that lets you specify a X windows class but not its geometry.

Of course you could always start two xephyr instances and make one fill your left screen and the other your right screen and then address screens via the DISPLAY environment variable, but this solution has other downsides.

In theory, the authority to govern window placement is the window manager. Consequently, if you want stuff your window manager can't to, it will be hacky and it's also the reason why there is no generic way to accomplish this.

Yet, if your concrete use case is your android emulator ... which is very different from asking for the option to specify SCREEN=n fooapp on the shell, then devilspie may be what you are looking for. Hint: I found it's best to identify applications by their window class.

Solution 2

This is a window manager specific solution. This is how to do it with kwin which is KDE's default window manager.

If you right click on the application Title Bar and select more settings -> special application settings.

Go to Window matching tab. Click on Detect Window Properties button. Then with mouse cursor click on application main window. It will select window class etc and also get window positioning info etc.

Then click on Size and Position tab. Click the Box Position and box Size and drop down box to right of both and set to remember.

Then click ok.

Next time you run the app it should be the same size and on same monitor.

I use this in KDE to open my uRxvt terminal on my left monitor on a 3-monitor setup and it works every time.

Good luck.

Solution 3

This depends on how your dual-monitors are setup.

If you are using XINERAMA to make both displays look like one to X, then you would use the -geometry option to ask applications to appear at a specific location. If you have the displays setup as different screens then you will use the DISPLAY to achieve this.

Give these a try:

$ DISPLAY=0.0 xterm &
$ DISPLAY=0.1 xterm &

If your monitors are screen 0 and 1, this should spawn an xterm on each monitor.

Solution 4

I did come across this method which makes use of xdotool, xprop, & wmctrl.

Here’s what the below script will do:

  1. Find the active window
  2. Get its maximized state and remember it
  3. Remove maximization
  4. Get its geometry
  5. Calculate the new position
  6. Move it
  7. Maximize based on the previous state
  8. Raise it
  9. Here’s a script that does that:

script

This will move the active window to the other monitor. It can only handle a side by side, aka. horizontal, configuration of monitors.

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

Source: Xubuntu – moving windows between monitors

Share:
24,768

Related videos on Youtube

Lekensteyn
Author by

Lekensteyn

Arch Linux user, open-source enthusiast, programmer, Wireshark developer, TRU/e Security master student at TU/e. Interests: network protocols, Linux kernel, server administration, Android, breaking &amp; fixing stuff.

Updated on September 18, 2022

Comments

  • Lekensteyn
    Lekensteyn over 1 year

    With multiple monitors, I have so far been dragging windows manually around. Is there a way to make a window open on a specific screen in a dual-head setup? (Not to be confused with multiple X server displays.)

    Something like: SCREEN=2 firefox or open-in-screen 2 firefox.

    If it matters, my environment is KDE using the KWin window manager. KWin window rules can only match some properties like title, but I could not find an environment variable as filter.

    The secondary display extends the primary screen:

    $ xrandr -q
    Screen 0: minimum 320 x 200, current 3520 x 1080, maximum 32767 x 32767
    LVDS1 connected 1600x900+0+0 (normal left inverted right x axis y axis) 382mm x 214mm
       1600x900       60.1*+
       1024x768       60.0  
       800x600        60.3     56.2  
       640x480        59.9  
    VGA1 disconnected (normal left inverted right x axis y axis)
    HDMI1 connected 1920x1080+1600+0 (normal left inverted right x axis y axis) 477mm x 268mm
       1920x1080      60.0*    50.0     59.9  
       1920x1080i     60.1     50.0     60.0  
       1680x1050      59.9  
       1280x1024      75.0     60.0  
       1440x900       59.9  
       1280x960       60.0  
       1280x720       60.0     50.0     59.9  
       1024x768       75.1     70.1     60.0  
       832x624        74.6  
       800x600        72.2     75.0     60.3     56.2  
       720x576        50.0  
       720x480        60.0     59.9  
       640x480        75.0     72.8     66.7     60.0     59.9  
       720x400        70.1  
    DP1 disconnected (normal left inverted right x axis y axis)
    
    • Elias Probst
      Elias Probst over 10 years
      Usually, I'd suggest to use kstart for this, but it seems kstart only supports defining a specific desktop, but not a screen on which the application should be started. Probably worth a WISHLIST bugreport on bugs.kde.org.
    • Lekensteyn
      Lekensteyn over 10 years
      Great tip about kstart, I also wanted to know that. The command is: kstart --desktop $N firefox where $N is a number between 1 and the total number of virtual desktops.
    • Elias Probst
      Elias Probst over 10 years
      Regarding kstart, there's also a bugreport on bugs.kde.org asking for this feature.
    • Lekensteyn
      Lekensteyn over 10 years
      How odd, I saw a post about wmctrl earlier today but haven't had a chance to test it. Why was it deleted?
    • Elias Probst
      Elias Probst over 10 years
      No idea about the wmctrl post, didn't see it.
    • Elliptical view
      Elliptical view over 2 years
  • Lekensteyn
    Lekensteyn over 10 years
    I've tried this already, but this configuration does not apply to me, one screen is positioned left from another. I'll update my question.
  • casey
    casey over 10 years
    Have you tried using the geometry option to apps that support it? Giving an x coordinate > 1600 should position the app in the second monitors space. You might also try that outside of kwin (just to see if it works) as in my experience kwin and -geometry doesn't always work properly.
  • Lekensteyn
    Lekensteyn over 10 years
    Thanks for explaining why this is not an easy feature. KWin also has window rules that force placement on a different screen (which is what I use now). While there is no default feature for SCREEN=x app, is it possible to add a window property in this way so that it can be filtered by KWin?
  • Bananguin
    Bananguin over 10 years
    In theory yes, but I am not aware of any existing solution for this. I think you can't add properties but you can change existing properties, so you simply could add some string to the window class that kwin could detect.
  • Lekensteyn
    Lekensteyn over 10 years
    This is how I do it right now, I was hoping there is a more straightforward way using envvars.
  • Lekensteyn
    Lekensteyn over 10 years
    This only works after an application has been opened, not really what I am looking for (a keybinding could probably be added for this though). If I am not mistaken, this always positions a window to the left side of a monitor?
  • slm
    slm over 10 years
    @Lekensteyn - Yes the intent of this solution is you launch appX and then run this cmd to move it. I do not have dual monitors 8-( so I have no way to test this, I simply found it and provided the details here for you to evaluate. It is an option and could likely be adapted but it's a "raw" solution in its current form.
  • slm
    slm over 10 years
    @Lekensteyn - this solution is not that much different than your SCREEN=2 appX concept, except it's appX; sleep 1; moveWindow.sh.
  • slm
    slm over 10 years
    @Lekensteyn - also I've used devilspie, xdotool, etc. to do exactly the same things (within a single window), this A compliments the other one, in that it shows the specifics, where the other only explains the possible approaches but doesn't show actual implementation details.
  • Lekensteyn
    Lekensteyn over 10 years
    It will be more like app & sleep 1; moveWindow.sh as the programs do not detach. I do not consider this an improvement over using KWin window rules though. +1 for your efforts.
  • slm
    slm over 10 years
    @Lekensteyn - Yes, I wrote that off the top of my head. Thanks. I agree this is a giant hack, but a lead none the less. I seem to remember a feature in GNOME that allowed you to direct windows to various locations to, might of been in Compiz, but this isn't usable if you're using KDE.
  • Lekensteyn
    Lekensteyn over 10 years
    I awarded the bounty to you because SE would otherwise grant it to the top-voted answer while your answer better describes the issues. The current answers are still not satisfatory though.
  • Lekensteyn
    Lekensteyn about 10 years
    Right, but this does not work if I want to keep the cursor in the current screen. I think this is even the default option, but not all programs respect it.
  • iLikeDirt
    iLikeDirt over 5 years
    This is the correct answer for a KDE KWin user.
  • zwessels
    zwessels over 3 years
    Thank you so much for this one!