5.7 KiB
AI Assistant Guide (opencode.nvim)
This guide covers using opencode.nvim for AI-assisted coding in Neovim.
Overview
opencode.nvim provides AI assistance directly in Neovim:
- Ask questions about code
- Get explanations
- Generate code and tests
- Refactor and optimize
- Review changes
- Document code
Quick Start
Open AI Panel
<leader>kk Toggle opencode panel
The panel opens on the right side (40% width).
Ask a Question
<leader>ka Ask opencode (prompts for input)
Or with file context:
<leader>kA Ask about current file
Key Bindings
General
| Key | Mode | Action |
|---|---|---|
<leader>kk |
n | Toggle opencode panel |
<leader>ka |
n, v | Ask opencode |
<leader>kA |
n, v | Ask about current file |
<leader>kn |
n | New session |
Code Understanding
| Key | Mode | Action |
|---|---|---|
<leader>ke |
n | Explain code near cursor |
<leader>kE |
v | Explain selection |
Code Review
| Key | Mode | Action |
|---|---|---|
<leader>kr |
n | Review current file |
<leader>kg |
n | Review git diff |
<leader>kf |
n | Fix diagnostics/errors |
<leader>kq |
n | Fix quickfix items |
Code Generation
| Key | Mode | Action |
|---|---|---|
<leader>ko |
v | Optimize selection |
<leader>kR |
v | Refactor selection |
<leader>kd |
v | Document selection |
<leader>kt |
v | Generate tests for selection |
Harpoon Integration
| Key | Mode | Action |
|---|---|---|
<leader>kh |
n | Ask about harpooned files |
<leader>kH |
n | Analyze harpooned files |
Common Workflows
Understanding Code
- Place cursor on complex code
<leader>ke- Explain code near cursor
Or for larger sections:
- Select code in visual mode (
V) <leader>kE- Explain selection
Fixing Bugs
- See error in diagnostics
<leader>kf- Ask AI to fix- Review suggested fix
- Apply or modify as needed
Generating Tests
- Select function to test (
Vfor lines) <leader>kt- Generate tests- Review generated tests
- Adjust assertions as needed
Refactoring
- Select code to refactor (
V) <leader>kR- Request refactoring- Describe what you want (or let AI decide)
- Review and apply
Code Review
Before committing:
<leader>kg Review git diff
AI will analyze your changes and suggest improvements.
Documenting Code
- Select function/class (
V) <leader>kd- Generate documentation- Review generated docs
- Integrate into code
Working with Multiple Files
Harpoon Context
Mark related files with harpoon, then ask about them together:
- Mark files:
<leader>Hon each file <leader>kh- Ask about all harpooned files- AI has context of all marked files
Example question:
"How does the user service interact with the database layer?"
Analyze Architecture
<leader>kH Analyze harpooned files
Good for understanding relationships between files.
Effective Prompting
Be Specific
Bad: "Fix this code" Good: "This function throws an error when the array is empty. Add a guard clause."
Provide Context
Bad: "Add error handling" Good: "Add try-catch with proper error logging following our project's error handling pattern"
Ask for Explanations
- "Explain why this regex works"
- "What are the edge cases here?"
- "Why might this be slow?"
Request Alternatives
- "Show me 3 ways to implement this"
- "What's a more functional approach?"
- "How would you do this idiomatically in Go?"
Session Management
New Session
<leader>kn Start fresh session
Clears context for a new topic.
Continuing Conversations
The panel maintains conversation history. Follow up naturally:
- Ask initial question
- "Can you also handle the null case?"
- "Now add tests for this"
Integration with Workflow
During Development
- Write initial implementation
<leader>ke- Understand any complex parts<leader>kt- Generate tests<leader>ko- Optimize if needed
During Code Review
<leader>gd- Open diffview<leader>kg- AI review of changes- Address suggestions
- Create PR
While Debugging
- Hit an error
<leader>kf- Ask AI to fix- Or
<leader>ke- Understand the error first - Apply fix
Tips
Quick Explanations
K (hover) shows LSP docs, but for deeper understanding:
<leader>ke AI explanation with context
Iterative Refinement
- Get initial suggestion
- "Make it more concise"
- "Add error handling"
- "Use async/await instead"
Learning Patterns
Ask AI to explain patterns:
- "Explain the repository pattern used here"
- "What design pattern would improve this?"
- "Show me the idiomatic way in this language"
Code Generation Templates
For repetitive tasks:
<leader>ka- "Generate a CRUD API endpoint for User model following the pattern in user.controller.ts"
Customization
Panel Position
Configured in lua/plugins/opencode.lua:
opts = {
window = {
position = "right", -- or "left", "top", "bottom"
width = 0.4, -- 40% of screen
},
}
Auto-reload
When AI edits files, they auto-reload:
auto_reload_buffers_on_edit = true,
Troubleshooting
Panel Not Opening
- Check opencode is installed:
:Lazy - Verify configuration in
lua/plugins/opencode.lua
Slow Responses
- Large files take longer
- Consider selecting specific sections instead of whole files
Context Issues
If AI seems confused:
<leader>kn- New session- Provide clearer context
- Select specific code rather than asking about whole file
Not Understanding Codebase
Use harpoon to provide multi-file context:
- Mark relevant files
<leader>kh- Include them in question