Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: Keyboard shortcut for moving a window to a specific display

  1. #1
    Join Date
    May 2006
    Location
    Worcester, UK
    Beans
    41
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Keyboard shortcut for moving a window to a specific display

    I noticed that while Compiz has every keyboard shortcut imaginable for moving windows between desktops, there's no shortcut for moving a window to a specific display when using dual monitors. I thought I'd publish my own solution to this problem here.

    So far, I've found the best method is to use the command line automation tool: xdotool - http://manpages.ubuntu.com/manpages/...xdotool.1.html

    Here's what I did:
    1. Test out the xdotool in a new terminal window using this command
      Code:
      xdotool windowmove `xdotool getwindowfocus` 100 100
      This should move your terminal window 100 pixels in from the top left of the leftmost display.
    2. Find out the top-left coordinates of your rightmost display by using the Nvidia X Server Settings tool in Main menu > Administration or via the terminal:
      Code:
      nvidia-settings
    3. Go to X Server Display Configuration and click the rightmost Display in the layout section. Its position offset should be displayed in the position field. Make a note of these two numbers.
    4. Create a script to move your window, either to the leftmost screen or the rightmost screen, using this code or similar:
      move-window-to-display.sh
      Code:
      #!/bin/bash
      
      if [ $1 -eq 2 ]
      then
          POS="1680 95"
      else
          POS="0 0"
      fi
      
      /usr/bin/xdotool windowmove `/usr/bin/xdotool getwindowfocus` $POS
      
      exit 0
      Remember to change the $POS variable to the offset position of your rightmost display. The script accepts a single argument - either "1" or "2" - to denote the display on the left or right.
    5. Once you've saved the script, make it executable by typing
      Code:
      sudo chmod +x /home/username/scripts/move-window-to-display.sh
    6. Open the keyboard shortcuts preferences: Main menu > Preferences > Keyboard shortcuts, and add a new custom keyboard shortcut (using the add button at the bottom of the window).
    7. Call it "Move window to display 1", and enter the path to your script, eg.
      Code:
      /home/username/scripts/move-window-to-display.sh 1
      Remember, the number at the end is an argument that denotes the display (1 for left, 2 for right).
    8. Assign a keyboard shortcut to your custom shortcut.
    9. Repeat for the right display.

    Now when you press the keyboard shortcut, the active window should be moved to either the left or right display! Hope this helps someone.

  2. #2
    Join Date
    Nov 2009
    Beans
    4

    Re: Keyboard shortcut for moving a window to a specific display

    Thanks. I haven't tried this yet, but i sure will. I really like speeding up my workflow by using hotkeys and i usually have quite some windows open on two monitors and two desktops, so this will be quite useful.

  3. #3
    Join Date
    Feb 2010
    Beans
    5

    Re: Keyboard shortcut for moving a window to a specific display

    Thanks for the idea!
    I have mapped this to 'Windows Key + Right (or left) Arrow' and can move windows between monitors...
    Would like to extend the script, if I can determine the window position in the first monitor BEFORE the move and manipulate the final position in the second monitor.

    Eg, right now, a move will always result in the window going to 0,0 of the second monitor... but if I can determine the window position prior to move, I can move the window to the second monitor, keeping the same 'y' value (x changes, of course)..

    Went thro the functions available in xdotool, but looks like I cannot figure out a way to GET window pos before move.

  4. #4
    Join Date
    May 2006
    Location
    Worcester, UK
    Beans
    41
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Keyboard shortcut for moving a window to a specific display

    After a bit of digging here:

    http://ubuntuforums.org/showthread.php?t=760651

    and here:

    http://www.ruby-forum.com/topic/165740#728314

    it looks like there's a solution. Try this in your script to get the current active window's X pos (or Y pos, just change the "Absolute upper-left X" to "Y"):
    Code:
    x=$(xwininfo -id `xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| cut -d ' ' -f 5` | grep "Absolute upper-left X" | cut -d ' ' -f 7)
    I've also grabbed the display resolutions via xorg.conf, so here's a new script that implements these changes:
    Code:
    #!/bin/bash
    
    offx=$( grep metamodes /etc/X11/xorg.conf | cut -d '+' -f 4 | sed 's/[^0-9]*//g' )
    offy=$( grep metamodes /etc/X11/xorg.conf | cut -d '+' -f 5 | sed 's/[^0-9]*//g' )
    curx=$(xwininfo -id `xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| cut -d ' ' -f 5` | grep "Absolute upper-left X" | cut -d ' ' -f 7)
    cury=$(xwininfo -id `xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| cut -d ' ' -f 5` | grep "Absolute upper-left Y" | cut -d ' ' -f 7)
    
    if [ $curx -ge $offx ]
    then
        newx=$((curx-offx))
        newy=$((cury-offy))
    else
        newx=$((curx+offx))
        newy=$((cury+offy))
    fi
    
    /usr/bin/xdotool windowmove `/usr/bin/xdotool getwindowfocus` $newx $newy
    
    exit 0
    There's probably a better sed command to find the display resolutions (I'm definitely no sed guru - in fact, it's a tool I still find incredibly hard to use after years of wrangling with it) and when I use the script to move a window, repeated moves means the window slowly gets lower on each screen, so there's pixels being added to the newy value for some reason, but I can't work out why.

    Anyway, let me know if this works for you.

  5. #5
    Join Date
    May 2006
    Location
    Worcester, UK
    Beans
    41
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Keyboard shortcut for moving a window to a specific display

    Also realised that this script works for horizontal layouts, but if your monitors are place vertically next to each other, you'll need to edit the script so it checks for [ $cury -ge $offy ]. I'm sure there's a simple if statement that can work out if your display layout is horizontal or vertical, but I'm needing to get on with some work!

  6. #6
    Join Date
    Sep 2008
    Beans
    11

    Re: Keyboard shortcut for moving a window to a specific display

    For our friends on KDE 4: this is supported out of the box.

    Open up System Settings > Keyboard and Mouse > Global Keyboard Shortcuts

    In there, pick the KDE Component KWin and the shortcuts in question are "Window to Screen [01234567]." I guess it supports you having up to 8 monitors.

  7. #7
    Join Date
    Apr 2005
    Beans
    126
    Distro
    Kubuntu Breezy 5.10

    Re: Keyboard shortcut for moving a window to a specific display

    @scallopedllama Awesome. Thanks so much. This is just what I have been needing for so long. I am glad someone helped me find it.

  8. #8
    Join Date
    Nov 2008
    Beans
    25

    Re: Keyboard shortcut for moving a window to a specific display

    Quote Originally Posted by wayfarer_boy View Post
    Code:
    #!/bin/bash
    
    offx=$( grep metamodes /etc/X11/xorg.conf | cut -d '+' -f 4 | sed 's/[^0-9]*//g' )
    offy=$( grep metamodes /etc/X11/xorg.conf | cut -d '+' -f 5 | sed 's/[^0-9]*//g' )
    curx=$(xwininfo -id `xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| cut -d ' ' -f 5` | grep "Absolute upper-left X" | cut -d ' ' -f 7)
    cury=$(xwininfo -id `xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| cut -d ' ' -f 5` | grep "Absolute upper-left Y" | cut -d ' ' -f 7)
    
    if [ $curx -ge $offx ]
    then
        newx=$((curx-offx))
        newy=$((cury-offy))
    else
        newx=$((curx+offx))
        newy=$((cury+offy))
    fi
    
    /usr/bin/xdotool windowmove `/usr/bin/xdotool getwindowfocus` $newx $newy
    
    exit 0
    There's probably a better sed command to find the display resolutions (I'm definitely no sed guru - in fact, it's a tool I still find incredibly hard to use after years of wrangling with it) and when I use the script to move a window, repeated moves means the window slowly gets lower on each screen, so there's pixels being added to the newy value for some reason, but I can't work out why.

    Anyway, let me know if this works for you.
    Thanks wayfarer_boy! This works great! For hose who might have problems with the above script, make sure you remove these lines from your xorg.conf file:

    Code:
    # Removed Option "metamodes" ....
    they are automatically commented out by the nvidia tool but they hinder the script.

    Cheers!

  9. #9
    Join Date
    May 2010
    Beans
    46
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Keyboard shortcut for moving a window to a specific display

    Hi,

    I'm having some problems with the newer script:

    clockworkpc@clockworkpc-laptop ~/Documents/Scripts $ ./move_window_to_other_display.sh
    ./move_window_to_other_display.sh: line 8: [: too many arguments
    ./move_window_to_other_display.sh: line 13: 1920
    1920

    1440: syntax error in expression (error token is "1920 1440")
    usage: windowmove wid x y


    System Details:
    Using Nvidia
    Primary Monitor = 1440 * 900
    Secondary Monitor = 1440 * 900 (to the right of Primary Monitor)

    Effectively it's (0-1440)x900 and then (1441-2880)x900

    Any ideas?

    The original, simple script works beautifully though. Thank you so much for this gem.
    Last edited by clockworkpc; September 7th, 2010 at 02:54 AM.

  10. #10
    Join Date
    Jun 2007
    Beans
    69
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Keyboard shortcut for moving a window to a specific display

    I'll link an old post of mine that might have useful things for you guys (and others finding this thread) to dig through:

    http://ubuntuforums.org/showthread.php?t=815925

    I have a 3-monitor system with 3 identical side-by-side monitors, but I'm also using a splitter box that makes them all look like one 3-wide monitor to one DVI plug from my video card. IOW, my computer sees one [3x 1280x1024 =] 3840x1024 screen, so each monitor's top left coords are 0,0, 1280,0, and 2560,0. I have Outputs set in Compiz to tell it where these logical boundaries are so I can max and fullscreen as usual. I'm using a bunch of things in the script to allow me to hotkey between monitors while retaining maximized and fullscreen status. I hooked them to Alt+h and Alt+l, as I use Vim, and character left/right hotkeys make sense for moving windows left and right.
    Last edited by gfixler; November 21st, 2010 at 02:25 PM. Reason: typos

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •