A simple yet powerful tool that lets you process selected text using AI models by pressing configurable keyboard shortcuts. Send selected text to OpenAI's API or a local Ollama instance, get the processed version back, and automatically replace the original text.
Ideal for grammar correction, summarization, translation, code explanation, and more!
- Configurable Shortcuts: Define multiple keyboard shortcuts for different tasks (e.g.,
Ctrl+Efor grammar,Ctrl+Sfor summary). - Multiple Backends: Supports both OpenAI API (GPT-4o, etc.) and local Ollama models (Llama3, Mistral, etc.).
- Custom Prompts: Assign a unique system prompt to each shortcut to tailor the AI's task.
- Works System-Wide: Use it in any Linux application that allows text selection.
- Preserves Meaning (Optional): Default prompt focuses on grammar correction without changing meaning, but you can customize prompts for any text transformation.
- Notifications: Desktop notifications for operation status.
- Error Logging: Logs errors to
~/bettrwrite_errors.log.
- Linux system (tested on Debian/Ubuntu derivatives like Mint, should work on others).
- Required packages:
xclip,xdotool,jq,curl,xbindkeys,libnotify-bin. - If using OpenAI: An OpenAI API key.
- If using Ollama:
- Ollama installed and running (see https://ollama.com).
- Desired Ollama models pulled (e.g.,
ollama pull llama3.2).
-
Clone this repository:
git clone https://github.com/yourusername/bettrwrite.git # Replace with actual repo URL cd bettrwrite
-
Run the installer script:
./install.sh
The script will:
- Install required dependencies if missing (using
sudo apt-get). - Ask if you want to configure OpenAI and/or Ollama.
- Guide you through setting up the API key (for OpenAI) or selecting and validating a local model (for Ollama).
- Create a configuration file at
~/.config/bettrwrite/config.json. - Set up a default shortcut (
Ctrl+E) for grammar correction using your chosen backend. - Configure
xbindkeysto handle the shortcuts and start automatically on login.
- Install required dependencies if missing (using
- Select text in any application.
- Press the configured keyboard shortcut (e.g.,
Ctrl+Efor the default grammar correction). - Wait a moment for the AI model to process the text.
- The selected text will be automatically replaced with the processed version.
You can define your own text processing actions by editing two files:
-
Configuration (
~/.config/bettrwrite/config.json):- Defines the behavior of each shortcut.
- Add new JSON objects to the
shortcutsarray. Each object needs:id: A unique name (string, no spaces) for this shortcut (e.g., "summarize", "translate_es").keys: The key combination string used byxbindkeys(e.g., "control + s", "alt + shift + t").backend:"openai"or"ollama".model: The specific model name (e.g.,"gpt-4o","llama3.2:latest").prompt: The system prompt telling the AI what to do. Important: Instruct the model to output only the desired text, otherwise, you might get conversational fluff pasted back.ollama_options/openai_options: Optional JSON objects for backend-specific parameters (liketemperature). See the respective API docs.
- You can also edit the
settingssection to change the Ollama URL or update the OpenAI key.
Example
shortcutsentry inconfig.jsonfor summarization:{ "id": "summarize_short", "keys": "control + shift + s", "backend": "ollama", "model": "mistral:latest", "prompt": "Summarize the following text into a single short sentence. Output ONLY the summary sentence.", "ollama_options": { "temperature": 0.6 } } -
Keybindings (
~/.xbindkeysrc):- Maps the physical key presses to execute the script with the correct
id. - For each new shortcut defined in
config.json, add a corresponding entry here. The format is:"/path/to/bettrwrite.sh <id>" <keys> - The script path is typically
~/.config/bettrwrite/bettrwrite.sh. <id>must match theidyou defined inconfig.json.<keys>must match thekeysyou defined inconfig.json.
Example entry in
~/.xbindkeysrccorresponding to the summarization example:"~/.config/bettrwrite/bettrwrite.sh summarize_short" control + shift + s - Maps the physical key presses to execute the script with the correct
-
Apply Changes: After editing
~/.xbindkeysrc, you need to reloadxbindkeys:pkill xbindkeys && xbindkeysChanges to
config.jsonare read by the script each time it runs, so no restart is needed for those.
Common Shortcut Ideas:
Control + Alt + C: Explain selected code.Control + T: Translate selected text (specify target language in prompt).Control + Shift + E: Change tone of selected text (e.g., make more formal/casual).Control + Alt + S: Spell check only.
- Check Logs: Look for errors in
~/bettrwrite_errors.log. - xbindkeys Running? Check with
ps aux | grep xbindkeys. If not running, start it withxbindkeys. If it fails, check~/.xbindkeys_errors. - xbindkeys Configuration: Ensure
~/.xbindkeysrchas the correct path tobettrwrite.shand the correctidandkeysmatchingconfig.json. Reloadxbindkeysafter changes (pkill xbindkeys && xbindkeys). - Ollama Issues:
- Is the Ollama server running? (
ollama psorcurl http://localhost:11434/). Start it if necessary. - Does the specified model exist locally? (
ollama list). Pull it if needed (ollama pull <model_name>). - Is the
ollama_base_urlinconfig.jsoncorrect?
- Is the Ollama server running? (
- OpenAI Issues:
- Is the API key correct? (Check
config.jsonorOPENAI_API_KEYenvironment variable). - Test the key directly (see original README example or OpenAI docs).
- Is the API key correct? (Check
- Permissions: Ensure
~/.config/bettrwrite/bettrwrite.shis executable (chmod +x ~/.config/bettrwrite/bettrwrite.sh). - Dependencies: Make sure
jq,curl,xclip,xdotool,libnotify-binare installed.
Run the uninstall.sh script:
./uninstall.sh