Inline Editing with Cmd+K
Inline Editing with Cmd+K
Cursor has several ways to get AI help with your code, and each one is designed for a different kind of task. Chat is for conversations. Tab completion is for fluid, continuous writing. Cmd+K is for surgical, focused edits -- the kind where you know exactly which block of code you want to change and you want to describe the transformation in a single instruction.
This lesson covers Cmd+K in depth: what it does, how to use it with and without a selection, how to read and respond to the diff it produces, and when to reach for Cmd+K instead of the other tools.
What You'll Learn
- What Cmd+K does and how the inline prompt bar works
- How to select code and use Cmd+K to edit or transform it
- How to use Cmd+K without a selection to generate new code in place
- How to read, accept, and reject the inline diff
- Common and high-value uses of Cmd+K
- The key difference between Cmd+K and the Chat panel
What Cmd+K Does
Press Cmd+K (or Ctrl+K on Windows/Linux) and a small prompt bar appears inline in your editor, right where your cursor is. You type a natural language instruction, press Enter, and Cursor edits or generates code directly in the file -- showing you a diff that highlights what changed before you commit to it.
Unlike Chat, which appears in a panel on the side, Cmd+K happens inside the editor itself. The change appears right where the code is, not in a separate window. This keeps your focus on the code rather than switching between panels.
The result is always shown as a diff: additions highlighted in green, deletions in red. You see exactly what Cmd+K wants to change before it becomes permanent. Then you accept or reject.
The Inline Prompt Bar
When you press Cmd+K, a compact input bar appears at the cursor position (or below a selection). It has a text field where you type your instruction and typically offers a few options like the model selector if you want to change which AI is used.
The prompt bar is deliberately minimal. It does not have a conversation history or multi-turn capabilities. You give it one instruction, it makes the edit, and then you decide whether to keep it. If you want to refine the result, you can press Cmd+K again and give a new instruction -- but it is a sequential edit workflow, not a conversation.
Dismissing the Prompt Bar
If you open the prompt bar and decide you do not want to use it, press Escape. The bar closes and your code is unchanged.
Editing Existing Code with Cmd+K
The most powerful way to use Cmd+K is to select a block of code first, then press Cmd+K to describe how you want it transformed.
Step-by-Step
- Select the code you want to edit (one line, a function, a block -- any range)
- Press Cmd+K
- In the prompt bar, type your instruction
- Press Enter
- Review the diff
- Press Tab to accept, Escape to reject
Example: Adding Error Handling
// Select this function:
async function fetchUser(id: string) {
const response = await fetch(`/api/users/${id}`);
const data = await response.json();
return data;
}
// Cmd+K prompt: "add try/catch error handling and throw a descriptive error if the response is not ok"
// Result after Cmd+K:
async function fetchUser(id: string) {
try {
const response = await fetch(`/api/users/${id}`);
if (!response.ok) {
throw new Error(`Failed to fetch user ${id}: ${response.status} ${response.statusText}`);
}
const data = await response.json();
return data;
} catch (error) {
throw new Error(`fetchUser failed: ${error instanceof Error ? error.message : String(error)}`);
}
}
Example: Converting to TypeScript
If you have JavaScript code and want to convert a block to TypeScript:
// Select this:
function processOrder(order) {
return {
id: order.id,
total: order.items.reduce((sum, item) => sum + item.price * item.quantity, 0),
status: order.status || 'pending',
};
}
// Cmd+K prompt: "convert to TypeScript with proper types"
Cursor will generate type definitions and add them to the function signature and return type.
Example: Refactoring for Readability
// Select this nested conditional:
function getDiscount(user) {
if (user.tier === 'premium') { if (user.yearsActive > 2) { return 0.30; } else { return 0.20; } } else { if (user.tier === 'standard') { return 0.10; } else { return 0; } }
}
// Cmd+K prompt: "refactor this to be more readable using early returns"
Using Cmd+K Without a Selection
You do not need to select existing code to use Cmd+K. When you press Cmd+K with nothing selected, the prompt bar still appears -- and now it is a code generation tool rather than an editing tool. Cursor will generate new code and insert it at the cursor position.
This is useful when you want to write a new function, add a new block, or insert something that does not exist yet.
Example: Generating a New Function
Place your cursor at an empty line where you want a function to appear, press Cmd+K, and type:
write a function that validates an email address and returns true/false
Cursor will generate the function and insert it at your cursor position, highlighting it as an addition so you can review it before committing.
Example: Inserting a Complex Expression
If you know you need a specific data transformation but do not want to think through the syntax:
// Cursor is at the position you want the code:
// Cmd+K prompt: "generate a reduce that groups an array of transactions by their currency code"
Accepting and Rejecting the Diff
After Cmd+K runs, your code is in a pending state. The changes are shown with a diff view:
- Green lines: new or modified code being proposed
- Red lines: code being removed or replaced
At this point, nothing is permanent. You are reviewing a proposal.
Accepting: Press Tab
Press Tab (or click the "Accept" button if one appears in the UI) to confirm the change. The diff view disappears and the new code becomes a normal part of your file.
Rejecting: Press Escape
Press Escape (or click "Reject") to discard the proposed change. Your code returns to exactly what it was before you ran Cmd+K.
Partial Acceptance
In some versions of Cursor, you can accept or reject individual hunks within a diff rather than the entire change at once. If the proposed edit touches multiple separate blocks of code, you may be able to accept the parts that look correct and reject the parts that do not.
What If the Result Is Almost Right?
If the result is close but not quite what you wanted, you have two good options:
- Accept and then Cmd+K again: Accept the current change, select the specific part that still needs work, and run Cmd+K with a more precise instruction.
- Reject and rephrase: Reject the change, and run Cmd+K again with a more detailed or different prompt. Sometimes adding more context ("do this without introducing a new dependency" or "keep the original function signature") produces a better result on the second attempt.
Common High-Value Uses of Cmd+K
These are the scenarios where Cmd+K is particularly efficient:
Add Error Handling
Select a function or block that has no error handling and prompt: "add try/catch with descriptive error messages". This is the most common single-operation use of Cmd+K.
Convert Code Between Styles
- "Convert this class component to a functional component with hooks"
- "Convert this callback-based code to async/await"
- "Convert this to use the new array destructuring syntax"
Write a New Function from Scratch
Place your cursor where you want the function, Cmd+K, and describe what it should do. More descriptive prompts produce better results: "write a debounce function that accepts a function and a wait time in milliseconds and returns a debounced version" is better than "write a debounce function".
Add or Update Comments and Documentation
Select a function and prompt: "add a JSDoc comment documenting the parameters, return type, and what this function does". This is much faster than writing documentation manually.
Add Logging
Select a block and prompt: "add console.log statements at key steps that would help debug this in production". Useful when you want diagnostic logging added systematically.
Write Validation Logic
Select an object or function and prompt: "add input validation that throws a descriptive error for missing required fields". Cmd+K handles the mechanical parts of validation so you can focus on the specific rules.
Rename and Restructure
Select a block and prompt: "rename userData to userProfile throughout this function and update related variable names to match". For localized renames within a function, Cmd+K is faster than a full-file find-and-replace.
Cmd+K vs Chat: Understanding the Difference
Both Cmd+K and Chat use the same underlying AI, but they are designed for different workflows:
| Aspect | Cmd+K | Chat (Ask Mode) |
|---|---|---|
| Interface | Inline prompt bar inside the editor | Separate panel on the side |
| Scope | One selection or insertion point | Can span files, ask questions, explore |
| Output | Code edit shown as a diff | Text response with code blocks |
| Conversation | Single-instruction, sequential | Multi-turn conversation |
| Context | Current selection | Current file, selection, plus @mentions |
| Best for | Targeted, surgical code transforms | Exploration, explanation, broader tasks |
The practical rule: use Cmd+K when you know exactly what you want to change and where. Use Chat when you want to think through a problem first, ask questions, or work on something that spans multiple files.
They are not mutually exclusive. A common pattern is to use Chat to understand a problem or plan an approach, and then use Cmd+K to execute specific changes one at a time.
Summary
Cmd+K is Cursor's inline editing tool -- a focused, surgical way to transform or generate code directly in your editor without switching to a panel. You select code, press Cmd+K, describe the change, and review a diff before committing. The key strengths of Cmd+K are its precision, its speed for single targeted changes, and the fact that it keeps your focus in the code rather than in a side panel.
Master the workflow: select, Cmd+K, describe, review diff, Tab to accept or Escape to reject. Once this is muscle memory, you will reach for Cmd+K dozens of times a day.
Key Takeaways
- Cmd+K (Ctrl+K on Windows/Linux) opens an inline prompt bar directly in your editor for immediate code generation or editing
- With a selection: Cmd+K transforms or edits the selected code according to your instruction
- Without a selection: Cmd+K generates new code and inserts it at the cursor position
- All changes are shown as a diff -- green for additions, red for deletions -- before becoming permanent
- Press Tab to accept a Cmd+K change; press Escape to reject it and restore the original
- High-value uses include adding error handling, converting code styles, writing documentation, and generating new functions from a description
- Cmd+K is best for targeted, surgical, single-operation changes; Chat is better for multi-turn exploration and broader tasks
- Cmd+K and Chat are complementary: use Chat to think, Cmd+K to execute
Quiz
Discussion
Sign in to join the discussion.

