Skip to content

Instantly share code, notes, and snippets.

@trapier
Last active January 30, 2019 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trapier/9ed0a1557615c263a1aede095978b19d to your computer and use it in GitHub Desktop.
Save trapier/9ed0a1557615c263a1aede095978b19d to your computer and use it in GitHub Desktop.
Mark the most-recently-focused i3 window with _last
#!/usr/bin/env python3
# Mark the most-recently-focused i3 window with _last
#
# Add this to your i3 config:
# exec_always --no-startup-id /path/to/script
# bindsym <your binding here> [con_mark=_last] focus
#
# Inspired by:
# - https://www.reddit.com/r/i3wm/comments/4d4luc/how_to_focus_the_last_window/
# + without the CPU overhead
# + without stomping on other marks that might be in use
# - https://github.com/acrisci/i3ipc-python/blob/master/examples/focus-last.py
# + without the socket
#
# Written in 2018 by Trapier Marshall
#
# To the extent possible under law, the author(s) have dedicated all copyright
# and related and neighboring rights to this software to the public domain
# worldwide. This software is distributed without any warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication along
# with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
import i3ipc
class LastMarker:
def __init__(self):
self.lastid = ''
self.i3 = i3ipc.Connection()
self.i3.on('window::focus', self.on_window_focus)
def on_window_focus(self, i3, event):
tree = i3.get_tree()
for c in tree.find_marked('_last'):
c.command('mark --toggle _last')
last=tree.find_by_id(self.lastid)
if last:
last.command('mark --add _last')
self.lastid = event.container.id
def run(self):
self.lastid = self.i3.get_tree().find_focused().id
self.i3.main()
if __name__ == '__main__':
last_marker = LastMarker()
last_marker.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment