Boost Your Emacs Setup: Implement AI in Spacemacs with Fabric


Last weekend, I decided to tackle a project that had been on my mind for a while: integrating AI capabilities directly into my Spacemacs environment. The result was Fabric.el, a wrapper around Fabric that supercharges text work with AI-driven patterns. Here’s how I did it.
The Idea
The idea was simple: bring the power of AI directly into Spacemacs. Having tried various Chat GPT integrations, none of which seemed to align with the way I wanted to use LLMs, I was very glad that I came across fabric I wanted to make it easy to apply AI patterns to text, whether it was an entire buffer or just a selected region. The goal was to enhance workflow efficiency by making advanced AI tools more accessible.
Getting Started
I started by cloning installing the Fabric AI repository. Fabric AI is an impressive tool that allows you to apply various AI patterns to text. My task was to create a seamless integration with Spacemacs, so users could leverage these capabilities without leaving their editor.
git clone https://github.com/danielmiessler/fabric.git
With the Fabric AI code in hand, I began writing the wrapper. It is just a simple wrapper around the CLI interface provided by fabric, you can clone it from here and them simply load the lisp file into your configuration:
(load "/path/to/fabric.el/fabric.el")
Key Features
The core features of Fabric.el are straightforward but powerful:
AI Integration: Directly integrates with Fabric AI, bringing advanced AI capabilities to your fingertips.
Effortless Pattern Application: Apply AI patterns to your entire buffer or just a selected region with ease.
Workflow Efficiency: Custom keybindings designed for Spacemacs enhance your workflow, making AI tools more accessible than ever.
Building the Integration
The integration itself was built around a few key functions. The most important were fabric-run-pattern-on-buffer
and fabric-run-pattern-on-region
. These functions allow users to apply AI patterns to either the entire buffer or a specific region of text.
Here’s a simplified version of what these functions look like:
(defun fabric-run-pattern-on-buffer ()
(interactive)
(let ((content (buffer-string)))
(fabric-apply-pattern content)))
(defun fabric-run-pattern-on-region (start end)
(interactive "r")
(let ((content (buffer-substring start end)))
(fabric-apply-pattern content)))
These functions call fabric-apply-pattern
, which sends the text to Fabric AI and returns the transformed content. The transformed content is then displayed in a separate buffer, ensuring that the original text remains untouched.
Example Use-Case
Imagine you’re working on a large text file and want to enhance its content using an AI pattern. With Fabric.el, you can simply write your prompt into a buffer or highlight a region you want to send to the LLM. Then, use fabric-run-pattern-on-buffer
or fabric-run-pattern-on-region
to apply the selected pattern (or use the keybindings). Fabric AI transforms your content, all within your Spacemacs environment. One of my favorite use-cases so far is writing email and working with daily text messages and other common text modalities.
Contributing
One of the best parts of working on open-source projects is the community. I welcome contributions from everyone! If you’re interested in improving Fabric.el or adding new features, please fork the repository, make your changes, and submit a pull request. Let’s make Fabric.el even better together.
Final Thoughts
Building Fabric.el over a weekend was a rewarding experience. It’s amazing how much you can accomplish with a clear goal and a bit of determination. If you’re a Spacemacs user looking to enhance your workflow with AI, give Fabric.el a try. It might just take your productivity to new heights.
Subscribe to my newsletter
Read articles from Daniel Alves Rosel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
