Cmd+K Inline Edit - Surgical Code Changes
Make precise AI-powered edits without leaving your code
💡 What You'll Learn
- Use Cmd+K (Ctrl+K) for instant code modifications
- Write effective edit prompts for best results
- When to use inline edit vs. Chat vs. Tab completion
- Advanced techniques for refactoring and debugging
🎯 What is Cmd+K Inline Edit?
Cmd+K (Ctrl+K on Windows/Linux) is Cursor's most precise tool. It lets you select code and describe how to change it in plain English.
🔑 The Magic Formula:
- Select the code you want to change
- Press
Cmd+K - Describe what you want in natural language
- Press Enter and watch the magic happen ✨
⌨️ Essential Shortcuts
| Action | Mac | Windows/Linux |
|---|---|---|
| Open inline edit | Cmd+K | Ctrl+K |
| Accept changes | Cmd+Enter | Ctrl+Enter |
| Reject changes | Cmd+Backspace | Ctrl+Backspace |
| Cancel | Esc | Esc |
🎯 Real-World Examples
Example 1: Quick Refactoring
return a + b;
}
return a + b;
}
Example 2: Add Error Handling
const response = await fetch(url);
if (!response.ok) throw new Error('Failed to fetch');
const data = await response.json();
} catch (error) {
console.error('Fetch error:', error);
throw error;
}
Example 3: Convert to Modern Syntax
if (err) console.error(err);
console.log(data);
});
const data = await fs.promises.readFile('data.txt');
console.log(data);
} catch (err) {
console.error(err);
}
✍️ Writing Effective Prompts
✅ Good Prompts
- "add TypeScript types"
- "convert to arrow function"
- "add error handling"
- "optimize this loop"
- "add JSDoc comments"
- "make this function async"
- "extract to separate function"
❌ Bad Prompts
- "fix" (too vague)
- "make it better" (unclear)
- "change stuff" (no direction)
- Very long explanations (be concise)
- Multiple unrelated changes
🔄 Cmd+K vs. Chat vs. Tab
| Use Case | Best Tool |
|---|---|
| Modify existing code | Cmd+K ⭐ |
| Write new code | Tab |
| Ask questions | Chat (Cmd+L) |
| Multi-file changes | Composer |
| Quick refactoring | Cmd+K ⭐ |
| Debug issues | Chat (Cmd+L) |
⚡ Pro Tips
💡 Tip 1: Select more context than needed - the AI uses surrounding code for better results
💡 Tip 2: Use Cmd+K for refactoring, Chat for understanding
💡 Tip 3: Chain multiple Cmd+K edits: accept first change, then Cmd+K again on the result
💡 Tip 4: If you don't like the result, press Cmd+Z to undo and try a different prompt
🎓 Practice Exercise
Try These Challenges:
- Take any function and use Cmd+K to "add TypeScript types"
- Select a fetch call and use Cmd+K to "add error handling"
- Choose a regular function and Cmd+K to "convert to arrow function"
- Pick undocumented code and Cmd+K to "add JSDoc comments"
🐛 Common Issues & Solutions
AI makes wrong changes
Select more surrounding code for context, or be more specific in your prompt
Changes are too aggressive
Use phrases like "only add" or "minimal changes" in your prompt
Cmd+K doesn't work
Make sure you've selected some code first, or check if Cursor is indexed
🔗 Next Steps
Master the AI Chat interface for asking questions and debugging: Cmd+L AI Chat Tutorial →