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

3 天前
 yuanzhiwei
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 等好用的技巧,你们还有哪些好用的技巧可以分享一下
1544 次点击
所在节点    Cursor
7 条回复
wisej
3 天前
.code-workspace 是 cursor 的规范还是 vscode 本身的啊?我也有过多项目希望共享上下文的需求,当时是建的 workspace
yuanzhiwei
3 天前
@wisej 是 vscode 的规范,cursor 本身也是 vscode 开发的,所以支持的很好
dssxzuxc
2 天前
我给项目定制了各种 rules ,拦不住它的发癫
举个例子,我的规则里禁止它以任何形式 console.log(),然后它就偶尔给我在前端组件上
<pre> {{JSON.stringify(xxx)}} </pre>
我:天才,出院!
禁止在非复杂逻辑写一堆破注释,于是它发癫时会留下 // This logic isn't complicated; it shouldn't need comments.
我:???好你个大聪明!
有时候就感觉跟一个傻子在打架,实在打不过了我选择投降用 Tab
其实大多时候还是好使的,只是偶尔碰上各种弱智错误真的血压高升。而定制更详细更多的规则也无法解决这个问题,相反 rules 超过一定长度反倒会加重幻觉,只有燃烧钱包才能有效解决。
gotoschool
2 天前
这样前后端一起开发 怕不怕上下文 不同导致程序写的有问题呢?
我怎么感觉不如单开窗口开发的好呢
johnhuangemc2
2 天前
我经常要维护前后端一体的项目, 需要前后端一起修改.
Cursor 里用了 workspace 会让 cursor 仅在当前 workspace 下工作. 用了 workspace 后 cursor 就只在前端或后端目录下工作了. 做不到同时进行调整.
后面出了 cursor-agent, 我就在 ide 里用 workspace, 然后用 cursor-agent 来写程序. 目前工作还算良好.
yuanzhiwei
2 天前
@gotoschool 在一个工作空间,上下文就是一样的, 你让他实现功能, 可以用 @File 指定一下前端页面是哪个,后端接口写在哪,它就会自己去操作。
gefangshuai
2 天前
你们都是怎么用上的?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://ex.noerr.eu.org/t/1157400

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX