22 May 2023

Recipe: Let’s Build a Kaomoji Generator

By Christian Rocha

This is what we’re gonna make.

Before the emoji there was the Kaomoji. A bunch of rando unicode letters smashed together to create cute, little images.

  • ʕっ•ᴥ•ʔっ Like a bear reaching out to hug you.
  • 。:゚(。ﹷ ‸ ﹷ ✿) Or a pouty child with a flower in her hair.
  • ((งง •̀•̀__•́•́))งง Or a dude who is so ready to get into a fight that he’s shaking with fury.

Like internet snowflakes, Kaomojis have seemingly infinite number of permutations. You could have an angry bear doing a table flip, or a melancholy man tossing stars to the wind.

But honestly, who has the time to come up with these? Or to research the best ones? Computers do, that’s who. So we’re gonna build a little Kaomoji generator with Mods, Gum, and bash.


The Mods Logo
The modchip of AI.

The keystone to all this is Mods, an open source tool that brings AI to the command line. It’s especially good with pipelines and generally working with other commmand line tools in true unix fashion. You can install it here.

You’ll also want to install Gum.

The script

So let’s get to it already. The basic approach we’re taking is:

  1. Ask mods to generate some Kaomojis
  2. Use gum to pick one
  3. Copy the Kaomoji to the clipboard

And here’s how we’re doing it:

#!/usr/bin/env bash

# If the user passes '-h', '--help', or 'help' print out a little bit of help.
# text.
case "$1" in
	"-h" | "--help" | "help")
        printf 'Generate kaomojis on request.\n\n'
        printf 'Usage: %s [kind]\n' "$(basename "$0")"
        exit 1

# The user can pass an argument like "bear" or "angry" to specify the general
# kind of Kaomoji produced.
if [[ $1 != "" ]]; then
	sentiment=" $1"

# Ask mods to generate Kaomojis. Save the output in a variable.
kaomoji="$(mods "generate 10${sentiment} kaomojis. number them and put each one on its own line.")"
if [[ $kaomoji == "" ]]; then
	exit 1

# Pipe mods output to gum so the user can choose the perfect kaomoji. Save that
# choice in a variable. Also note that we're using cut to drop the item number
# in front of the Kaomoji.
choice="$(echo "$kaomoji" | gum choose | cut -d ' ' -f 2)"
if [[ $choice == "" ]]; then
	exit 1

# If xsel (X11) or pbcopy (macOS) exists, copy to the clipboard. If not, just
# print the Kaomoji.
if command -v xsel &> /dev/null; then
	printf '%s' "$choice" | xclip -sel clip # X11
elif command -v pbcopy &> /dev/null; then
	printf '%s' "$choice" | pbcopy # macOS
	# We can't copy, so just print it out.
	printf 'Here you go: %s\n' "$choice"
	exit 0

# We're done!
printf 'Copied %s to the clipboard\n' "$choice"

To execute the script just call it with bash:

$ bash kaomoji

Or make the script executable and call it directly:

$ chmod +x ./kaomoji
$ ./kaomoji

Going further

How would you improve this script? Here are a few ideas:

  • Add an argument to specify the number of Kaomojis generated
  • Let the user choose more than one Kaomoji with gum choose --no-limit
  • Colorize the output with gum style
  • Save your fave Kaomojis to a Skate database

Whatcha think?

Have some feedback on this post? We’d love to hear. Let us know in Discord or via email at vt100@charm.sh.


Read this post in your terminal with Glow:

glow -p https://charm.sh/blog/kamoji-generator.md Copied!

By Christian Rocha

22 May 2023

Christian is one of the Charm founders.

Lets chat!

Have a question about a command line thing you’re building? Got an idea for a new feature? Just wanna hang out? You’re always welcome in the Charm Discord.