Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to write a little script to help manage my windows via keyboard shortcuts.

In order to do what I want, I need to be able to find the current location and size of the "active" (focused) window. By location, I mean the X,Y coordinates of the window on the desktop, and by size I mean the height and width of the window.

Is there a command line utility that can fetch this information?

share|improve this question
up vote 20 down vote accepted

You can get all the info for the current active window by using this command:

xwininfo -id $(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}')


xwininfo -id $(xdpyinfo | grep focus | grep -E -o 0x[0-9a-f]+)

Just parse the output for the co-ordinates you need. Here are some useful commands when interacting with windows:

  • xwininfo will give you the relative and absolute geometry.
  • xprop will tell you a lot of info, including if the window is active, but not it's geometry
  • xdpyinfo will get you the id of the active window too, but no extra info.
  • wmctrl will get you a list of information about windows, and allow you select the active window for modification, but not information.

For example to change the title of the active window to "New Name":

wmctrl -r :ACTIVE: -N "New Name"

Or to change the position of the active window:

wmctrl -r :ACTIVE: -e 0,20,20,200,400

Consider wmctrl if you need to just change something simple, like the window's geometry. Consider using the other scripts mentioned above only if you want to do other more complex things.

share|improve this answer
I'd just like to note that I get different results when running the second command. First one works as I'd expect. – TM. Jan 11 '11 at 2:27
I use Arch but it might help someone here in the future. The 1st one didn't work for me, 2nd one didn't provide correct dimensions maybe due to the WM. But the combination of the two worked. xwininfo -id $(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)" | grep -Eo "0x[0-9a-f]{2,}") – Sev Jan 19 '13 at 16:10
Thanks! But I agree with the first two comments. The second command, xwininfo -id $(xdpyinfo | grep focus | grep -E -o 0x[0-9a-f]+) shows me a child window (has no name) of my Terminal. When I just do xdpyinfo | grep focus I get focus: window 0x435b877, revert to Parent, and 0x435b877 is the id of the window returned by the first command (which does work for me). This is on Trusty 14.04.1 LTS – nealmcb Feb 2 '15 at 15:21

With "xdotool" you can easily interact with windows using keyboard or mouse events.

Example of sending a ctrl-c sequence to the active window:

xdotool getwindowfocus key ctrl-c # Sends CTRL-C to the current window.

If you really need the window position:

eval $(xdotool getmouselocation --shell); echo $X $Y

For more actions/options check the man page.

share|improve this answer
Interesting, but I just get Error: Invalid key sequence 'ctrl-c'. – l0b0 May 22 '13 at 14:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.