Skip to content

RyotaUshio/hsnips-dev

Repository files navigation

My hsnips

I'm trying out HyperSnips to make it easier to type $\LaTeX$ equations in VSCode/Cursor. This is my current setup for developing HyperSnips snippets.

It turned out to be pretty hard to work directly with the vanilla .hsnips files, especially when I have to a bunch of JavaScript to implement something complex like auto-fraction (inspired by Latex Suite for Obsidian).

That's why I'm using snipbuild, a build tool that I wrote to solve this problem.

  • Global block JavaScript is developed with TypeScript and unit-tested with Vitest. It makes maintainance far easier.
    • Each src/globals/[LANGUAGE].ts exports what will be included in the global block of the corresponding snippet file.
  • The snippet blocks for each language is located at src/snippets/[LANGUAGE].hsnips. It is in the exact same format as normal .hsnips files except:
    • It does not have the global ... endglobal block at the top.
    • It allows you to include common snippets shared by several (but not necessarily all) languages using #include "...".
  • When running pnpm build, Rolldown generates the global JavaScript from src/globals/[LANGUAGE.ts], and the output is concatenated with the snippet blocks from src/snippets/[LANGUAGE].hsnips.

Setup

Installation

  1. pnpm i to install dependencies
  2. pnpm build to generate snippet files under the dist/ directory
  3. Install the generated snippet files. On macOS, you can just run make to do it.
  4. Make sure you execute the HyperSnips: Reload Snippets command after updating the snippet files.

Once you install the snippets using symbolic links (as shown above), all you have to do after making edit to the source code is pnpm bulid & HyperSnips: Reload Snippets.

Customize

This repo is still very young and many useful snippets are yet to be added. To customize snippets by yourself, edit the following files:

  • Global blocks: src/globals/[LANGUAGE].hsnips
  • Snippet blocks: src/globals/[LANGUAGE].hsnips

Fix <Tab> key

By default, VSCode accepts a suggestion when <Tab> is pressed. This causes conflicts with snippets with tab stops.

To fix it, run Preferences: Open Keyboard Shortcuts (JSON) command, and add the following to keybindings.json:

  {
    "key": "tab",
    "command": "-acceptSelectedSuggestion"
  }

Optional: Fix <Esc> key

Ignore this section if you're not a vscodevim user!

By default, you have to press <Esc> twice to go back to Normal mode after expanding a snippet. This is annoying!

To fix this, install multi-command and add the following to keybindings.json:

  {
    "key": "escape",
    "command": "extension.multiCommand.execute",
    "args": {
      "sequence": ["leaveSnippet", "extension.vim_escape"]
    },
    "when": "editorTextFocus && vim.active && inSnippetMode"
  }

Credits

  • Gilles Castel's amazing blog post and UtilSnip snippets
  • artisticat's Latex Suite plugin for Obsidian
    • Seriously, it proved pretty difficult to recreate in VSCode/Cursor what LaTeX Suite makes possible in Obsidian, which made me realize what a well-written piece of software it is. Crazy work.
    • Especially inspired by her implementation of auto-fraction

About

My setup for developing HyperSnips snippets.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published