- Learn
- AI Code Editors
- Cursor
- Inline Editing with Cmd+K
Learn to use Cursor's inline editing feature to modify, generate, and transform code directly in your editor.
Inline Editing with Cmd+K
While Tab completion suggests code as you type, inline editing lets you describe changes you want to make to existing code. Press Cmd+K (Mac) or Ctrl+K (Windows/Linux) to open the inline prompt.
How Inline Editing Works
Opening the Prompt
There are two ways to use inline editing:
Without selection: Place your cursor where you want to generate code, then press Cmd+K. An input field appears where you can describe what you want.
With selection: Select code first, then press Cmd+K. The AI will modify the selected code based on your instructions.
The Inline Dialog
When you trigger inline editing, a small input box appears:
┌─────────────────────────────────────┐
│ Describe changes or generate code │
└─────────────────────────────────────┘
Type your instruction in natural language, then press Enter.
Generating New Code
Basic Generation
Without selecting anything, ask for new code:
Cmd+K → "Create a React component for a user profile card"
Cursor generates:
interface UserProfileCardProps {
name: string
email: string
avatar: string
}
export function UserProfileCard({ name, email, avatar }: UserProfileCardProps) {
return (
<div className="rounded-lg bg-white p-6 shadow-md">
<img
src={avatar}
alt={name}
className="h-16 w-16 rounded-full"
/>
<h2 className="mt-4 text-xl font-semibold">{name}</h2>
<p className="text-gray-600">{email}</p>
</div>
)
}
Contextual Generation
Cursor considers your file context:
// In a file with existing User type
Cmd+K → "Add a function to validate user data"
Cursor generates code that uses your existing types and conventions.
Modifying Existing Code
Refactoring
Select code and describe the refactor:
// Select this code:
function getData() {
const response = fetch('/api/data')
return response.json()
}
// Cmd+K → "Make this async and add error handling"
Result:
async function getData() {
try {
const response = await fetch('/api/data')
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
return await response.json()
} catch (error) {
console.error('Failed to fetch data:', error)
throw error
}
}
Adding Features
// Select a function
function calculateTotal(items: Item[]) {
return items.reduce((sum, item) => sum + item.price, 0)
}
// Cmd+K → "Add discount support with optional discount percentage"
Fixing Bugs
# Select buggy code
def get_average(numbers):
return sum(numbers) / len(numbers)
# Cmd+K → "Handle empty list case"
Converting Code
// Select callback-based code
fs.readFile('data.txt', (err, data) => {
if (err) throw err
console.log(data)
})
// Cmd+K → "Convert to promise-based with async/await"
Effective Prompts
Be Specific
❌ "Make this better"
✅ "Add input validation and TypeScript types"
❌ "Fix this"
✅ "Fix the null pointer exception when user is undefined"
Include Constraints
"Convert to TypeScript using strict mode"
"Refactor using only ES6 features"
"Add tests using Jest"
"Optimize for performance, avoid unnecessary re-renders"
Reference Context
"Match the style of the other functions in this file"
"Use the ErrorBoundary component from our shared library"
"Follow the pattern used in UserService"
Reviewing Changes
The Diff View
After generating code, Cursor shows a diff:
- function old() {
+ async function new() {
- return data
+ try {
+ return await fetchData()
+ } catch (e) {
+ handleError(e)
+ }
}
Accepting or Rejecting
- Accept: Press
Cmd+Enteror click "Accept" - Reject: Press
Escapeor click "Reject" - Edit further: Type in the prompt again for iterations
Partial Acceptance
Sometimes you want only part of the suggestion. You can:
- Accept the full change
- Manually revert parts you don't want
- Use
Cmd+Zto undo specific sections
Multi-Cursor Editing
Inline editing works with multiple cursors:
- Place cursors in multiple locations (
Cmd+Dto select next occurrence) - Press
Cmd+K - Enter your prompt
- Changes apply to all cursor positions
Combining with Tab
A powerful workflow:
- Use
Cmd+Kto generate a code skeleton - Accept the generation
- Use Tab completion to fill in details
- Use
Cmd+Kagain to refine specific parts
Common Use Cases
Adding Documentation
// Select a function
export function processOrder(order: Order): ProcessedOrder {
// ...implementation
}
// Cmd+K → "Add JSDoc documentation"
Type Conversions
# Select code
data = {"name": "John", "age": 30}
# Cmd+K → "Convert to TypedDict with type hints"
Test Generation
// Select a function
function validateEmail(email) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)
}
// Cmd+K → "Generate Jest tests for this function"
Accessibility Improvements
// Select a component
<button onClick={handleClick}>
<img src="icon.png" />
</button>
// Cmd+K → "Add ARIA attributes and keyboard handling"
Settings and Customization
Keybinding
Change the default shortcut:
- Open Settings → Keyboard Shortcuts
- Search for "inline edit"
- Assign your preferred keybinding
Model Selection
For inline editing, you can choose different AI models:
- Faster models for simple edits
- More powerful models for complex refactors
Troubleshooting
Changes Don't Make Sense
If the AI misunderstands:
- Be more specific in your prompt
- Select more context (surrounding code)
- Try rephrasing your request
- Break complex changes into steps
Large Changes Are Incomplete
For big refactors:
- Use Composer instead (designed for multi-file changes)
- Break into smaller inline edits
- Increase the max tokens in settings
Summary
- Cmd+K opens inline editing at your cursor
- Without selection: Generate new code
- With selection: Modify existing code
- Be specific in your prompts for better results
- Review diffs before accepting changes
- Iterate by prompting again if needed
Next Steps
Inline editing is great for quick changes. For conversations about your code and more complex assistance, the Chat panel offers a richer experience.