Lompat ke konten

Pelajaran 3 ยท Getting Started

Your First Cursor Session

Walk through a first Cursor session: explore a project, make one small change, read the diff, run the checks and learn the habits that keep you in control.

Now that the editor is installed, you will complete one round of work: understand a project, change one small thing, review the change and verify it. This loop is the heart of the whole tutorial.

The official quickstart suggests a similar order, starting with questions about the codebase before any editing. The prompts below are our own.

The loop

flowchart LR
  A["Understand"] --> B["Ask for a small change"]
  B --> C["Review the diff"]
  C --> D["Run the checks"]
  D --> E["Commit"]
  D -->|"fails"| B
  C -->|"wrong"| B

One round of work. A failed check or a wrong diff sends you back with better instructions.

Step 1: ask before you change

Open your practice folder. Open the agent panel with Ctrl+I (Cmd+I on a Mac). Press Shift+Tab or use the mode picker until you are in Ask mode, which reads your code but does not edit it. Then type something like this:

TEXT
Explain what this project does. List each file, say what it is for,
and tell me which one I should read first. Do not change anything.

Read the answer next to the real files. Check at least two claims yourself. An answer that sounds right can still describe a function wrongly.

Step 2: ask for one small change

Switch to Agent mode. Give one clear goal, name the files and say what "done" means:

TEXT
In shop/cart.py, make total() reject negative prices with a ValueError.
The message should include the bad value.
Add tests for it in tests/test_cart.py using unittest.
Do not change other behavior. Run the tests when you finish.

Notice the four parts: goal, place, limits and check. This small template works for most tasks.

Part Weak Better
Goal "Improve the cart" "Reject negative prices"
Place (none) "In shop/cart.py"
Limits (none) "Do not change other behavior"
Check (none) "Run the tests"

The anatomy of a prompt goes deeper into writing good requests.

Step 3: review the diff

The editor shows what changed as removed and added lines. In the terminal you can run git diff. Read every line and ask:

  • Is every change related to my request?
  • Did it edit a test only so that the test would pass?
  • Did it add a dependency, a file or a setting I did not ask for?

Here is a change like the one you asked for, written by hand for this lesson. It is not output from the product. Run it and check the behavior.

Python The change you want, with its tests

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.

Look at the last line of output. It should be 10.0, but it is 0.0. If prices is a generator, the loop consumes it, and sum then sees nothing. All the tests pass anyway. This is why review means thinking about inputs that the tests do not cover. The fix is one line at the top of the function: prices = list(prices). Add it and run the code again.

Step 4: run the checks yourself

Even if the agent says the tests pass, run them yourself, at least while you are learning how much to trust it:

Terminal
python -m unittest discover -s tests -t .
git status
git diff

This assumes that tests contains an empty __init__.py file. git status also shows new files that you might not expect.

Step 5: commit or undo

If the change is good, commit it with a message that explains why. If it is not, you can ask for a correction, or undo it and write a better request. When the direction is wrong, starting again with clearer instructions is often faster than adding many corrections.

Terminal
git add shop tests
git commit -m "Reject negative prices in cart total"
# or, to throw away uncommitted edits to tracked files:
git restore .

git restore . does not delete new files that Git does not track yet. Check git status after it.

The agent also takes its own snapshots, called checkpoints. You learn about them in how the agent works. They help, but Git remains your real history.

Habits to start now

  • One task, one chat. Start a new chat when you switch topics.
  • Small tasks. If a diff is too big to review carefully, split the task.
  • State the check. A task with a clear test is easier to verify.

What to do when the result is wrong

A wrong result is normal, and it is useful information. Before you try again, work out which kind of problem you have:

Symptom Likely cause What to change
Edited the wrong place The request did not name a file Name or attach the file
Ignored your style No example to follow Point to a good example
Changed too much No limits stated Say what must not change
Solved a different problem The goal was vague Give an input and the expected output
Claims success, still broken No real check Name the exact command to run

Then pick one change to your request and try again from a clean state. If you change five things at once, you do not learn which one helped. If two or three tries fail, stop and think. The task may need to be smaller, or you may need to understand the code better first. Writing the first part yourself often shows what the agent was missing.

Keep a short log

For your first weeks, note each task in a few lines: what you asked, what went wrong and what fixed it. Patterns appear quickly. The things you repeat in many requests, such as your test command, are good candidates for project rules.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which mode should you use to explore a project without edits?

    Ask mode is documented as read-only: it explores and answers without editing.
  2. 2. Which request is best?

    It names the goal, the place and the limits, and asks for a check.
  3. 3. The agent says all tests pass. What should you still do?

    Passing tests only prove what they test. The generator example passed its tests and was still wrong.
  4. 4. What does git restore . leave behind?

    It restores tracked files. Untracked files stay, so check git status.

Summary

  • Work in a loop: understand, request, review, verify, commit.
  • A good request names a goal, a place, limits and a check.
  • Read the whole diff and think about untested inputs.
  • Use Git as your safety net.

Next: Cursor Tab Completion

Progres Anda disimpan di browser ini. Tidak perlu akun.