notes from /dev/null

by Charles Choi 최민수


Announcing Casual RE-Builder

15 Jul 2024  Charles Choi

Knowing how to use regular expressions (regexp) is a superpower. While there are many different variants of regexp syntax, in Emacs the reward is handsome if you put in the effort to learn its flavor of it. Emacs has many interactive commands that take in a regexp argument. Here’s a small sample:

dired-do-copy-regexp, dired-do-find-regexp , dired-do-find-regexp-and-replace , dired-do-hardlink-regexp , dired-do-isearch-regexp , dired-do-query-replace-regexp , dired-do-rename-regexp , dired-do-search , dired-do-symlink-regexp , dired-flag-files-regexp , dired-isearch-filenames-regexp , dired-mark-files-containing-regexp , dired-mark-files-regexp , ibuffer-do-isearch-regexp , ibuffer-do-query-replace-regexp , ibuffer-mark-by-content-regexp , ibuffer-mark-by-file-name-regexp , ibuffer-mark-by-mode-regexp , ibuffer-mark-by-name-regexp , isearch-backward-regexp , isearch-forward-regexp , isearch-highlight-lines-matching-regexp , isearch-highlight-regexp , isearch-query-replace-regexp , query-replace-regexp

Emacs has a built-in tool called RE-Builder that makes it easier to learn and construct regexps. Invoked by the command re-builder, this creates a small editable window where you can interactively enter a regexp and see matches (if any) highlighted in a target buffer, typically the last one you were working on. While it’s nice to have around, recalling its command set can be a chore which makes it an ideal candidate for Casual.

Announcing Casual RE-Builder now on MELPA as part of the Casual Suite of Emacs porcelains. Install this as an individual package or if you already have Casual Suite installed, update your packages and you’ll automatically pick this up.

Notable Features

  • Easy access to regexp syntax documentation.
    • The less friction it takes to figure out the right syntax, the further along you are to having your desired regexp.
  • Export regexp for interactive use.
    • Basic RE-Builder only supports exporting a regexp to use in Elisp code. This regexp unfortunately can not work when entered interactively because of character escaping. Casual addresses this by providing support for exporting a regexp that can be used interactively.

Usage

Basic Usage

When the command re-builder is invoked, a buffer named “✳︎RE-Builder✳︎” is created. Activate Casual RE-Builder with the binding C-o (or one of your preference).

At the top of the menu shows the title “RE-Builder” with the target buffer enclosed in parenthesis. The regexp pattern will be applied to the target buffer. The target buffer can be changed with the (b) Target buffer menu item.

Emacs supports three different regexp syntax: 1) read, 2) string, 3) Rx. Use the (x) Syntax menu item to alter it. The current syntax is shown in parenthesis.

If multiple sub-expressions are in the regexp pattern, then they can be observed via the (s) Subexp mode menu item.

If the regexp pattern entered in the “✳︎RE-Builder✳︎” finds multiple matches, a match can be navigated to via the (p) Previous and (n) Next menu items.

Exporting the Regexp Pattern

Once a desired regexp pattern is defined, there are two menu items that can be used to export (copy) it to the kill-ring for further use.

  • (w) Interactive will copy the regexp to the kill-ring so that it can be yanked in an interactive command that requires a regexp (e.g. query-replace-regexp).
    • This should only be used when the regexp syntax is set to string.
    • ❗️When yanking (typically C-y) a regexp into an interactive prompt, you must have the point/focus in the minibuffer prompt (typically via mouse). Otherwise the desired content can be altered with extra escaping.
  • (c) Code will copy the regexp to the kill-ring so that it can be yanked into a Elisp code that requires a regexp argument.

Regexp Syntax Help

The menu item (i) will invoke the Info page for regexp syntax with respect to the current syntax type.

Quitting RE-Builder

Select (q) Quit to exit the RE-Builder tool.

Closing Thoughts

While I’ve known and used RE-Builder for years, it has never been an “essential” tool to me because it was so clumsy to orchestrate its output with other interactive regexp commands. Add to that the inertia to learn yet another regexp variant and ultimately it would result in me overlooking Emacs commands that used regexp arguments.

Adding interactive export and orchestrating it with the regexp commands in Casual Dired and Casual IBuffer has been an eye-opening and exhilarating experience. With the above I can now do multi-file refactoring in Emacs with confidence. Before Casual RE-Builder, this would of been both a heroic and unthinkable notion.

If you’re like me and are most familiar with Perl Compatible Regular Expressions (PCRE) (for yours truly, the Python library re in particular), it won’t be too much of a stretch to understand Emacs regexps. My recommendation is to jump in! The reward will be a whole universe of Emacs regexp commands that will become available to you to use at your discretion.

References

emacs

 

AboutMastodonInstagramGitHub

Feeds & TagsGet Captee for macOS

Powered by Pelican