Skip to content

Instantly share code, notes, and snippets.

@TauPan
Last active February 7, 2024 20:31
Show Gist options
  • Save TauPan/9c09bd9defc5ac3c9e06 to your computer and use it in GitHub Desktop.
Save TauPan/9c09bd9defc5ac3c9e06 to your computer and use it in GitHub Desktop.
My current qtile config
# -*- coding: utf-8 -*-
import distutils.spawn
import os
import re
import socket
import subprocess
import xcffib.xproto
from libqtile import layout, bar, widget, hook
from libqtile.command import lazy
from libqtile.config import Key, Screen, Group, Drag, Click
# copied from libqtile/resources/default_config.py and adapted by me
mod = "mod4"
def hostname():
return socket.gethostname()
gemini = (hostname == 'gemini')
laptop = os.path.exists('/sys/class/power_supply/BAT0/status')
# thanks to rogerduran for the implementation of my idea (borrowed
# from stumpwm)
class PrevFocus(object):
"""Store last focus per group and go back when called"""
def __init__(self):
self.focus = None
self.old_focus = None
self.groups_focus = {}
hook.subscribe.client_focus(self.on_focus)
def on_focus(self, window):
group = window.group
# only store focus if the group is set
if not group:
return
group_focus = self.groups_focus.setdefault(group.name, {
"current": None, "prev": None
})
# don't change prev if the current focus is the same as before
if group_focus["current"] == window:
return
group_focus["prev"] = group_focus["current"]
group_focus["current"] = window
def __call__(self, qtile):
group = qtile.current_group
group_focus = self.groups_focus.get(group.name, {"prev": None})
prev = group_focus["prev"]
if prev and group.name == prev.group.name:
group.focus(prev, False)
# taken from
# https://github.com/qtile/qtile-examples/blob/master/roger/config.py#L34
# and adapted
def pull_window_group_here(**kwargs):
"""Switch to the *next* window matched by match_window_re with the given
**kwargs
If you have multiple windows matching the args, switch_to will
cycle through them.
(Those semantics are similar to the fvwm Next commands with
patterns)
"""
def callback(qtile):
windows = windows_matching_shuffle(qtile, **kwargs)
if windows:
window = windows[0]
qtile.current_screen.set_group(window.group)
window.group.focus(window, False)
window.focus(window, False)
return lazy.function(callback)
def window_switch_to_screen_or_pull_group(**kwargs):
"""If the group of the window matched by match_window_re with the
given **kwargs is in a visible on another screen, switch to the
screen, otherwise pull the group to the current screen
"""
def callback(qtile):
windows = windows_matching_shuffle(qtile, **kwargs)
if windows:
window = windows[0]
if window.group != qtile.current_group:
if window.group.screen:
qtile.cmd_to_screen(window.group.screen.index)
qtile.current_screen.set_group(window.group)
window.group.focus(window, False)
return lazy.function(callback)
switch_window = window_switch_to_screen_or_pull_group
def make_sticky(qtile, *args):
window = qtile.current_window
screen = qtile.current_screen.index
window.static(
screen,
window.x,
window.y,
window.width,
window.height)
def pull_window_here(**kwargs):
"""pull the matched window to the current group and focus it
matching behaviour is the same as in switch_to
"""
def callback(qtile):
windows = windows_matching_shuffle(qtile, **kwargs)
if windows:
window = windows[0]
window.togroup(qtile.current_group.name)
qtile.current_group.focus(window, False)
return lazy.function(callback)
def windows_matching_shuffle(qtile, **kwargs):
"""return a list of windows matching window_match_re with **kwargs,
ordered so that the current Window (if it matches) comes last
"""
windows = sorted(
[
w
for w in qtile.windows_map.values()
if w.group and window_match_re(w, **kwargs)],
key=lambda ww: ww.window.wid)
idx = 0
if qtile.current_window is not None:
try:
idx = windows.index(qtile.current_window)
idx += 1
except ValueError:
pass
if idx >= len(windows):
idx = 0
return windows[idx:] + windows[:idx]
def window_match_re(window, wmname=None, wmclass=None, role=None):
"""
match windows by name/title, class or role, by regular expressions
Multiple conditions will be OR'ed together
"""
if not (wmname or wmclass or role):
raise TypeError(
"at least one of name, wmclass or role must be specified"
)
ret = False
if wmname:
ret = ret or re.match(wmname, window.name)
try:
if wmclass:
cls = window.window.get_wm_class()
if cls:
for v in cls:
ret = ret or re.match(wmclass, v)
if role:
rol = window.window.get_wm_window_role()
if rol:
ret = ret or re.match(role, rol)
except (xcffib.xproto.WindowError, xcffib.xproto.AccessError):
return False
return ret
def modifier_window_commands(match, spawn, *keys):
# Use switch_window by default (just mod)
# Use pull_window_here with additional ctrl
# spawn new window with additional shift
# Use pull_window_group_here with additional shift (mod, "shift", "control")
mapping = (
([mod], switch_window(**match)),
([mod, "control"], pull_window_here(**match)),
([mod, "shift"], lazy.spawn(spawn)),
([mod, "shift", "control"], pull_window_group_here(**match)))
return [Key(mods, key, command)
for mods, command in mapping
for key in keys]
TERM_PREFS = [
('st', 'st.*', '-e'),
('gnome-terminal', 'gnome-terminal-server', '--'),
# assuming urxvtd has been run from .xsession:
('urxvtc', 'URxvt', '-e'),
# Keybindings in shell are broken because of Meta-d opening a
# menu, etc.
('xfce4-terminal', 'xfce4-terminal', '-x'),
# Seems to break the terminal for weechat permanently:
('alacritty', 'Alacritty', '-e')
]
# See https://stackoverflow.com/questions/377017/test-if-executable-exists-in-python/12611523#12611523 # noqa
for term_exec, term_class, term_cmd_prefix in TERM_PREFS:
if distutils.spawn.find_executable(term_exec):
break
keys = (
modifier_window_commands(
{'wmclass': term_class},
f"{term_exec} {term_cmd_prefix} tmux -2 new-session -A -s {os.environ.get('TMUX_MAIN_SESSION', '0')} \\; set-window-option -q allow-rename on \\; set-window-option -q automatic-rename on", # noqa
'a', '1') # I used to use Aterm
+ modifier_window_commands(
{'wmclass': "Emacs"},
"em",
'e', '2') # Emacs
+ modifier_window_commands(
{'role': "browser"},
"google-chrome --remote-debugging-port=9222",
'g', '3') # Galeon used to be my browser
+ modifier_window_commands(
{"wmname": ".*pdf$", "wmclass": "(Evince|Acroread|Xpdf|Okular)"},
"evince",
'd') # pDf
+ modifier_window_commands(
{"wmclass": "jetbrains-pycharm"},
"/afs/dfn-cert.de/pet/software/bin/pycharm.sh",
"c", '4') # pyCharm
+ modifier_window_commands(
{"wmclass": "dolphin"},
"dolphin",
"p") # dolpPhin
+ modifier_window_commands(
{'wmclass': "Thunderbird"},
"thunderbird",
'z') # used to be: chatZilla is started from firefox but I
# (normally) don't use firefox for anything else
#
# since chatZilla is not supported any more, I use thunderbird
# chat for IRC
+ modifier_window_commands(
{'wmclass': "Claws-mail"},
"claws-mail",
'm') # claws-Mail
+ modifier_window_commands(
{'wmclass': "keepassxc"},
"keepassxc",
'k') # keepassxc
+ modifier_window_commands(
{'wmclass': "TelegramDesktop"},
"Telegram",
't') # Telegram
+ [
Key(
[mod, "control", "shift"], "Return",
lazy.spawn("sh -c 'screen -S tmuxttyS0 -X kill ; (i3lock --ignore-empty-password --show-failed-attempts --tiling --image ~/Bilder/OrionWF1920x1200.png || xlock -mode blank); while pgrep \"(xlock|i3lock)\"; do sleep 2; done; tmux-on-serial'")),
# Switch between windows in current stack pane
Key(
[mod], "Down",
lazy.layout.down()
),
Key(
[mod], "Up",
lazy.layout.up()
),
Key(
[mod], "Left",
lazy.layout.next(),
lazy.layout.left()
),
Key(
[mod], "Right",
lazy.layout.previous(),
lazy.layout.right()
),
# Move windows in current stack pane -> I'd like to have this, but
# move_* commands do not exist
Key(
[mod, "shift"], "Down",
lazy.layout.shuffle_down()
),
Key(
[mod, "shift"], "Up",
lazy.layout.shuffle_up()
),
Key(
[mod, "shift"], "Left",
lazy.layout.client_to_previous(),
lazy.layout.shuffle_left()
),
Key(
[mod, "shift"], "Right",
lazy.layout.client_to_next(),
lazy.layout.shuffle_right()
),
# resize columns
Key(
[mod, "control", "shift"], "Down",
lazy.layout.grow_down()
),
Key(
[mod, "control", "shift"], "Up",
lazy.layout.grow_up()
),
Key(
[mod, "control", "shift"], "Left",
lazy.layout.grow_left()
),
Key(
[mod, "control", "shift"], "Right",
lazy.layout.grow_right()
),
# Switch window focus to other pane(s) of stack
Key(
[mod], "Tab",
lazy.layout.previous()
),
Key(
[mod, "shift"], "Tab",
lazy.layout.next()
),
# Swap panes of split stack
Key(
[mod, "control"], "space",
lazy.layout.rotate()
),
# Toggle between split and unsplit sides of stack.
# Split = all windows displayed
# Unsplit = 1 window displayed, like Max layout, but still with
# multiple stack panes
Key(
[mod, "shift"], "Return",
lazy.layout.toggle_split()
),
Key([mod], "Return", lazy.spawn("xterm")),
Key(
[mod, "shift"], "plus",
lazy.layout.add_column()
),
Key(
[mod, "shift"], "minus",
lazy.layout.remove_column()
),
Key(
[mod], "n",
lazy.layout.normalize
),
# Toggle between different layouts as defined below
Key([mod], "space", lazy.next_layout()),
Key([mod, "shift"], "space", lazy.prev_layout()),
Key([mod, "control"], "r", lazy.restart()),
Key([mod, "control"], "q", lazy.shutdown()),
Key([mod, "shift"], "r", lazy.spawncmd()),
Key([mod], "r", lazy.spawn("rofi -show-icons -show combi")),
Key([mod], "x", lazy.qtilecmd()),
Key([mod], "b", lazy.hide_show_bar()),
Key([mod, "control"], "Delete", lazy.window.kill()),
# Key([mod], "w", lazy.window.kill()),
Key([mod], "y",
lazy.function(PrevFocus())),
Key([mod, "control"], "Left", lazy.prev_screen()),
Key([mod, "control"], "Right", lazy.next_screen()),
Key([mod, "control"], "f", lazy.window.toggle_floating()),
Key([mod], "f", lazy.window.toggle_fullscreen()),
Key([mod], "comma", lazy.window.toggle_minimize()),
Key([mod], "period", lazy.window.toggle_maximize()),
Key([mod], "s", lazy.spawn("rofi -show-icons -show window")),
# not focusable or managable any more, use with caution
Key([mod, "control"], "s", lazy.function(make_sticky)),
Key([mod, "mod1"], "s",
lazy.spawn("sudo -n pm-suspend-hybrid")),
Key([mod, "control", "mod1"], "s",
lazy.spawn("sudo pm-suspend")),
Key(["mod1", "mod4"], "Scroll_Lock",
lazy.spawn("/usr/bin/env bash -c '"
"autorandr --change; "
"setxkbmap -variant basic; "
"xmodmap ~/.Xmodmap; xmodmap ~/.Xmodmap.$(hostname)"
"'"),
lazy.restart()),
Key([mod], "odiaeresis", lazy.spawn("playerctl previous")),
Key([mod], "adiaeresis", lazy.spawn("playerctl next")),
Key([mod], "numbersign", lazy.spawn("playerctl play-pause")),
Key(["mod1", "mod4"], 'a', lazy.next_urgent()),
Key(["control", "mod1", "mod4"], 'a',
lazy.cmd_simulate_keypress([mod], "a"))
]
)
if gemini:
keys.extend([
Key(["mod5"], "b", lazy.spawn("gemini-brightness -")),
Key(["mod5"], "n", lazy.spawn("gemini-brightness +")),
Key([], "XF86MonBrightnessDown", lazy.spawn("gemini-brightness -")),
Key([], "XF86MonBrightnessUp", lazy.spawn("gemini-brightness +")),
Key(["mod5"], "XF86MonBrightnessDown",
lazy.spawn("gemini-brightness -")),
Key(["mod5"], "XF86MonBrightnessUp",
lazy.spawn("gemini-brightness +")),
Key(["mod5"], "c",
lazy.spawn(
"/usr/bin/env bash -c 'aumix -v -||amixer sset Master 1%-'")),
Key(["mod5"], "v",
lazy.spawn(
"/usr/bin/env bash -c 'aumix -v +||amixer sset Master 1%+'")),
Key([], "XF86AudioLowerVolume",
lazy.spawn(
"/usr/bin/env bash -c 'aumix -v -||amixer sset Master 1%-'")),
Key([], "XF86AudioRaiseVolume",
lazy.spawn(
"/usr/bin/env bash -c 'aumix -v +||amixer sset Master 1%+'")),
])
else:
keys.extend([
Key([mod], "minus",
lazy.spawn(
"/usr/bin/env bash -c 'aumix -v -||amixer sset Master 1%-'")),
Key([mod], "plus",
lazy.spawn(
"/usr/bin/env bash -c 'aumix -v +||amixer sset Master 1%+'")),
])
groups = [Group(i, persist=(int(i) < 4), init=(int(i) < 4))
for i in "12345678"]
for i in groups:
# mod + F+ number of group = switch to group
keys.append(
Key([mod], "F%d" % int(i.name), lazy.group[i.name].toscreen())
)
# mod1 + shift + letter of group = switch to & move focused window to group
keys.append(
Key([mod, "shift"], "F%d" % int(i.name), lazy.window.togroup(i.name))
)
layouts = [
layout.Max(),
layout.Columns(name="Columns", num_columns=2, fair=True),
layout.RatioTile(),
layout.Floating()
]
if gemini:
widget_defaults = dict(
font='Bitstream Vera Sans',
fontsize=23,
padding=1,
)
else:
widget_defaults = dict(
font='Bitstream Vera Sans',
fontsize=16,
padding=3,
)
def _barstart():
return [
widget.GroupBox(this_current_screen_border='00FF00'),
widget.Prompt(),
# widget.WindowName(), # either this
# or this, not both!:
widget.TaskList(highlight_method='block',
# I need this for windows without icons:
unfocused_border='#333333'),
widget.Sep(),
# widget.TextBox("Friedel's config", name="config"),
]
def _barend():
ret = [
# widget.TextBox("Vol", name="volume_label"),
widget.Volume(fmt=" {}", emoji=True, volume_app="pavucontrol"),
widget.Volume(volume_app="pavucontrol"),
# widget.PulseVolume(volume_app="pavucontrol")
]
if laptop:
ret += [
widget.BatteryIcon(),
widget.Battery(format='{percent:2.0%} {hour:d}:{min:02d}'),
]
ret += [
widget.CurrentLayoutIcon(),
widget.CurrentLayout(markup=True, fontsize=20, fontshadow='0000FF'),
widget.Sep(),
widget.Clock(format='🕒 %Y-%m-%d %a %H:%M:%S'),
]
return ret
screens = [
Screen(
bottom=bar.Bar(
_barstart() + [
widget.TextBox("C", name="cpu_label"),
widget.CPUGraph(),
widget.TextBox("M", name="memory_label"),
widget.MemoryGraph(),
widget.TextBox("N", name="net_label"),
widget.NetGraph(),
widget.Systray(),
widget.Sep(),
] + _barend(),
30)),
Screen(
bottom=bar.Bar(
_barstart()
+ [
widget.Sep()
]
+ _barend(), 30))
]
# Drag floating layouts.
mouse = [
Drag([mod], "Button1", lazy.window.set_position_floating(),
start=lazy.window.get_position()),
Drag([mod], "Button3", lazy.window.set_size_floating(),
start=lazy.window.get_size()),
Click([mod], "Button2", lazy.window.bring_to_front())
]
dgroups_key_binder = None
dgroups_app_rules = []
main = None
follow_mouse_focus = True
bring_front_click = True
cursor_warp = False
floating_layout = layout.Floating()
auto_fullscreen = True
focus_on_window_activation = "smart" # "never" is also possible
# XXX: Gasp! We're lying here. In fact, nobody really uses or cares about this
# string besides java UI toolkits; you can see several discussions on the
# mailing lists, github issues, and other WM documentation that suggest setting
# this string if your java app doesn't work correctly. We may as well just lie
# and say that we're a working one by default.
#
# We choose LG3D to maximize irony: it is a 3D non-reparenting WM written in
# java that happens to be on java's whitelist.
wmname = "LG3D"
# see
# http://docs.qtile.org/en/latest/manual/faq.html#my-pointer-mouse-cursor-isn-t-the-one-i-expect-it-to-be # noqa
# see https://wiki.dfn-cert.de/cgi-bin/wiki.pl/Workstations15.1Not_WorkingYet#toc15
@hook.subscribe.startup
def runner():
subprocess.Popen(['xsetroot', '-cursor_name', 'left_ptr'])
# prevent xfce4-notifyd windows from jumping around by ignoring them
@hook.subscribe.client_new
def auto_sticky(window):
if window.name == "xfce4-notifyd":
if window.group:
screen = window.group.screen.index
else:
screen = window.qtile.current_screen.index
window.window.configure(stackmode=xcffib.xproto.StackMode.Above)
window.static(screen)
# from http://qtile.readthedocs.org/en/latest/manual/config/hooks.html#automatic-floating-dialogs
@hook.subscribe.client_new
def floating_dialogs(window):
dialog = window.window.get_wm_type() == 'dialog'
transient = window.window.get_wm_transient_for()
bubble = window.window.get_wm_window_role() == 'bubble'
if dialog or transient or bubble:
window.floating = True
@hook.subscribe.client_new
def float_plasma(window):
if window and window_match_re(window, wmclass="plasmashell"):
window.floating = True
# from https://github.com/ramnes/qtile-config/blob/98e097cfd8d5dd1ab1858c70babce141746d42a7/config.py#L108
@hook.subscribe.screen_change
def set_screens(qtile, event):
if not os.path.exists(os.path.expanduser('~/NO-AUTORANDR')):
subprocess.run(["autorandr", "--change"])
qtile.cmd_restart()
@desprit
Copy link

desprit commented Feb 12, 2021

group = qtile.currentGroup should be group = qtile.current_group for the recent version. Thanks for the config!

@TauPan
Copy link
Author

TauPan commented Feb 18, 2021

group = qtile.currentGroup should be group = qtile.current_group for the recent version. Thanks for the config!

Gist updated.

@Stefanomarton
Copy link

Hi there, sorry to bother, I found your config extremely nice. I'm trying to modify one of the function you wrote and I hope you can give me some insights.

def window_switch_to_screen_or_pull_group(**kwargs):
    """If the group of the window matched by match_window_re with the
    given **kwargs is in a visible on another screen, switch to the
    screen, otherwise pull the group to the current screen
    """

    def callback(qtile):
        windows = windows_matching_shuffle(qtile, **kwargs)
        if windows:
            window = windows[0]
            if window.group != qtile.current_group:
                qtile.focus_screen(window.group.screen.index)
            window.group.focus(window, False)

    return lazy.function(callback)

Instead of pulling the group in the current screen if the windows is not visibile on other screen I would like to make it visibile on the other screen and make it focus. I notice that qtile.focus_screen(window.group.screen.index) do not work if the windows is not visibile on other screen. Maybe you can suggest a workaround for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment