How to move mouse cursor to the center of the window on i3 window manager

2016/03/31

Tweet

This document describes how to move your mouse cursor to the center of window automatically when you change window focus.

Demos

Let's see how it works.

How you can establish

I'm using Arch Linux, so the commands include ``pacman'' which is a sort of package manager. You can translate pacman commands into apt-get if you use Ubuntu family.

Here is my complete i3 configuration on Github.

Install some packages

You need to install some packages.

pacman -S xdotool xorg-xwininfo

WARNING: xdotool 3.20150503.1 has a bug which is critical for tiling window manager. I already sent the pull request, and it is merged, but it is not released yet. So, you may need to install git version of xdotool.

Put script

Here is my small script which move the mouse cursor after executing a i3 command. Alternatively, you can download from github.

You should put this to "~/.i3/move-cursor-window-center.sh" so you can version control entire i3 configuration.

#!/bin/sh
eval i3-msg $*
HERE=`xdotool getwindowfocus`

ULX=`xwininfo -id $HERE | grep "  Absolute upper-left X:" | awk '{print $4}'`
ULY=`xwininfo -id $HERE | grep "  Absolute upper-left Y:" | awk '{print $4}'`

# If there is no window, ULX == 1 and ULY == 1.
if [ $ULX != "-1" -o $ULY != "-1" ]; then
    eval `xdotool getwindowgeometry --shell $HERE`

    NX=`expr $WIDTH / 2`
    NY=`expr $HEIGHT / 2`

    xdotool mousemove --window $WINDOW $NX $NY
fi

The usage is following. Following command move your focus to the left window and move the cursor to the center of the window.

./move-cursor-window-center.sh focus left

Write .i3/config

You need to change raw i3 command to wrapped command with move-cursor-window-center.sh. For example, here is bind for $mod+j which move your focus to left.

bindsym $mod+j exec "~/.i3/move-cursor-window-center.sh focus left"

Here is a simplified version of my i3 configuration.

# change focus
bindsym $mod+u exec "~/.i3/move-cursor-window-center.sh workspace prev"
bindsym $mod+i exec "~/.i3/move-cursor-window-center.sh workspace next"

bindsym $mod+j         exec "~/.i3/move-cursor-window-center.sh focus left"
bindsym $mod+k         exec "~/.i3/move-cursor-window-center.sh focus down"
bindsym $mod+l         exec "~/.i3/move-cursor-window-center.sh focus up"
bindsym $mod+semicolon exec "~/.i3/move-cursor-window-center.sh focus right"
bindsym $mod+a         exec "~/.i3/move-cursor-window-center.sh focus parent"
bindsym $mod+Shift+j         exec "~/.i3/move-cursor-window-center.sh move left"
bindsym $mod+Shift+k         exec "~/.i3/move-cursor-window-center.sh move down"
bindsym $mod+Shift+l         exec "~/.i3/move-cursor-window-center.sh move up"
bindsym $mod+Shift+semicolon exec "~/.i3/move-cursor-window-center.sh move right"

# enter fullscreen mode for the focused container
bindsym $mod+f exec "~/.i3/move-cursor-window-center.sh fullscreen"

# switch to workspace
bindsym $mod+1 exec "~/.i3/move-cursor-window-center.sh workspace 1"
bindsym $mod+2 exec "~/.i3/move-cursor-window-center.sh workspace 2"
bindsym $mod+3 exec "~/.i3/move-cursor-window-center.sh workspace 3"
bindsym $mod+4 exec "~/.i3/move-cursor-window-center.sh workspace 4"
bindsym $mod+5 exec "~/.i3/move-cursor-window-center.sh workspace 5"
bindsym $mod+6 exec "~/.i3/move-cursor-window-center.sh workspace 6"
bindsym $mod+7 exec "~/.i3/move-cursor-window-center.sh workspace 7"
bindsym $mod+8 exec "~/.i3/move-cursor-window-center.sh workspace 8"
bindsym $mod+9 exec "~/.i3/move-cursor-window-center.sh workspace 9"
bindsym $mod+0 exec "~/.i3/move-cursor-window-center.sh workspace 10"

# move focused container to workspace
bindsym $mod+Shift+1 exec "~/.i3/move-cursor-window-center.sh move container to workspace 1"
bindsym $mod+Shift+2 exec "~/.i3/move-cursor-window-center.sh move container to workspace 2"
bindsym $mod+Shift+3 exec "~/.i3/move-cursor-window-center.sh move container to workspace 3"
bindsym $mod+Shift+4 exec "~/.i3/move-cursor-window-center.sh move container to workspace 4"
bindsym $mod+Shift+5 exec "~/.i3/move-cursor-window-center.sh move container to workspace 5"
bindsym $mod+Shift+6 exec "~/.i3/move-cursor-window-center.sh move container to workspace 6"
bindsym $mod+Shift+7 exec "~/.i3/move-cursor-window-center.sh move container to workspace 7"
bindsym $mod+Shift+8 exec "~/.i3/move-cursor-window-center.sh move container to workspace 8"
bindsym $mod+Shift+9 exec "~/.i3/move-cursor-window-center.sh move container to workspace 9"
bindsym $mod+Shift+0 exec "~/.i3/move-cursor-window-center.sh move container to workspace 10"

Enjoy!