Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Search for Emojis Using ripgrep or grep with a Regex

search-for-emojis-using-ripgrep-or-grep-with-a-regex.jpg

Ripgrep has a convenience pattern to search for but if you're using regular grep you can use a long regex.

Quick Jump:

This is especially handy if something you’re developing sprinkles in a few emojis and you want to search for it in your code base. I like using emojis in flash messages and sometimes in nav bars when I don’t want to pull in a 3rd party icon library.

# Ripgrep

If you’re using ripgrep life is easy, you can use rg "[\p{Emoji}--\p{Ascii}]". You can make it even easier with an alias. I’ve set up a ge (grep emojis) alias for that in my dotfiles.

For example, I ran the above within my docker-flask-example app and it returns:

README.md
1:# ๐Ÿณ An example Flask + Docker app
20:## ๐Ÿงพ Table of contents
36:## ๐Ÿงฌ Tech stack
77:## ๐Ÿฃ Notable opinions and extensions
115:## ๐Ÿš€ Running this app
225:## ๐Ÿ” Files of interest
262:## โœจ Running a script to automate renaming the project
346:## ๐Ÿ›  Updating dependencies
390:## ๐Ÿค See a way to improve something?
395:## ๐ŸŒŽ Additional resources
423:## ๐Ÿ‘€ About the author

The creator of ripgrep posted about this solution in a GitHub issue.

As he mentioned, it’s not bullet proof. 0๏ธโƒฃ may not get picked up. It doesn’t for me. In fact with the Microsoft Terminal 0-9 emojis don’t even render. Still, as long as you’re aware of that it works quite nicely for the other 99% of them.

I did find this gist which has most emojis listed, the above pattern found them all.

By the way, if your editor’s fuzzy finder uses ripgrep you can use the same pattern, although I find that to be less convenient since I haven’t memorized the pattern. Personally I find it faster to run the alias from my terminal and then copy / paste what’s needed into my editor.

# Grep

There’s no shortcut like ripgrep has but you can write a regex and then alias it:

# It's important to use -P | --perl-regexp for Perl compatible regular expressions.
grep --perl-regexp "[\x{1f300}-\x{1f5ff}\x{1f900}-\x{1f9ff}\x{1f600}-\x{1f64f}\x{1f680}-\x{1f6ff}\x{2600}-\x{26ff}\x{2700}-\x{27bf}\x{1f1e6}-\x{1f1ff}\x{1f191}-\x{1f251}\x{1f004}\x{1f0cf}\x{1f170}-\x{1f171}\x{1f17e}-\x{1f17f}\x{1f18e}\x{3030}\x{2b50}\x{2b55}\x{2934}-\x{2935}\x{2b05}-\x{2b07}\x{2b1b}-\x{2b1c}\x{3297}\x{3299}\x{303d}\x{00a9}\x{00ae}\x{2122}\x{23f3}\x{24c2}\x{23e9}-\x{23ef}\x{25b6}\x{23f8}-\x{23fa}]" <FILE>

I sniped that from StackOverflow and the author did mention it covers “almost all” emojis.

That returns the same output as the ripgrep solution, at least for the files I’ve tried. I diff’d that regex against the ripgrep version using this list of emojis and the output is the same.

Since I have ripgrep I likely won’t be using the grep variant but it’s good to know that a solution exists.

The demo video shows both solutions.

# Demo Video

Timestamps

  • 0:18 – Ripgrep solution
  • 1:48 – Grep solution
  • 2:43 – Diff a large list of emojis

Do you think you’ll be using this from time to time? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments