Skip to main content

How to Use Prompts in Cursor

tip

Effective use of prompts can significantly enhance your coding productivity in Cursor by leveraging AI assistance.

Understanding Prompts​

Prompts in Cursor are natural language instructions that help you:

  • Generate code
  • Debug issues
  • Refactor existing code
  • Get explanations
  • Automate tasks

Basic Prompt Types​

1. Code Generation​

# Basic code generation
/generate Create a React button component with hover effects

2. Code Explanation​

# Get explanation for code
/explain What does this regex pattern do: ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$

3. Code Refactoring​

# Request refactoring
/refactor Convert this function to use async/await

Writing Effective Prompts​

1. Be Specific​

✅ Good Prompt:

/generate Create a TypeScript function that validates email addresses using regex with proper error handling

❌ Poor Prompt:

/generate Make an email checker

2. Provide Context​

/explain This code in the context of a React application using Redux for state management:

3. Use Parameters​

/generate language:typescript
framework:react
style:functional
Create a form component with email validation

Advanced Prompt Features​

1. Multi-step Tasks​

/task Create a user authentication system
Steps:
1. Create login form
2. Add validation
3. Implement API calls
4. Handle responses
5. Add error handling

2. Template Generation​

/template Create a new React component template with:
- TypeScript
- Styled Components
- Unit tests
- Documentation

3. Custom Rules​

/rules Add custom rules for:
- Code style
- Error handling
- Documentation
- Testing requirements

Language-Specific Prompts​

JavaScript/TypeScript​

/generate js
Create an ES6 class for handling API requests with:
- Axios integration
- Error handling
- Request caching

React Components​

/generate react
Create a responsive navigation component with:
- Mobile menu
- Dark mode support
- Accessibility features

Best Practices​

1. Structure Your Prompts​

/generate
Purpose: Create authentication middleware
Requirements:
- JWT validation
- Role-based access
- Error handling
- Logging
Technologies:
- Express.js
- jsonwebtoken library

2. Use Templates​

Create reusable prompt templates:

# Save template
/template save auth-component
Create an authentication component with:
- Login/Register forms
- Validation
- API integration
- Error handling

# Use template
/template use auth-component

3. Include Examples​

/generate
Create a function similar to:
```javascript
function example(data) {
return data.filter(item => item.active);
}

but with additional type checking and error handling


## Prompt Patterns

### 1. Code Review

```bash
/review
Check this code for:
- Performance issues
- Security vulnerabilities
- Best practices
- Type safety

2. Documentation​

/doc
Generate documentation for:
- Function parameters
- Return values
- Examples
- Edge cases

3. Testing​

/test
Create unit tests covering:
- Happy path
- Edge cases
- Error scenarios
- Async behavior

Advanced Techniques​

1. Chain Prompts​

/chain
1. Generate API service
2. Add error handling
3. Create tests
4. Add documentation

2. Context Awareness​

/context
Consider:
- Current file imports
- Project structure
- Dependencies
- Coding standards

3. Iterative Refinement​

/refine
Improve the generated code by:
1. Adding type safety
2. Optimizing performance
3. Enhancing readability
4. Adding comments

Troubleshooting​

Common Issues​

  1. Unclear Results

    /clarify
    The previous generation didn't handle:
    - Edge cases
    - Error scenarios
  2. Missing Context

    /context add
    Project uses:
    - React 18
    - TypeScript 4.8
    - Material UI
  3. Incomplete Generation

    /continue
    Please complete the implementation with:
    - Error handling
    - Type definitions

Integration Tips​

1. Version Control​

/generate
Create a commit message following:
- Conventional commits
- Issue reference
- Breaking changes

2. Project Setup​

/setup
Initialize project with:
- TypeScript configuration
- ESLint rules
- Prettier settings
- Git hooks

3. Documentation​

/doc
Generate project documentation:
- README.md
- API documentation
- Setup instructions
- Contributing guidelines

Tips for Success​

  1. Be Clear and Specific

    • State your requirements
    • Provide examples
    • Specify constraints
  2. Iterate and Refine

    • Start with basic prompts
    • Add details gradually
    • Refine based on results
  3. Learn from Responses

    • Analyze AI suggestions
    • Improve prompt patterns
    • Save effective prompts
tip

Use the /help command to discover more prompt features and options!