NokiMo
The Linux Cast
The Linux Cast

patreon


Fun with grep, awk and sed.

The Linux Dabbler, aka Mike, posted a video yesterday detailing a process where you could use a few terminal commands to display your keybindings. He got the idea from AwesomeWM, and then wrote a script for his spectrwm install.

It's awesome.

So I thought I'd steal the idea and do something for DWM. I didn't think it deserved it own video, so I just decided to share it here for my patrons.

What I did first was:

grep XK_ ./config/suckless/dwm/config.def.h

This gave me a list basically of all my key bindings, but it wasn't perfect.

In the end I decided to go for

grep MODKEY ./config/suckless/dwm/config.def.h

This was better. It still threw out some lines in the config that use MODKEY but are not keybindings, so I combined together with a pipe.

Then it was a matter of using awk to print out just the things I needed to display.

awk {'print $2 " " $3 " --- " $4 " " $7'} 

Then sed out all the extraneous symbols and changing ControlMask to Ctrl and ShiftMask to Shift so that it's prettier.

This is what I ended up with. 

grep XK_ .config/suckless/dwm/config.def.h | grep MODKEY | awk {'print $2 " " $3 " --- " $4 " " $7 '} | sed 's/,//g' | sed 's/XK_//g' | sed 's/}//g' | sed 's/&//g' | sed 's/|ShiftMask/ + Shift'/g | sed 's/MODKEY/MOD/g' | sed 's/|ControlMask/ + Ctrl/g' | column

It's not perfect. 

Like MOD tab isn't showing up right. but the rest of it is pretty good. I'll need to figure out a bit about how to get things to line up and then put it into an actual script so I can call it via a key binding. 

I know most of my keybindings by heart, but the layout ones are always ones I'm forgetting. I'll need to also add to the script a line in there to print out what layouts correspond with which function keys as well.

I'll admit that this was very fun. I'm going to play around a lot more with awk and maybe do a video on it once I learn more. 





Related Creators