I need id of window which is active or focused. I try to use xdotool command. There is command:

xdotool getactivewindow

the result is saved to window stack. I want to get widnow id from this window stack. Command

xdotool getactivewindow getwindowpid

does not satisfy me. I do not want to get window id by process id.


I think xdotool getactivewindow is what you want - did you try it?

It prints the window id (from the window stack) if there are no further xdotool subcommands on the command line.

In xdotool getactivewindow getwindowpid for example, getactivewindow puts the id on the window stack, and getwindowpid uses this id to query the PID. Note that running that command in the terminal will always return the ID of the terminal window, because it is active. In order to get the ID from another window try sleep 2s && xdotool getactivewindow and select the window of interest in the two seconds time span.

There is a complication when using xdotool with other tools for window handling:

While xdotool output uses decimal numbers for windwo ids, most other tools use hexadecimal numbers for output (they support both for input usually).

For example, if you find a window with xdotool getactivewindow, you will not find the result in the output of xwininfo -root -tree, that lists all windows. It needs to be converted to a hexadecimal number first:

$ xdotool getactivewindow                              
$ printf 0x%x 69206716                  
$ xwininfo -root -tree | grep 0x42002bc
           0x42002bc (has no name): ("konsole" "Konsole")  1154x781+0+0  +1289+498

Converting decimal to hexadecimal:

printf 0x%x 69206716

Converting hexadecimal to decimal:

printf %i 0x42002bc
  • I tried xdotool getactivewindow command and problem is that it does not print result in terminal. It save result to window stack. This command prints some number but It is not window id (e.g 35651588)
    – Mariusz
    Sep 9 '14 at 11:20
  • Why do you think 35651588 is not the window id? Sep 9 '14 at 11:20
  • Yes, I think that 35651588 is not the window id. You can see windows id by xwininfo command, e.g xwininfo -root -tree - this command prints all windows and windows have another types of numbering.
    – Mariusz
    Sep 9 '14 at 11:23
  • 1
    xwininfo shows the window id as hexadecimal numbers, and xdotool shows decimal numbers, but it's just the same. Sep 9 '14 at 11:24
  • you are right. 35651588=0x2200004. xwininfo -root -tree | grep 2200004 0x2200004 "user@host:~": ("gnome-terminal" "Gnome-terminal") 1680x974+0+26 +0+51
    – Mariusz
    Sep 9 '14 at 11:29

This command works for me

xdotool key --window "$(xdotool getactivewindow)" F5

xdotool version 3.20160805.1

  • 1
    xdotool getactivewindow key F5 should produce the same result Jul 9 '19 at 14:57

I know this is an old question. Maybe someone will find this answer useful.

xdotool selectwindow getmouselocation --shell

then click on the window your want the id for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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