Vibe coding with AI course Β· Module 1: Cursor AI - First Steps
Ctrl+K - Editing Selected Code with AI
In this lesson8
Welcome back! Now I'll show you the most important shortcut in Cursor: Ctrl+K (or Cmd+K on Mac).
What is Ctrl+K?
In Cursor 3, Ctrl+K (just like Ctrl+L) adds the selected code to a new chat with the AI Agent. The old in-place editing (inline edit) has been replaced by exactly this workflow. Instead of writing code by hand, you describe what you want to change, AI changes the code, and you review the changes and accept or reject them.
How to Use Ctrl+K - Step by Step
Method 1: Editing selected code
- Select the code you want to change
- Press Ctrl+K (or Cmd+K on Mac)
- Describe what you want to change in the chat that opens
- AI will suggest changes
- Accept or Reject
Method 2: Adding new code
- Place your cursor where you want to add code (or select the line it should go under)
- Press Ctrl+K to open the chat
- Describe what you want to add and where
- AI will generate code
- Accept or Reject
Practical Examples
Example 1: Changing color
You have:
<h1>Ahoy Pirates!</h1>You want: Red text color
What you do:
- Select
<h1>Ahoy Pirates!</h1> - Ctrl+K
- Type: "add red text color"
- AI will suggest:
<h1 style="color: red;">Ahoy Pirates!</h1>- Accept!
Example 2: Adding an image
You have:
<h1>Welcome aboard!</h1>You want: Add a pirate image below the heading
What you do:
- Place your cursor at the end of the h1 line
- Ctrl+K
- Type: "add a pirate image from unsplash URL"
- AI will add:
<h1>Welcome aboard!</h1>
<img src="https://images.unsplash.com/photo-pirate" alt="Pirate">- Accept!
Example 3: Refactoring
You have:
<p>Ship</p>
<p>Anchor</p>
<p>Map</p>You want: Change to a list
What you do:
- Select all 3 paragraphs
- Ctrl+K
- Type: "change to ul with li elements"
- AI will suggest:
<ul>
<li>Ship</li>
<li>Anchor</li>
<li>Map</li>
</ul>- Accept!
Selection + Ctrl+K vs a Question in AI Chat - When to Use Which?
Both ways open a chat with AI. The difference is whether you start from a selected piece of code (Ctrl+K) or from a general question in AI Chat (Ctrl+L with nothing selected).
| Situation | Tool | Why |
|---|---|---|
| Quick change in existing code | Ctrl+K | The selected code goes straight into the chat |
| Question about a concept | AI Chat | Longer conversation |
| Generating large new code | AI Chat | More space |
| Editing a specific fragment | Ctrl+K | Precise, fast |
| Learning how something works | AI Chat | Explanations |
| Adding a small feature | Ctrl+K | Quick |
Rule: If you know WHERE in the code you want a change β select that fragment and use Ctrl+K. If you don't know where to start β use AI Chat.
Pro Tips
1. Be specific
"change this" "change background color to black and text to gold"
2. Use technical language (when you know it)
"make a nice button" "add a button with padding 10px and border-radius 5px"
3. Iterate
First Accept β See the result β Ctrl+K again β Refine
4. Experiment
Don't be afraid! You can always:
- Reject - discard the changes
- Ctrl+Z - undo
- Ctrl+K again - improve
Hands-on Exercise
Time for your own experiments! Open Cursor and:
- Create a new file
pirate.html - Type:
<h1>My Ship</h1> - Select that line
- Ctrl+K β "add black background and white text"
- Accept
- Ctrl+K again β "add a 2px red border"
- Accept
Congratulations! You just used Ctrl+K twice in a row!
FAQ
Q: What if AI doesn't understand my instruction? A: Reject and try describing it more specifically. For example, instead of "make it prettier" write "bigger font and centered".
Q: Can I use Ctrl+K on multiple lines? A: Yes! Select as many lines of code as you want.
Q: What if I want to add something at the beginning of the file? A: Press Ctrl+K and say that the change should go at the beginning of the file.
Q: Does Ctrl+K only work with HTML? A: No! It works with any language: JavaScript, Python, CSS, everything!
Summary
Ctrl+K sends the selected code to a chat with AI Use it for quick changes in code You can edit selected code or add new code You can always Reject and try again The more specific you are, the better the result
This is YOUR MAGIC WAND, pirate!
In the next module we'll start building real ships (applications) with Next.js!
But first you need to complete a few exercises to solidify your skills.
See you on the seas of code!
Captain Redbeard out.
Code for this lesson: index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pirate Ship</title>
<style>
body {
background-color: #0f172a;
color: #fef3c7;
font-family: 'Courier New', monospace;
padding: 30px;
}
h1 {
color: #f59e0b;
text-align: center;
font-size: 36px;
}
.ship {
border: 3px solid #dc2626;
background-color: #78350f;
padding: 20px;
margin: 20px 0;
border-radius: 10px;
}
</style>
</head>
<body>
<h1>My Pirate Ship</h1>
<div class="ship">
<h2>Black Pearl</h2>
<p><strong>Captain:</strong> Redbeard</p>
<p><strong>Crew:</strong> 50 pirates</p>
<p><strong>Treasure:</strong> 1000 gold coins</p>
</div>
<p>This is my first ship! Change the name, captain, add more!</p>
</body>
</html>Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What is the Ctrl+K shortcut used for in Cursor?
2. Which shortcut opens AI Chat in Cursor?
3. What do you do when you don't like the changes the AI proposed after Ctrl+K?
4. When is it best to start by selecting code and pressing Ctrl+K instead of asking a general question in AI Chat?
5. Which Ctrl+K instruction is the best?
6. Which languages does Ctrl+K work with?
7. What can you do with AI in Cursor?
Hands-on tasks in the game
- Code editor
Use Cursor to create a simple HTML file
- Vertical ordering
Arrange the steps of editing selected code using Ctrl+K:
- Horizontal ordering
Assemble the shortcut that undoes the last change in the code:
- Vertical ordering
Arrange the steps of using Ctrl+K in the correct order: