2

I've been using Texpander as described in this answer to insert boilerplate text snippets into browser windows and word processor documents in an Xorg session.

Recently, a comment by @vanadium here indicated that Rofi could be used as well:

I would for sure use rofi (or dmenu) for such user case. It has a fantastic quick search that narrows quickly down based on substrings you type. I use it for inserting custom text snippets among others.

How do I go about using Rofi for this purpose?

DK Bose
  • 42,548
  • 23
  • 127
  • 221

1 Answers1

2

Requirements: rofi, xclip, and xdotool in an Xorg session

According to apt show rofi, Rofi is a

window switcher, run dialog and dmenu replacement

I searched the internet and came across User scripts. One of the scripts listed there, Kaomoji Rofi is described as "a simple kaomoji picker". I took its code (which I don't fully understand) and modified it:

#!/bin/bash

selection=$(rofi -i -width 1000 -theme solarized -dmenu $@ < /home/dkb/Documents/snippets.txt)
kaomoji=$(echo $selection)
echo -n "$kaomoji" | xclip -selection clipboard
sleep 0.1
xdotool key shift+Insert
  • Rofi doesn't feature word wrap and so having -width 1000 or more is useful to see as much of the snippet contents because only one line is allotted to per snippet. Anything exceeding the width is represented by …
  • if -theme isn't used, the default theme or the theme last chosen by using rofi-theme-selector as described here will be used.
  • The snippets are in a plain text file, one snippet per line. (Running sort -u -o $HOME/Documents/snippets.txt $HOME/Documents/snippets.txt once in a while keeps them sorted though it isn't really necessary.)
  • sleep 0.1 and xdotool key shift+Insert are not essential. Users can use their preferred method to paste clipboard text.

I saved the script as rofi-snippets.sh in $HOME/bin, made it executable, and bound it to Ctrl+` as the keyboard shortcut to launch it.

Users can rapidly select the snippet of their choice by typing in one or a few characters to highlight just the one snippet and then pressing Enter.

Here's an animation illustrating the script in action:

Animation showing the use of Rofi to add snippets

DK Bose
  • 42,548
  • 23
  • 127
  • 221