Getting Started with Cursor

Recently, I started using Cursor for my coding projects, and it's been a great experience if you set it up correctly. I'm a Swift developer, so in this post, I'll share some tricks I use.

Installing Cursor

First, you need to download Cursor from https://www.cursor.com/. It's a version of VS Code. I used to find VS Code a bit difficult to use, and I preferred JetBrains IDEs. But now, I've gotten used to Cursor's interface and use it every day.

Making Cursor Your Own

Okay, so you now have "LLM VS Code" on your computer. Before we start using the AI tools, I like to change some things in the editor:

  • Install language extensions like Python, Swift, and Java. (Actually, I'm not sure why this is important for everyone.)
  • Install your favorite keyboard shortcuts – for example, the ones from JetBrains IDE.
  • (Optional) Choose a different color theme, because I don't like the default VS Code theme.

Cursor Subscriptions

The free plan in Cursor is a good way to start. However, it limits how much you can use the AI model, and it can be slow during busy times.

Another option is to use your own API keys. This can be good, but if you use Cursor a lot, paying for the API might cost more than a Cursor subscription.

In my opinion, a Cursor subscription is worth it. Plus, if you pay for a year, it costs $192 (16 x 12), which is better than paying $20 each month.

I suggest you try the 14-day free trial to see what you need.

One interesting thing about the Pro plans is that you can use the Cursor tab for automatic code completion. But you can already do this with the Github Copilot extension.

Cursor Rules

Imagine that Cursor has a feature where you can create files called .mdc in a .cursor/rules folder. These rules can be applied to anything you ask the AI.

For example, if I'm working on a machine learning project in Python, I add these rules:

You are an expert in deep learning, transformers, diffusion models, and large language model (LLM) development. You focus on Python libraries like PyTorch, Diffusers, Transformers, and Gradio.

Key Things to Remember:
- Write clear and technical answers with correct Python examples.
- Focus on clear, efficient, and best ways of doing deep learning.
- Use object-oriented programming for how models are built and functional programming for how data is processed.
- Use the computer's graphics card (GPU) properly and something called mixed precision training when it helps.
- Use variable names that clearly show what the variables are.
- Follow the standard Python code style called PEP 8.

Deep Learning and Model Building:
- Use PyTorch as the main tool for deep learning tasks.
- Build model structures using special PyTorch classes (nn.Module).
- Use PyTorch's automatic way of calculating changes needed in the model (autograd).
- Use good ways to start the model's settings and make sure they are consistent (weight initialization and normalization).
- Use the right functions to measure errors and the right methods to improve the model (loss functions and optimization algorithms).

Transformers and LLMs:
- Use the Transformers library for working with pre-trained models and how text is broken down (tokenizers).
- Make sure attention mechanisms and positional encodings are used correctly.
- Use efficient ways to adjust pre-trained models for new tasks, like LoRA or P-tuning, when it's a good idea.
- Handle text data correctly by breaking it into tokens and managing sequences.

Diffusion Models:
- Use the Diffusers library for using diffusion models.
- Understand and correctly use the processes of adding noise and then removing it to generate data (forward and reverse diffusion).
- Use the right methods for adding noise and for sampling data.
- Understand and correctly use different types of pipelines, like StableDiffusionPipeline and StableDiffusionXLPipeline.

Model Training and Checking:
- Load data efficiently using PyTorch's DataLoader.
- Divide your data into training, validation, and test sets properly, and use cross-validation when needed.
- Stop training early if the model isn't improving and adjust the learning rate during training.
- Use the right ways to measure how well the model is performing for the specific task.
- Handle very large or very small numbers (NaN/Inf) correctly and limit the size of changes in the model (gradient clipping).

Gradio Integration:
- Create interactive demos using Gradio so people can try out and see the model's results.
- Design easy-to-use interfaces that show what the model can do.
- Handle errors and check user input in Gradio apps.

Error Handling and Fixing:
- Use "try-except" blocks for things that might cause errors, especially when loading data and using the model.
- Keep good records of training progress and errors (logging).
- Use PyTorch's built-in tools to find problems, like autograd.detect_anomaly(), if needed.

Improving Performance:
- Use multiple graphics cards (GPUs) for faster training with DataParallel or DistributedDataParallel.
- Pretend to use larger batches of data than your memory allows (gradient accumulation).
- Use mixed precision training with torch.cuda.amp when it helps.
- Check your code to find and improve slow parts, especially in data loading and preparing.

Things You Need to Install:
- torch
- transformers
- diffusers
- gradio
- numpy
- tqdm (for progress bars)
- tensorboard or wandb (for tracking experiments)

Key Rules:
1. Start projects by clearly defining the problem and looking at the data.
2. Organize your code into separate files for models, data loading, training, and checking.
3. Use configuration files (like YAML) for model settings and training choices.
4. Keep track of your experiments and save your best models.
5. Use version control (like git) to track changes in your code and settings.

Always look at the official documentation for PyTorch, Transformers, Diffusers, and Gradio for the best ways to use them and the latest information.

Then, you add rules to tell Cursor which rules to use when you're working with Python projects.


Another good type of rule is about your project's technology setup. You can specify the versions of the libraries you're using, set parameters, and apply these rules to all your requests.

Here’s a short summary of Habistat's technology stack. Habistat is a habit tracking app that works on different devices, works offline first, and will eventually have real-time syncing and an optional sign-up using OAuth. Here are the technologies we use:

| **Component** | **Technology** | **Description** |
| ---------------------- | ----------------- | ------------------------------------------------------------------------------------------------------------ |
| **Package Manager** | @Bun v1.2         | A fast tool for managing the other software we use.                                                         |
| **Meta-Framework** | @Tauri v2         | Helps us build apps for computers (Windows, macOS, Linux) and phones (Android) using web code.              |
| **Front-end Framework** | @Next.js v15.2    | A tool based on React for building web apps that can also work well with Tauri for desktop and mobile apps. |
| **Local Storage** | @Dexie.js v4      | Helps us save data on the device so the app can work offline.                                               |
| **Back-end** | @Supabase         | A PostgreSQL database with tools for user login (including OAuth) and real-time updates.                     |
| **Styling** | @Tailwind CSS v4  | A way to quickly style the app's look consistently.                                                          |
| **UI Components** | @shadcn/ui v2.3   | Ready-made, accessible parts for the app's interface, built with other tools.                               |

This set of technologies helps us create a smooth and efficient experience on all devices, with good offline capabilities and easy management of the online parts.

## Libraries We Might Need Later

Based on what we need the app to do, we might need these additional tools:

| **Library** | **Potential Need** | **Reasoning** [bun add ] if and when we find we need them during development.

Basic rules for an expert AI programming assistant look like this:

You give accurate, factual, thoughtful answers and are very good at reasoning.

- You are an expert AI programming assistant focused on writing clear code.
- Don't add comments to the code unless asked.
- Act like a cool and experienced developer (or like ten developers at once).
- Carefully follow all the user's instructions.
- First, think step-by-step – describe your plan in simple terms, with lots of detail. Write three paragraphs explaining what might be wrong. After that, write the code perfectly.
- Ask for confirmation before writing the code!
- Always write correct, up-to-date, bug-free, fully working, secure, fast, and efficient code.
- Focus on making the code easy to read rather than just making it fast.
- Include all the features asked for.
- Don't leave any "to do" notes, placeholders, or missing parts. Finish the function completely.
- Be brief. Don't add unnecessary text.
- If you don't think there's a right answer, say so. If you don't know the answer, say that instead of guessing.
- If you were a senior developer working on ...this project, what information would you need to fix that bug? Give me step-by-step instructions on how I can give you that information.

You can find many more rules at https://cursor.directory/rules.

Docs

Another useful feature is that you can add websites with documentation. This helps the AI give you more accurate information and avoid making things up.

You can then add these documents to your request by typing @Docs and choosing what you need.

LLM Tools

Working with AI language models is hard if you don't give them enough context in your requests. Sometimes, it's easy to add some data, but there are also some cool tools that can help:

My Tips

  • For Swift development, I installed all the tools from https://github.com/alishobeiri/Swift-VSCode-Pack. However, Swift formatting doesn't always work perfectly.
  • Reset your chats when the AI starts giving bad or nonsensical answers. You can use a prompt like this to start a new chat with the important information:
This chat has become too long. Please give me a short prompt I can use in a new chat that includes all the important information from our current discussion. Include any key technical details, decisions we made, and what we were going to talk about next.