3.4 KiB
Cursor Autocompletion for Neovim
This setup integrates Cursor's AI-powered autocompletion into Neovim using cursor-agent CLI and nvim-cmp.
Features
- AI-powered completions: Get intelligent code completions powered by Cursor's AI
- Context-aware: Uses surrounding code context (last 30 lines) for better suggestions
- Integrated with nvim-cmp: Works seamlessly with other completion sources (LSP, snippets, buffer, etc.)
- Prioritized suggestions: Cursor completions appear at the top of the completion menu
Setup
The integration is already configured! Just make sure:
-
cursor-agent CLI is installed and authenticated:
cursor-agent statusIf not authenticated, run:
cursor-agent login -
Restart Neovim to load the new configuration
-
Install plugins (if using lazy.nvim, they'll auto-install):
:Lazy sync
Usage
- Start typing in any file
- Press
<C-Space>to trigger completion manually, or wait for automatic triggers - Look for
[Cursor]in the completion menu to see AI suggestions - Navigate with
<Tab>/<S-Tab>and accept with<Enter>
Configuration
Keybindings
Default keybindings (can be customized in lua/config/plugin/cmp.lua):
<C-Space>- Trigger completion<Tab>- Next completion item / Expand snippet<S-Tab>- Previous completion item / Jump snippet backward<Enter>- Confirm completion<C-e>- Close completion menu
Adjusting Completion Behavior
Edit lua/plugins/cmp-cursor.lua to customize:
- Context window: Change
line_num - 30to adjust how many lines of context are sent - Minimum trigger length: Change
#before_cursor:gsub('%s+', '') < 3to require more/less context - Timeout: Change
8000(8 seconds) to adjust how long to wait for completions
Disabling Cursor Completions
To temporarily disable Cursor completions, comment out the cursor source in lua/config/plugin/cmp.lua:
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
-- { name = 'cursor', source = cursor_completion_source }, -- Disabled
{ name = 'luasnip' },
}, {
How It Works
-
When you trigger completion, the plugin captures:
- Current line up to cursor position
- Last 30 lines of context
- File content
-
It sends a prompt to
cursor-agent --printasking for completion suggestions -
The AI response is parsed and formatted as completion items
-
These appear in the nvim-cmp completion menu with
[Cursor]label
Troubleshooting
No Cursor completions appearing:
- Check
cursor-agent statusto ensure you're authenticated - Verify
cursor-agentis in your PATH:which cursor-agent - Check Neovim messages:
:messagesfor any errors
Completions are slow:
- This is expected - AI completions take 2-8 seconds
- Consider increasing the minimum trigger length to reduce API calls
- The timeout is set to 8 seconds
Completions don't make sense:
- The AI uses context from your file - ensure you have meaningful code before the cursor
- Try typing more context before triggering completion
Notes
- Cursor completions use API credits from your Cursor account
- Completions are generated on-demand, not cached
- The integration works best with code files (not plain text)
- For best results, ensure your code is syntactically correct before requesting completions