Skip to content

Instantly share code, notes, and snippets.

@WGH-
Created October 30, 2016 20:42
Show Gist options
  • Select an option

  • Save WGH-/50be0a5c4dd9cd9d3ffc5d050d009c83 to your computer and use it in GitHub Desktop.

Select an option

Save WGH-/50be0a5c4dd9cd9d3ffc5d050d009c83 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import pprint
import re
import subprocess
def get_mapping():
p = subprocess.Popen(["xmodmap", "-pke"], stdout=subprocess.PIPE)
keysym_to_code = {}
for line in p.stdout:
line = line.decode('ascii').strip()
keycode, keysyms = line.split("=")
keycode = keycode.strip()
keysyms = keysyms.strip().split()
keycode = int(re.match('^keycode +(\d+)$', keycode).group(1) )
for keysym in keysyms:
keysym_to_code[keysym] = str(keycode)
p.wait()
return keysym_to_code
def process_bindsym(line, keysym_to_code):
args = line.split(" ")
assert args[0] == "bindsym"
args[0] = "bindcode"
args[1] = args[1].split("+")
args[1][-1] = keysym_to_code[args[1][-1]]
args[1] = "+".join(args[1])
line = " ".join(args)
return line
def main():
keysym_to_code = get_mapping()
#pprint.pprint(keysym_to_code)
for line in sys.stdin:
line = line.rstrip("\n")
# preserve whitespace indentation
m = re.match("^( *)(bindsym.*)$", line)
if m:
print(m.group(1) + "#-- " + m.group(2))
print(m.group(1) + process_bindsym(m.group(2), keysym_to_code))
else:
print(line)
if __name__ == "__main__":
main()
@make-github-pseudonymous-again
Copy link

make-github-pseudonymous-again commented Feb 13, 2018

Some of my bindsym's had no matching bindcode so I added a try...except block. I also tweaked the script so that its output can be piped through another instance. So I can keep just one config file. You can see the result here.

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