magbo system

i3 keeping mouse pointer after workspace switching

I use i3 window manager. If you switch to another workspace, mouse pointer will move to the center of the active window. It doesn’t matter for my work but sometimes (mainly on workspace where I have web browser) the centered mouse pointer obstructs the view and I’m forced give it away.

I created small utility which moves the mouse pointer to the last position for each workspace. It works with changing focus too.

Here is the part of my ~/.config/i3/config:

bindsym $mod+h exec "~/bin/ focus --due left"
bindsym $mod+j exec "~/bin/ focus --due down"
bindsym $mod+k exec "~/bin/ focus --due up"
bindsym $mod+l exec "~/bin/ focus --due right"

bindsym $mod+1 exec "~/bin/ workspace 1"
bindsym $mod+2 exec "~/bin/ workspace 2"
bindsym $mod+3 exec "~/bin/ workspace 3"
bindsym $mod+4 exec "~/bin/ workspace 4"
bindsym $mod+5 exec "~/bin/ workspace 5"
bindsym $mod+6 exec "~/bin/ workspace 6"
bindsym $mod+7 exec "~/bin/ workspace 7"
bindsym $mod+8 exec "~/bin/ workspace 8"
bindsym $mod+9 exec "~/bin/ workspace 9"
bindsym $mod+0 exec "~/bin/ workspace 10"

Here is the utility:


import click
import json
import logging
import os
import re
import subprocess

log = logging.getLogger(__name__)

def get_current_workspace():
    process =[
    ], check=True, stdout=subprocess.PIPE)

    return [workspace['name'] for workspace in json.loads(process.stdout) if workspace['focused']][0]  # noqa: E501

def get_current_window_id():
    ps = subprocess.Popen([
    ], stdout=subprocess.PIPE)

    process =[
        '.. | select(.focused? and .focused == true).id'
    ], check=True, stdin=ps.stdout, stdout=subprocess.PIPE)

    return process.stdout.decode('utf-8').strip()

def get_current_window_id2():
    process =[
    ], check=True, stdout=subprocess.PIPE)

    def find(element, results=[]):
        if 'focused' in element and element['focused']:
        if 'nodes' in element:
            for node in element['nodes']:
                find(node, results)
        return results

    return find(json.loads(process.stdout))[0]['id']

def get_current_mouse():
    process =[
    ], check=True, stdout=subprocess.PIPE)

    match = re.match('x:(\\d+) y:(\\d+) screen:(\\d+) window:(\\d+)', process.stdout.decode('utf-8'))  # noqa: E501
    return match[1], match[2]

def communicate(data: str):
    fifo = '/tmp/in-memory-storage'
    if not os.path.exists(fifo):
        return None

    with open(fifo, 'w') as f:
    with open(fifo, 'r') as f:
def cli():

@click.option('--due', type=click.Choice(['left', 'up', 'down', 'right']))
def focus(due):
    old = get_current_workspace()
    mouse_x, mouse_y = get_current_mouse()
    communicate(f'i3-workspace-{old}={mouse_x} {mouse_y}')[
    ], check=True)

    new = get_current_workspace()
    if old == new:

    match = re.match('(\\d+) (\\d+)', communicate(f'i3-workspace-{new}'))
    if match:
        window_id = get_current_window_id2()[
        ], check=True)[
            f'[con_id="{window_id}"] focus',
        ], check=True, stdout=subprocess.PIPE)

def workspace(name):
    mouse_x, mouse_y = get_current_mouse()
    communicate(f'i3-workspace-{get_current_workspace()}={mouse_x} {mouse_y}')[
    ], check=True)

    match = re.match('(\\d+) (\\d+)', communicate(f'i3-workspace-{name}'))
    if match:[
        ], check=True)

if __name__ == '__main__':

It will be published on my Github repo

Leave a Reply

Your email address will not be published. Required fields are marked *