V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
yuanzhiwei
V2EX  ›  程序员

深度使用一段时间 Cursor 后, 分享一点我的实用小技巧

  •  
  •   yuanzhiwei · 20 小时 58 分钟前 · 1012 次点击
    1. 多项目(前后端分离项目)共用窗口上下文。
    可以在项目目录中创建一个.code-workspace 结尾的 json 文件,内容示例:
    {
    "folders": [
    {
    "name": "xxx-web",
    "path": "./xxx-web"
    },
    {
    "name": "xxx-api",
    "path": "./xxx-api"
    }
    ],
    "settings": {
    "editor.tabSize": 2,
    "files.exclude": {
    }
    }
    }
    cursor 选择从文件打开项目,这样就可以前后端联动愉快的开发啦~

    2. 学会使用 Rules 和指令打造你的专属编程风格。
    AI 的代码风格可能千奇百怪,一会儿用 axios ,一会儿用 fetch ;一会儿遵循你的项目规范,一会儿又“自由发挥”,有木有同感啊兄弟们~
    ( 1 )全局规则,跨项目生效
    玩法示例,cursor settings > rules 设置:
    ----
    - All React components must be functional components using hooks. Do not use class components.
    - For state management, prefer Zustand over Redux. Only use React Context for simple, non-frequently updated state like theming.
    - When creating a new component, structure it in its own folder with an `index.ts` for exports, the component file (e.g., `Button.tsx`), and a Storybook file (`Button.stories.tsx`).
    - For all asynchronous operations, use async/await syntax and wrap them in a try/catch block with proper error logging to Sentry.
    - Always use Tailwind CSS for styling. Do not write inline styles or use CSS-in-JS libraries.
    - The default testing framework is Vitest with React Testing Library. Generated tests must follow this pattern.


    ( 2 )项目级规则 (.cursorrules)
    示例:
    ---
    // .cursorrules file in a legacy project

    - This project uses a custom data fetching hook `useLegacyDataFetcher`. Do not use `fetch` or `axios` directly.
    - All API endpoints are prefixed with `/api/v2`.
    - When modifying any file in the `/src/utils/` directory, be extra careful about breaking changes and suggest adding a unit test for the modification.
    - This project uses a custom design system. When asked to create UI elements, look for existing components in `/src/components/ui/` before creating new ones.

    3. 当然还有灵活使用 @File, @Docs, @Web 等等精准的指向上下文

    4. 超越聊天:Cmd+K 的进阶艺术
    Cmd+K 不仅仅是“写个方法”或“加个注释”那么简单,他可以进行精准选中与意图明确的微操。
    Cmd+K 还可以在终端中使用,用自然语言来生成复杂的 shell 命令

    还有其他的如 Restore Checkpoint 等好用的技巧,你们还有哪些好用的技巧可以分享一下
    6 条回复    2025-09-06 12:32:52 +08:00
    wisej
        1
    wisej  
       20 小时 51 分钟前
    .code-workspace 是 cursor 的规范还是 vscode 本身的啊?我也有过多项目希望共享上下文的需求,当时是建的 workspace
    yuanzhiwei
        2
    yuanzhiwei  
    OP
       20 小时 47 分钟前
    @wisej 是 vscode 的规范,cursor 本身也是 vscode 开发的,所以支持的很好
    dssxzuxc
        3
    dssxzuxc  
       19 小时 4 分钟前
    我给项目定制了各种 rules ,拦不住它的发癫
    举个例子,我的规则里禁止它以任何形式 console.log(),然后它就偶尔给我在前端组件上
    <pre> {{JSON.stringify(xxx)}} </pre>
    我:天才,出院!
    禁止在非复杂逻辑写一堆破注释,于是它发癫时会留下 // This logic isn't complicated; it shouldn't need comments.
    我:???好你个大聪明!
    有时候就感觉跟一个傻子在打架,实在打不过了我选择投降用 Tab
    其实大多时候还是好使的,只是偶尔碰上各种弱智错误真的血压高升。而定制更详细更多的规则也无法解决这个问题,相反 rules 超过一定长度反倒会加重幻觉,只有燃烧钱包才能有效解决。
    gotoschool
        4
    gotoschool  
       17 小时 17 分钟前
    这样前后端一起开发 怕不怕上下文 不同导致程序写的有问题呢?
    我怎么感觉不如单开窗口开发的好呢
    johnhuangemc2
        5
    johnhuangemc2  
       10 小时 54 分钟前
    我经常要维护前后端一体的项目, 需要前后端一起修改.
    Cursor 里用了 workspace 会让 cursor 仅在当前 workspace 下工作. 用了 workspace 后 cursor 就只在前端或后端目录下工作了. 做不到同时进行调整.
    后面出了 cursor-agent, 我就在 ide 里用 workspace, 然后用 cursor-agent 来写程序. 目前工作还算良好.
    yuanzhiwei
        6
    yuanzhiwei  
    OP
       6 小时 55 分钟前
    @gotoschool 在一个工作空间,上下文就是一样的, 你让他实现功能, 可以用 @File 指定一下前端页面是哪个,后端接口写在哪,它就会自己去操作。
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2518 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 11:28 · PVG 19:28 · LAX 04:28 · JFK 07:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.