2022-10-15 Converting words and sentences to identifiers in shell

About two weeks ago I wrote about a simple piece of Elisp to convert a random English phrase to identifier. I showed it to one of my colleagues and she decided it would be cool to have it outside Emacs. Since it was basically just having fun with regexen, I decided that a shell script utilizing sed should work, too (though obviously, and Emacs command is more convenient;-)). So, after a bit of playing (and also learning from her how to make sed convert letters into lowercase), we came up with this.

#!/bin/bash
sed -r -e 's/[[:upper:]]/\L&/g' -e 's/[^[:alpha:]]+$//' -e 's/[^[:alpha:]]+/_/g'

The first sed command finds all the uppercase characters and converts them to lowercase. The \L construct is a GNU sed feature – \L1 converts the first matched group to lowercase, and \L& converts the whole thing. The second command trims non-letter characters from the end of the input, and the last one converts runs of non-letter characters into underscores.

How to plug this into VSCode (or other editors) is an exercise left to those pour souls trapped in VSCode (or other editors).

CategoryEnglish, CategoryBlog