This page looks best with JavaScript enabled

Some Notes About SXHKD

 ·   ·  ☕ 5 min read · 👀... views

Simple X Hotkey Daemon

In simple words, SXHKD is a keyboard shortcut manager for any X server. It runs as a process or daemon and grabs keys to call bash commands. This is created by the same authors of BSPWM, the popular window manager.

Some people think that, SXHKD is/works only for BSPWM. But this is just a myth. SXHKD will work with any window manager or even desktop environments like Gnome as long as it is running the X window server.
This post contains just some advanced usage of SXHKD. So without writing any introductory information about the software as you will find tons of things better than I will ever write, I am writing just some cool aspects you can achieve with SXHKD that is hardly found on the internet.

My sxhkd setup

I have been using BSPWM for 1 year only and I am in love with this amazing software sxhkd. I use this to manage windows and launch frequently used programs like terminal/browser/file-manager. The way I use sxhkd is vim-like multimodal window editing.

When I am in normal mode, I use super + {h,j,k,l} to move focus to other windows. To move window to another place, I use super + shift + {h,j,k,l}. And when I go to VISUAL-MODE, I use {h,j,k,l} to move focus to other windows. To move window to different location while in VISUAL-MODE, I use super + {h,j,k,l}. Though it is totally my personal preference, I think this way, it saves a lot of key combos to do advanced layout operations like flipping/rotating layouts etc.

You can find my sxhkdrcs’ in my linux-dots repository.

Without further ado, let me just share the things I am able to achieve.

VIM like multi-mode with polybar status

You can change modes of your keyboard in SXHKD just like you change mode in vim!

mode-select

Notice that, sxhkd has built in support for modes (called key chords). But they have some limitations, like you cannot define how to switch between chords or exit from one. Also, it becomes cluttered if you dump all configurations of all modes in a single sxhkdrc file. So I went to a different approach.

To change mode with a keybinding, you need to restart the sxhkd with different config file. So select a key comb you want to use to change to a specific mode and put the following lines in your sxhkdrc file.

# Change to VISUAL mode
super + m
	killall -e sxhkd && sxhkd -c $HOME/.config/sxhkd/mode-visual &

Here, mode-visual is another sxhkdrc file with the bindings necessary for that new mode in sxhkdrc. To go back to the default or other mode, just do the same with your desired mode’s config file.

# Exit visual mode
@Escape
	killall -e sxhkd && sxhkd &
super + m
	killall -e sxhkd && sxhkd &

As shown in the above gif, you can use polybar hook to update an icon indicating the current mode of sxhkd.

# Change to VISUAL mode
super + m
	killall -e sxhkd; polybar-msg hook window-mode 2 > /dev/null && sxhkd -c $HOME/.config/sxhkd/mode-visual &

And in your polybar configuration, use the below module in any bar you want:

1
2
3
4
5
6
[module/window-mode]
type = custom/ipc
content = 
hook-0 = echo "󰍹"
hook-1 = echo %{F#f00}󱋆%{F-}
initial = 1

I basically use this just like vim. In normal mode, I can use the current window and navigate to other windows normally. But in VISUAL-MODE I can more advanced window moving like rotating/flipping layouts, adjusting sized and receptacles, etc. This way you can save a lot of keybindings that you do not need while simply using 2 or 3 windows.

Organize and use sxhkd in multiple setups

I do not remember where I found this on internet, but with sxhkd, you can use multiple config files at the same time overriding same keybindings by the latter loaded config file. For example, using the below command:

1
sxhkd -c $HOME/.config/sxhkd/sxhkdrc $HOME/.config/sxhkd/sxhkdrc.common

will first load the sxhkdrc file, and then load sxhkdrc.common file. If they have a common keybinding in both files, sxhkdrc will be given priority.

By this way, you can put all your machine independent keybindings in sxhkdrc.common file, and put machine specific keybindings in sxhkdrc file of that machine.

Display keyboard shortcut help with rofi/dmenu

This one looked very cool to me. You can easily parse your sxhkdrc file and display them as a help menu with dmenu or rofi. Use the below script for this. You can put this in show-help script in a directory which is in your PATH.

1
2
3
4
5
#!/bin/bash

awk '/^[a-z]/ && last {print "<small>",$0,"\t",last,"</small>"} {last=""} /^#/{last=$0}' ~/.config/sxhkd/sxhkdrc{,.common} |
    column -t -s $'\t' |
    rofi -dmenu -i -markup-rows -no-show-icons -width 1000 -lines 15 -yoffset 40

Your sxhkdrc file should be well documented like the following structure.

# Your Documentation for this keybinding
super + a
	your_command

Just put a keybinding to show help for all keybindings 😲

# Show help
super + slash
    sxhkd-help

Conclusion

I really like the software sxhkd because it goes with the unix philosophy. It is straight forward and very much customizable. With the help of xdotool, you can pretty much do whatever you want!

If you also have dome some cool things with sxhkd, I will be glad to hear some in the comments!

Share on

Rahat Zaman
WRITTEN BY
Rahat Zaman
Graduate Research Assistant, School of Computing