Skip to content

Lesson 5 ยท Completions and Inline Edits

Editing Selected Code with AI

Edit a selected block of code with Cursor, learn what happened to the old Ctrl+K inline edit, write tight edit requests and verify that behavior stays the same.

Between a one-line completion and a large agent task lies a very common job: "change this block". You select some code and say what should happen to it. The selection shows the AI where to work, which helps keep the result focused and easy to review.

What happened to inline edit

For a long time, Ctrl+K (Cmd+K on a Mac) opened inline edit: a small box where you typed an instruction and the change appeared in place. A similar prompt bar existed in the terminal. Many older videos and articles show it.

According to the official IDE release notes, version 3.28 retired inline edit and the terminal prompt bar. Now Ctrl+K adds your selection to an agent chat, as Ctrl+L does, and Ctrl+Shift+K does the same in the editor. Custom keys that were bound to the old commands follow the new behavior. Official pages differ slightly on whether a given key opens a new chat or uses the current one, so check the shortcut list in your version. The Agents Window documentation also says that Ctrl+K opens its command palette, so what the key does can depend on where you are and whether code is selected.

If a guide tells you to press Ctrl+K and type in a box above your code, it describes an older version. Some older official pages may still describe it too. The working idea is the same: select first, then give a precise instruction.

The selection workflow

  1. Select only the code you want changed.
  2. Add it to a chat with the shortcut. The shortcut list also includes Ctrl+Shift+L for adding selected code as context.
  3. Write one instruction about that selection.
  4. Read the diff.
  5. Run the checks.

You can also copy code and paste it into the chat input. The shortcut list describes this as adding copied reference code as context.

Write a tight edit request

When you edit a selection, say what must change and what must stay.

TEXT
Rewrite only the selected function so it uses early returns
instead of nested if statements.
Keep the name, parameters, return values and error messages exactly.
Do not touch any other function or file.
Kind of edit Useful extra instruction
Refactor "Keep behavior identical"
Add types "Do not change logic or parameter names"
Add comments "Explain why, not what each line does"
Fix a bug "Here is a failing input and the output I expect"
Change style "Follow the style of the function above"

Prove that a refactor kept behavior

A refactor must not change what the code does. A cheap and strong check is to run the old and new versions side by side on many inputs. This is real Python that you can reuse in your own projects.

Python Compare old and new behavior

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The inputs deliberately sit on the boundaries: zero, just below 50 and exactly 50. An agent can write such a comparison for you, but check that the inputs it chose are not all easy ones.

See exactly what changed

Beyond the editor's diff view, you can compare two versions of a function in code with Python's difflib.

Python Print a unified diff

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice a trap: this edit renamed the parameters. Any caller that uses area(w=2, h=3) now fails. "Just adding types" changed the public interface. The instruction should have said "keep parameter names".

When to use which tool

Choose by size

Tab A few lines you can see
Selection One block, one instruction
Agent task Several files or commands
1 / 2

A selection is a pointer, not a fence. Because the request now goes to an agent chat, the agent may read or change other code. If the job grows to several files, treat it as an agent task and plan it first, as described in plan mode.

A worked request

Suppose a selected function reads a price from a form and converts it with float. A weak request is "improve this". It gives no definition of better. A stronger one names the problem, the rules and the proof:

TEXT
Selected function: parse_price(text).
Problem: it accepts "-5" and "nan".
Change: raise ValueError for negative values and for anything
that is not an ordinary decimal number.
Keep: the name, the parameter and the return type.
Show me which inputs you tested.

Notice the last line. It asks for evidence that you can repeat. Then add your own inputs, such as an empty string, spaces around a number and a very large value.

If the diff surprises you

Do not repair a surprising diff by piling on more requests. Reject it, restore the file, and ask yourself whether the request was ambiguous. Often one sentence about what must stay the same prevents the surprise on the next try. If the change really needs to touch other files, accept that it is a bigger task, and give it the planning and review that a bigger task needs.

Common mistakes

  • Selecting too little, so important variables are out of view. Select the whole function.
  • Selecting a whole file for a one-line fix. This invites a bigger diff.
  • Asking for "cleaner" code with no definition. Say what cleaner means to you.
  • Assuming only your selection changed. Check the full diff.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. According to the release notes, what happened to the classic inline edit box?

    Inline edit and the terminal prompt bar were retired; the shortcut now adds the selection to an agent chat.
  2. 2. What is a reliable way to check a refactor?

    Equal behavior on a good set of inputs is far better evidence than appearance.
  3. 3. Why can renaming parameters break code?

    Keyword arguments depend on parameter names, so a rename changes the interface.

Summary

  • Select the code, add it to a chat and give one precise instruction.
  • The old Ctrl+K inline edit was retired according to the official release notes.
  • State what must change and what must stay.
  • Compare behavior before and after a refactor.
  • Review the diff for changes outside your selection.

Next: Ask Mode and Codebase Questions

Your progress is saved in this browser. No account needed.