How do I use Cursor's AI to generate code from comments?

Updated October 2026 · How we answer

Short answerWrite a clear comment describing what you want, then trigger Cursor's inline AI (Cmd/Ctrl+K) or Composer (Cmd/Ctrl+I) to generate code that matches the comment.

Using Inline AI (Cmd/Ctrl+K)

Place your cursor on a new line where you want the code, or select an existing comment. Press Cmd+K on Mac or Ctrl+K on Windows/Linux to open the inline prompt bar.

Type a natural-language instruction like 'create a function that validates email addresses' and press Enter. Cursor will generate code directly in the editor, which you can accept with Tab or reject with Esc.

You can also write the comment first (e.g., // function to calculate factorial) and then press Cmd/Ctrl+K with the cursor on that line. Cursor will use the comment as context to generate the code.

Using Composer (Cmd/Ctrl+I)

For larger tasks or multiple files, open Composer with Cmd+I (Mac) or Ctrl+I (Windows/Linux). Type a detailed comment or instruction, such as 'add a user authentication flow with JWT', and Composer will generate code across your project.

Composer works best when you provide context about the files involved. You can reference specific files with @ mentions, like @src/auth.js, to guide the AI.

After Composer generates code, review the diff and accept or reject changes. You can also refine the request in the same Composer session.

Tips for Better Results

Be specific in your comments: include input/output types, edge cases, and any libraries you want to use. For example, '// using axios, fetch user data from /api/users and return JSON'.

Cursor uses your open files and project context, so keep relevant files open. You can also use @ symbols to reference files, folders, or docs.

If the generated code isn't right, edit your comment and try again, or use the inline prompt to ask for adjustments like 'make it async'.

  • Write comments as if explaining to a junior developer.
  • Use Cmd/Ctrl+K for quick, single-function generation.
  • Use Cmd/Ctrl+I (Composer) for multi-file or complex features.
  • Reference files with @ to give the AI more context.
  • Review generated code for security and logic errors.

Common mistakes

  • Assuming Cursor will perfectly understand vague comments like 'fix this' without additional context.
  • Forgetting to select the relevant code or place the cursor correctly before triggering inline AI.
  • Expecting Composer to work without any project context, such as when no files are open.
From our shopsCaseMorph: Type an idea, see a custom phone case in seconds, then print a one-of-one.