🚀 A command with autocomplete made with C++.
Today was pretty calm, so I spent some time tinkering with ideas. Among them, I found a link to a bunch of emojis in my bookmarks and thought:
- — Why not fetch these emojis via the command line to make things easier?!
So I started writing some basic code and came up with this simple command to help out, even if just in a basic way.
See how to compile and install it on your Unix-like system.
💼 Installation
Of course, you’ll need either GCC or Clang installed.
Then just follow these steps:
⤵️ 01. Download the emoji list:
Using wget:
wget -q bit.ly/emoji-txt -O ~/.emojis.txt Or using cURL:
curl -sL bit.ly/emoji-txt -o ~/.emojis.txt 📄 02. Create a C++ file:
Example:
vim emoji.cpp
Paste this code inside:
#include <iostream> #include <fstream> constexpr auto show_emoji = [](const std::string& query){ const std::string user = std::getenv("USER"); std::ifstream file("/home/" + user + "/.emojis.txt"); std::string line; bool found = false; while(std::getline(file, line)){ const size_t space_pos = line.find(' '); if (space_pos == std::string::npos) continue; const std::string name = line.substr(0, space_pos); const std::string emoji = line.substr(space_pos + 1); if(name.find(query) != std::string::npos){ std::cout << emoji << " " << name << '\n'; found = true; } } if(!found){ std::cerr << "Emoji not found.\n"; } }; int main(int argc, char** argv){ if (argc < 2) return 1; show_emoji(argv[1]); } 🚧 03. Compile and install:
g++ -O3 -ffast-math emoji.cpp -o emoji sudo mv emoji /usr/local/bin 📺 04. Add autocomplete to your terminal:
To add autocomplete for Bash:
Add this at the end of your
~/.bashrc
_emoji_complete() { local cur=${COMP_WORDS[COMP_CWORD]} COMPREPLY=( $(compgen -W "$(grep "^$cur" ${HOME}/.emojis.txt)" -- "$cur") ) } complete -F _emoji_complete emoji Then run: source ~/.bashrc
To add autocomplete for ZSH:
Add this at the end of your
~/.zshrc
_emoji_complete() { local -a matches local cur="$words[2]" matches=($(grep "^$cur" "${HOME}/.emojis.txt")) compadd -- $matches } compdef _emoji_complete emoji Then run: source ~/.zshrc
Test:
$ emoji game<TAB> game game_die $ emoji game<ENTER> 🎮 game 🎲 game_die I used this tool to fill in the emojis for this post: I typed things like emoji tv, emoji const (for construction), emoji doc (document), ... Soon you’ll get used to the keywords and find them all easily! 😺
Pretty neat, right? 😃

Top comments (0)