Cursor AI 编程助手使用指南
Cursor 使用体验
Cursor 是一款非常好用的 AI 编程助手。我直接购买了 Pro 版本,每月 20 美元的订阅费用物有所值。Pro 版本不仅提供更强大的代码补全功能,还支持更长的上下文理解,同时迁移成本也很低。
作为一个基于 VSCode 开源分支的编辑器,Cursor 对 VSCode 插件有着良好的支持。可以直接导入 VSCode 的账号设置,在 React 开发中对状态管理提供了很大帮助。它不仅能直接生成代码,还可以修改 CSS 样式。代码补全功能也非常智能,比如在我使用 unocss
时,它能够智能地进行样式转换。
对于简单的页面,可以直接复制粘贴设计图片让 AI 生成代码。通过使用 @docs
命令,还可以解决 AI 模型因训练数据滞后而无法获取最新版本库信息的问题。
常用快捷键
cmd + k
打开聊天窗口cmd + shift + L
打开设置cmd + i
打开 composer@
打开提示词库@docs
查看官方文档@file
查看当前文件的文档@folder
查看当前文件夹下所有文档@web
搜索网络上的信息
配置 Cursor
- 安装 Cursor
- 导入 Cursor 插件
- 配置 Cursor 规则
一个不错的 Cursor 提示词库 Awesome CursorRules
详细提示词
点击查看详细提示词
You are an expert in Solidity, TypeScript, Node.js, Next.js 14 App Router, React, Vite, Viem v2, Wagmi v2, Shadcn UI, Radix UI, and Tailwind Aria. Key Principles:
- Write concise, technical responses with accurate TypeScript examples.
- Use functional, declarative programming. Avoid classes.
- Prefer iteration and modularization over duplication.
- Use descriptive variable names with auxiliary verbs (e.g., isLoading).
- Use lowercase with dashes for directories (e.g., components/auth-wizard).
- Favor named exports for components.
- Use the Receive an Object, Return an Object (RORO) pattern.
JavaScript/TypeScript:
- Use "function" keyword for pure functions. Omit semicolons.
- Use TypeScript for all code. Prefer interfaces over types. Avoid enums, use maps.
- File structure: Exported component, subcomponents, helpers, static content, types.
- Avoid unnecessary curly braces in conditional statements.
- For single-line statements in conditionals, omit curly braces.
- Use concise, one-line syntax for simple conditional statements (e.g., if (condition) doSomething()).
- Prioritize error handling and edge cases:
- Handle errors and edge cases at the beginning of functions.
- Use early returns for error conditions to avoid deeply nested if statements.
- Place the happy path last in the function for improved readability.
- Avoid unnecessary else statements; use if-return pattern instead.
- Use guard clauses to handle preconditions and invalid states early.
- Implement proper error logging and user-friendly error messages.
- Consider using custom error types or error factories for consistent error handling.
Dependencies:
- Next.js 14 App Router
- Wagmi v2
- Viem v2
React/Next.js:
- Use functional components and TypeScript interfaces.
- Use declarative JSX.
- Use function, not const, for components.
- Use Shadcn UI, Radix, and Tailwind Aria for components and styling.
- Implement responsive design with Tailwind CSS.
- Use mobile-first approach for responsive design.
- Place static content and interfaces at file end.
- Use content variables for static content outside render functions.
- Minimize 'use client', 'useEffect', and 'setState'. Favor RSC.
- Use Zod for form validation.
- Wrap client components in Suspense with fallback.
- Use dynamic loading for non-critical components.
- Optimize images: WebP format, size data, lazy loading.
- Model expected errors as return values: Avoid using try/catch for expected errors in Server Actions. Use useActionState to manage these errors and return them to the client.
- Use error boundaries for unexpected errors: Implement error boundaries using error.tsx and global-error.tsx files to handle unexpected errors and provide a fallback UI.
- Use useActionState with react-hook-form for form validation.
- Code in services/ dir always throw user-friendly errors that tanStackQuery can catch and show to the user.
- Use next-safe-action for all server actions:
- Implement type-safe server actions with proper validation.
- Utilize the `action` function from next-safe-action for creating actions.
- Define input schemas using Zod for robust type checking and validation.
- Handle errors gracefully and return appropriate responses.
- Use import type { ActionResponse } from '@/types/actions'
- Ensure all server actions return the ActionResponse type
- Implement consistent error handling and success responses using ActionResponse
Cursor 个人使用分享
- 用 Markdown 给他写消息,通过标题、列表来让 GPT 更容易了解主次。
- Step by step 让他一步一步写顺序。
- 用白话多次让他写 Markdown 提示词,不断修改,最后的提示词再喂给 Cursor 去生成。
- 更长的上下文
- 可以阅读整个项目
- 生成后类似 DIFF 的修改
- 多模型,模型
Claude3.5 Sonnet
比GPT4o
好用 - Composer 可以生成代码,直接执行
甚至用 CopyCode 网站,可以直接使用 Figma 设计稿,生成一套提示词,给到 Composer 使用,直接就能生成 90% 的静态网站了。
功能特性 | Cursor | GitHub Copilot | 对比说明 |
---|---|---|---|
基础功能 | |||
代码补全 | ✅ 更智能的上下文理解 | ✅ 基于上下文补全 | Cursor的补全更准确,理解更深入 |
多行编辑 | ✅ 智能批量编辑 | ⚠️ 仅支持基础多光标 | Cursor支持更智能的批量修改 |
代码解释 | ✅ 实时、详细 | ✅ 基础解释 | Cursor的解释更加详细和准确 |
AI 特性 | |||
对话功能 | ✅ 内置Chat功能 | ⚠️ 需要Copilot Chat | Cursor原生支持,无需额外订阅 |
代码重构 | ✅ 智能重构建议 | ⚠️ 有限支持 | Cursor提供更完整的重构方案 |
错误修复 | ✅ 自动循环修复 | ✅ 基础修复建议 | Cursor支持自动化修复循环 |
高级功能 | |||
Agent模式 | ✅ 完整支持 | ❌ 不支持 | Cursor独有功能 |
终端集成 | ✅ 智能命令生成 | ❌ 不支持 | Cursor支持终端智能操作 |
图片识别 | ✅ 支持 | ❌ 不支持 | Cursor可直接识别UI转代码 |
项目理解 | |||
代码库理解 | ✅ 深度理解 | ✅ 基础理解 | Cursor对项目结构理解更深入 |
跨文件引用 | ✅ 智能关联 | ⚠️ 有限支持 | Cursor提供更智能的代码关联 |
Web集成 | ✅ 支持 | ❌ 不支持 | Cursor可直接搜索网络资源 |
开发工具集成 | |||
Git集成 | ✅ 智能commit | ❌ 不支持 | Cursor支持自动化Git操作 |
自动化工作流 | ✅ 完整支持 | ❌ 不支持 | Cursor支持端到端自动化 |
LSP支持 | ✅ 完整支持 | ✅ 完整支持 | 两者都有良好的语言服务支持 |
性能与体验 | |||
响应速度 | ⚡️ 快速 | ⚡️ 快速 | 两者性能相当 |
资源占用 | 📊 中等 | 📊 较低 | Copilot资源占用较少 |
使用门槛 | 📈 中等 | 📉 较低 | Cursor功能更丰富,需要一定学习 |