home

Emacs, meet Lozenge

Recently I’ve stolen from tremendous anvil editor 1 idea of using Lozenge () as an utility character in Emacs.

Since it integrated almost seamlessly I decided to share it with world.

While article this is Emacs-specific, it’s highly probable you could integrate following tricks in the editor of your choice.

Setup

Before I get to integration I’ll start basic some axioms and conventions + setup.

  • this is lozenge
  • lozenge doesn’t have to be a lozenge symbol - one can use any character or unicode symbol, but it must be unique in a way that there’s very low chance it will be part of the text 2.

Personally I use 3 different “lozenges”: ◊ ♯ ♭ for Lozenge, SHIFT+, META+

How to input ?

In Emacs there are two uncomfortable ways to do it:

  • C-x 8 RET LOZENGE
  • C-q 22712 SPC

These are quite unwieldy though. Thankfully, my keyboard has a § on it. While § on itself is perfectly good candidate for lozenge, I remapped it to ◊ because I like the look of it more.

Tricks

Smart Flush

Imagine you have the following, anonymous text:

item-one
item-abc
item-two
item-def
item-three
item-ghi
... 30 more lines

Now you’d like to flush only the lines that have: one, two, three, etc. Of course, a solution would be to write smart Emacs Lisp, but you’re low on time. With lozenges, you can do this instead:

◊item-one
item-abc
◊item-two
item-def
◊item-three
item-ghi

... 30 more lines

…and C-x h M-x flush-lines ◊ RET (i.e. mark whole buffer and flush lines with lozenge) - voilà!

Instant Templates (just add water)

Because is treated as a part of the word, it means you can have the ad-hoc templates.

three_◊ys = "◊y ◊y ◊y"

Select ◊y, start iedit – I think default is C-;, type “pig” and observe

three_pigs = "pig pig pig"

And again, as is part of the word, which means that one can have multiple various “lozenge variables” and use it in template with iedit.

♬ can you feel theeeee ♬ …………… SPEED OF EDITING EXPERIENCE ?! 3

Regexp Catch-Guards

Thanks to lozenges, it’s easier to perform transformations on a text using regex, for instance, consider the following:

one   = "1"
two   = "2"
three = "3"
# ...write until 20

I’ve fallen asleep even looking at it, but hey, work is work, right?

But let’s minimize amount of work needed:

one◊1
two◊2
three◊3
four◊4

With that I can now do C-M-% (regexp replace) and provide a simple \(.*\)◊\(.*\) -> \1 = “\2”= regexp for a replacement.

In fact, if you’re feeling Lispy you could even make couple search templates like these, and bind them to some command:

  • \(.*\)◊\(.*\)
  • \(.*\)◊\(.*\)◊\(.*\)
  • \(.*\)◊\(.*\)◊\(.*\)◊\(.*\)
  • \(.*\)◊\(.*\)◊\(.*\)◊\(.*\)◊\(.*\)
  • \(.*\)◊\(.*\)◊\(.*\)◊\(.*\)◊\(.*\)◊\(.*\)

…or maybe have delimited ones:

  • ◊\(.*\)◊
  • ◊\(.*\)◊\(.*\)◊
  • ◊\(.*\)◊\(.*\)◊\(.*\)◊
  • ◊\(.*\)◊\(.*\)◊\(.*\)◊\(.*\)◊
  • ◊\(.*\)◊\(.*\)◊\(.*\)◊\(.*\)◊\(.*\)◊

Psst… I wrote above using template ;-)

The Recipe For Macro Success

  1. Mark places with
  2. Start your macro with C-s ◊ RET
  3. …profit!

Having a placeholder for macros can be somewhat annoying; if places are simply marked using lozenges, it is not only possible to have a visual cue where exactly you’ll land, but it is much harder to misfire on searches.

G◊-◊-◊-◊ Lozenge crazy

If you’re using tool like Ukelele or can modify your keyboard layout through other means you can go completely modifier crazy, and, for example (◊K is LOZENGE KEY):

  • ⁣ ⁣ ◊K --> ◊
  • M-◊K --> ♯
  • C-◊K --> §
  • S-◊K --> ⧫
  • … and so on

This provides endless opportunities for indulging in lozenge-driven marking, replacing, deleting or templating experiences.

Final Words

If you’d like to add anything, found a typo, or just want to chat Emacs/Engineering/Life feel free to drop me an-email. I respond even though I tend to be forgetful at times.


  1. Check it out if you’re suffering from Emacs Fatigue ↩︎

  2. I’d recommend against using any cool symbol - if you use it you’re going to see that symbol A LOT. Boring, non-intrusive symbols work the best. ↩︎

  3. Note that it’s possible to have multiple variables starting with , so one could have ◊first, ◊second, etc. ↩︎

Przemysław Alexander Kamiński
vel xlii vel exlee

gh | li | rss

Powered by hugo and hugo-theme-nostyleplease.