Integrate rofi launcher with dict.

In this blog post I would like to demonstrate something. Rofi is a great application launcher. It is fully configurable, has some decent key bindings and you can customize how it looks in every aspect. That makes rofi a prefect choice if you are using minimal window manager like openbox, dwm, bspwm etc.

Dictd is an dictionary software that can be used in console. It can be used to combine word search in multiple dictionaries like english-german, english-french and some "non-standard" like The Devils Dictionary .

Now, I will show you something cool. You can search for word definitions in dictionaries by selecting suggested word!

Here is how this is done.

  1. Rofi can be launched with custom mode. We will use that to launch dictd with selected word from rofi.
  2. To display some possible selections (words) use the dictionary files for given language in the /usr/share/dict/ directory.
  3. Word selection is narrowed by the rofi itself. To display first possible word at least two characters need to be inserted and enter has to be pressed. Loading whole word dictionary (~120k lines) might slow down rofi.
  4. Chosen word is passed to dict $CHOSEN_WORD and output is printed to stdout.
  5. To make rofi open a window with results use -e switch supported by most of the terminal emulators, like termite -e "echo \"dupa\" | less". Less will keep the new window opened and it used to navigate through results.

Below video demonstrates how it works:

The whole solution is implemented by below code:

rofi modi script

LANG=$1
WORD=$2
WORD_LEN=${#WORD}
if [ "${WORD:0:5}" = word: ]
then
    DICT_WORD=${WORD:5}
    LANG=en_US.utf8 alacritty --class Dictionary,Dictionary -e bash -c "dict $DICT_WORD | less" &> /dev/null &
    exit
fi

if [ $WORD_LEN -gt 1 ]
then
    if [ $1 == "PL" ]
    then
        grep ^$WORD /usr/share/dict/polish | sed -e 's/^/word:/'
    else
        grep ^$WORD /usr/share/dict/american-english | sed -e 's/^/word:/'
    fi
fi

To run it use:

rofi -modi "dictionary:PATH_TO_ABOVE_SCRIPT EN" -show dictionary

or use a keybinding, in this example sxhkd

super + e
    /usr/bin/rofi -modi "dictionary:~/.config/rofi/scripts/dictionary.sh EN" -show dictionary

You can find complete solution in my dotfiles.

Top