Cursor 101

Cursor Tutorial 2 - Are You Really Using the Prompt Bar (Cmd K) Feature Effectively?

The prompt bar (Cmd K) feature is the quickest way for users to guide AI-assisted programming, compensating for AI's inability to fully understand user intentions. This article will introduce the basic functions of Cursor's prompt bar (Cmd K) and some usage tips.

Author: 鸟飞鱼跃 | Original Article

  1. How to quickly generate git commit messages in Cursor?
  2. What are some tips for using the prompt bar (Cmd K)?
  3. Do you know the difference between the prompt bar (Cmd K) and the chat function (Cmd L)?

This article will introduce the second main feature of Cursor AI-assisted programming: the prompt bar (Cmd K). Let's find answers to these questions in the following content.

What is the Prompt Bar (Cmd K) Feature?

If the Tab feature is the most direct form of AI-assisted coding (directly generating code), then the prompt bar (Cmd K) is the quickest way for users to guide AI on what to do, compensating for AI's inability to fully understand user intentions.

The prompt bar (Cmd K) is the fastest way to interact with AI in Cursor. It's like a programming assistant always on standby, allowing you to quickly get AI help.

Cursor's Prompt Bar

Unlike the chat function (Cmd L), the prompt bar (Cmd K) is mainly used to generate or modify local code, while the chat function (Cmd L) is used for broader code discussions, better supporting multi-round conversations and answering more general programming questions.

Cursor's prompt bar is divided into two types:

  • Code editing prompt bar: Used in the code editor, mainly for supplementing or editing code.
  • Terminal prompt bar: Used in the terminal, mainly for generating commands in the terminal.

Code Editing Prompt Bar

In the code editor, pressing Cmd K (or Ctrl K on Windows/Linux) will bring up the prompt bar (as shown below). Users can enter prompts in the bar to generate code suggestions.

Cursor's Code Editing Prompt Bar

Context Information Used

The effectiveness of context information determines the quality of generated code.

When using the prompt bar in code editing, Cursor automatically tries to find useful information to improve code generation, in addition to the @ symbols you manually include. There's also additional context:

  • Files related to the code you're currently editing
  • Recently viewed files

Cursor ranks context items based on their relevance to your editing/generation and retains the most relevant items as context for the large language model.

Inline Generation

If no code is selected when you press Cmd K, Cursor will generate new code based on what you enter in the prompt bar.

Inline Generation

Inline Editing

If some code is selected when you press Cmd K, Cursor will modify the selected code based on what you enter in the prompt bar.

Inline Editing

Terminal Prompt Bar

In Cursor's built-in terminal, you can press Cmd K (or Ctrl K on Windows/Linux) to open the prompt bar at the bottom of the terminal.

Terminal Prompt Bar

  1. In the prompt bar, briefly describe what you want to do in the terminal.
  2. Cursor will automatically write a terminal command based on your description.
  3. If you think the command is usable:
    • Press Esc once, and the command will appear in the terminal but won't execute yet.
    • If you want to execute the command immediately, press Cmd + Enter.

Context Information Used

The terminal Cmd K by default considers the following context:

  • Recent history in the terminal
  • Any other content in the prompt bar

Tips

Follow-up Instructions

When using AI to generate code or answers, we often find that the first result isn't perfect. This is why we need the "follow-up instructions" feature. It's like having a conversation with AI, allowing you to guide it step by step until you get the desired result.

For example, if the code generated by AI the first time has some issues, you can directly say in the prompt bar, "This function needs to add error handling." Then press Enter, and AI will modify the code based on your new instructions. You don't need to restart the entire process but can continue to improve on the existing basis.

Quick Answer

Sometimes, we might need to quickly understand the function of a piece of code or get some simple information, but we don't want to immediately generate or modify code. This is where the quick answer feature comes in handy.

If you press Option/Alt Enter in the prompt bar, Cursor will immediately answer any questions about the selected content and related context (without generating or modifying code).

Quick Answer

This feature is particularly useful because:

  1. It can quickly answer your questions, helping you better understand the code.
  2. You don't need to switch to the chat window; you can get answers directly in the editor.
  3. The content of this conversation is remembered, so you can use this information in subsequent operations.

Quickly Generate Git Commit Messages

You can use the terminal prompt bar to quickly generate git commit messages.

Steps:

  1. In the terminal, enter 'git diff | cat' to get recent changes.
  2. Then enter 'git add .' to add all changes to the staging area.
  3. Press Cmd K, enter 'git commit message' in the prompt bar, and Cursor will generate a git commit command with a generated commit message.
  4. Press Cmd + Enter to execute the command, completing the entire process in one go.

Summary

By using the prompt bar (Cmd K) feature, you can quickly generate and modify code, create git commit messages, and even get quick answers to simple questions about your code. These features can help you write code more efficiently, save time, and improve your productivity.