Beginner15 min1 prerequisite

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:

Terminal
┌─────────────────────────────────────┐
 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:

Terminal
Cmd+K  "Create a React component for a user profile card"

Cursor generates:

Terminal
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:

Terminal
// 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:

Terminal
// Select this code:
function getData() {
  const response = fetch('/api/data')
  return response.json()
}

// Cmd+K  "Make this async and add error handling"

Result:

Terminal
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

Terminal
// 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

Terminal
# Select buggy code
def get_average(numbers):
    return sum(numbers) / len(numbers)

# Cmd+K  "Handle empty list case"

Converting Code

Terminal
// 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

Terminal
 "Make this better"
 "Add input validation and TypeScript types"

 "Fix this"
 "Fix the null pointer exception when user is undefined"

Include Constraints

Terminal
"Convert to TypeScript using strict mode"
"Refactor using only ES6 features"
"Add tests using Jest"
"Optimize for performance, avoid unnecessary re-renders"

Reference Context

Terminal
"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:

Terminal
- function old() {
+ async function new() {
-   return data
+   try {
+     return await fetchData()
+   } catch (e) {
+     handleError(e)
+   }
  }

Accepting or Rejecting

  • Accept: Press Cmd+Enter or click "Accept"
  • Reject: Press Escape or click "Reject"
  • Edit further: Type in the prompt again for iterations

Partial Acceptance

Sometimes you want only part of the suggestion. You can:

  1. Accept the full change
  2. Manually revert parts you don't want
  3. Use Cmd+Z to undo specific sections

Multi-Cursor Editing

Inline editing works with multiple cursors:

  1. Place cursors in multiple locations (Cmd+D to select next occurrence)
  2. Press Cmd+K
  3. Enter your prompt
  4. Changes apply to all cursor positions

Combining with Tab

A powerful workflow:

  1. Use Cmd+K to generate a code skeleton
  2. Accept the generation
  3. Use Tab completion to fill in details
  4. Use Cmd+K again to refine specific parts

Common Use Cases

Adding Documentation

Terminal
// Select a function
export function processOrder(order: Order): ProcessedOrder {
  // ...implementation
}

// Cmd+K  "Add JSDoc documentation"

Type Conversions

Terminal
# Select code
data = {"name": "John", "age": 30}

# Cmd+K  "Convert to TypedDict with type hints"

Test Generation

Terminal
// Select a function
function validateEmail(email) {
  return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)
}

// Cmd+K  "Generate Jest tests for this function"

Accessibility Improvements

Terminal
// 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:

  1. Open Settings → Keyboard Shortcuts
  2. Search for "inline edit"
  3. 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:

  1. Be more specific in your prompt
  2. Select more context (surrounding code)
  3. Try rephrasing your request
  4. Break complex changes into steps

Large Changes Are Incomplete

For big refactors:

  1. Use Composer instead (designed for multi-file changes)
  2. Break into smaller inline edits
  3. 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.

Mark this lesson as complete to track your progress