Skip to main content

Emacs Package

The khoj.el Emacs package brings the power of Khoj directly into your Emacs workflow. Search and chat with your org-mode notes, markdown files, and more without leaving your editor.

Khoj Search in Emacs

Khoj Chat in Emacs

Package Information

MELPA Stable

MELPA Stable

MELPA

MELPA

Features

Chat

  • Faster answers: Find answers quickly from your private notes or the public internet
  • Assisted creativity: Smoothly weave across retrieving answers and generating content
  • Iterative discovery: Iteratively explore and re-discover your notes

Search

  • Natural language: Advanced natural language understanding using Transformer-based ML models
  • Incremental search: Fast, search-as-you-type experience
  • Org-mode integration: Seamlessly works with org-agenda files

Installation

Prerequisites

Requires Emacs 27.1 or higher

Generate API Key

1

Get API Key

Generate an API key from the Khoj Web App Settings.
2

Keep It Handy

Copy the API key - you’ll need it for the configuration step below.

Installation Methods

Install directly from MELPA:
M-x package-install RET khoj RET
Then add to your config file (~/.emacs.d/init.el):
;; Set your Khoj API key
(setq khoj-api-key "YOUR_KHOJ_CLOUD_API_KEY")
(setq khoj-server-url "https://app.khoj.dev")
By default, Khoj will index your org-agenda files automatically.

Self-Hosted Configuration

If you’re running your own Khoj server, modify the configuration accordingly.
(setq khoj-server-url "http://127.0.0.1:42110")

Usage

Opening Khoj Menu

Khoj Emacs Menu Access the Khoj menu:
C-c s  or  M-x khoj
From the menu:
  • Press t to cycle through content types
  • Press n twice, then enter the number of results you want
  • Press -f u to force update the content index
Perform natural language search across your notes:
1

Open Search

Press C-c s s or run M-x khoj RET s
2

Enter Query

Type your query in natural language:
What is the meaning of life?
3

View Results

Khoj displays relevant entries from your indexed files.

Advanced Search with Filters

Add query filters for more precise results:
What is the meaning of life? -"god" +"philosophy" dt:"last week"
Learn more about query filters in the Query Filters documentation.

Chat

Chat with Khoj directly from Emacs:
1

Open Chat

Press C-c s c or run M-x khoj RET c
2

Ask Questions

Ask questions in a natural, conversational style:
When did I file my taxes last year?
Summarize my work notes from this month
3

Iterate

Continue the conversation with follow-up questions and refinements.
See the Chat Features documentation for advanced chat capabilities.

Find Similar Entries

Discover related content based on your current position:
1

Position Cursor

Move your cursor to an org-mode entry, markdown section, or text paragraph.
2

Run Command

Press C-c s f or run M-x khoj RET f
3

Browse Results

Khoj displays similar entries from your knowledge base.

Opening Search Results

Navigate directly to source files from search results:
C-c C-o 2
This:
  1. Calls org-open-at-point on the current entry
  2. Opens the second link (the org-id or heading text)
  3. Takes you directly to the entry in its source file
If you have speed keys enabled, o 2 also works.

Configuration Options

Index Configuration

(setq khoj-index-directories 
      '("~/org-roam" 
        "~/notes" 
        "~/Documents/research"))

Display Configuration

;; Set image width for generated images
(setq khoj-image-width 156)

;; Set number of search results
(setq khoj-results-count 10)

Server Configuration

;; Server URL
(setq khoj-server-url "https://app.khoj.dev")

;; API Key
(setq khoj-api-key "YOUR_API_KEY")

;; Local server indicator
(setq khoj-server-is-local nil)  ; set to t for local servers

Keyboard Shortcuts

Default key bindings (when using :bind ("C-c s" . 'khoj)):
CommandKeybindingDescription
khojC-c sOpen Khoj menu
SearchC-c s sOpen search
ChatC-c s cOpen chat
Find SimilarC-c s fFind similar entries
Update IndexC-c s uUpdate content index
Customize these bindings by modifying the :bind section in your configuration.

Updating

1

Refresh Package List

M-x package-refresh-contents
2

Reinstall Khoj

M-x package-reinstall RET khoj RET
3

Restart Emacs

Restart Emacs to ensure the new version loads correctly.

Troubleshooting

  1. Verify Emacs version is 27.1 or higher: M-x emacs-version
  2. Check if dependencies are installed: transient, dash
  3. Try refreshing packages: M-x package-refresh-contents
  4. Restart Emacs with --debug-init to see error messages
  1. Verify khoj-server-url is correct
  2. Check khoj-api-key is set properly
  3. Test connection: M-x khoj-server-status
  4. For self-hosted: ensure Khoj server is running
  1. Force update the index: C-c s then -f u
  2. Check that files are being indexed: verify khoj-index-directories and khoj-index-files
  3. Ensure files have content and are readable
  4. Check *Messages* buffer for errors
  1. Check for conflicts: C-h k C-c s
  2. Verify khoj is loaded: M-x khoj-mode
  3. Try calling commands directly: M-x khoj
  4. Check your config for other packages binding C-c s

Supported Content Types

The Emacs package can index and search:

Org Mode

  • .org files
  • org-agenda files
  • org-roam notes

Markdown

  • .md files
  • .markdown files

Other

  • Plain text (.txt)
  • PDF files
  • Images (via OCR)

Tips and Tricks

  • Use specific directories instead of your entire home folder
  • Exclude large directories with many binary files
  • Index only frequently accessed org files explicitly
  • Use query filters to narrow results: +include -exclude dt:"date"
  • Be specific in your queries
  • Use natural language phrasing
  • Try different wordings if first search doesn’t return good results
(setq khoj-index-directories 
      (list org-roam-directory))
This automatically indexes your entire org-roam knowledge base.
(use-package khoj
  :ensure t
  :bind (("C-c k s" . 'khoj-search)
         ("C-c k c" . 'khoj-chat)
         ("C-c k f" . 'khoj-find-similar)))

Next Steps

Chat Features

Explore advanced chat capabilities

Search

Master natural language search

Query Filters

Learn advanced search syntax

Desktop App

Sync additional files and folders

Build docs developers (and LLMs) love