nvim-snippets

repository·main·Indexed 42 days ago

https://github.com/garymjr/nvim-snippets

A Neovim plugin that enables VSCode-style snippets using the native vim.snippet API. It provides built-in support for friendly-snippets and integrates with nvim-cmp for completion.

Tokens
1.3K
Snippets
4
Records
4
Agent score
4%

What's inside nvim-snippets

  1. Install nvim-snippets using lazy.nvim

    main

    To install nvim-snippets, add it to your lazy.nvim configuration. The installation includes recommended keybindings for <Tab> and <S-Tab> to handle snippet jumping (forward and backward) in Insert (i) and Select (s) modes using the native vim.snippet API.

    {
      "garymjr/nvim-snippets",
      keys = {
        {
          "<Tab>",
          function()
            if vim.snippet.active({ direction = 1 }) then
              vim.schedule(function()
                vim.snippet.jump(1)
              end)
              return
            end
            return "<Tab>"
          end,
          expr = true,
          silent = true,
          mode = "i",
        },
        {
          "<Tab>",
          function()
            vim.schedule(function()
              vim.snippet.jump(1)
            end)
          end,
          expr = true,
          silent = true,
          mode = "s",
        },
        {
          "<S-Tab>",
          function()
            if vim.snippet.active({ direction = -1 }) then
              vim.schedule(function()
                vim.snippet.jump(-1)
              end)
              return
            end
            return "<S-Tab>"
          end,
          expr = true,
          silent = true,
          mode = { "i", "s" },
        },
      },
    }
  2. Configure nvim-snippets options

    main

    You can customize how snippets are loaded and integrated. Use create_cmp_source = true to automatically create an nvim-cmp source named snippets. If you are not using nvim-cmp, set create_autocmd = true to ensure snippets are loaded when opening files.

    -- Example configuration structure
    {
      "garymjr/nvim-snippets",
      opts = {
        create_cmp_source = true,
        friendly_snippets = true,
        global_snippets = {"all"},
        search_paths = { vim.fn.stdpath('config') .. '/snippets' },
        -- other options...
      }
    }
  3. Create VSCode-style snippets

    main

    The plugin supports VSCode-style JSON snippet definitions. Snippets use prefix for triggers and body for the snippet content, supporting placeholders like ${1:default} and the final cursor position $0.

    {
      "Say hello to the world": {
        "prefix": ["hw", "hello"],
        "body": "Hello, ${1:world}!$0"
      }
    }
  4. Reference nvim-snippets configuration options

    main

    The following options are available for configuring nvim-snippets:

    | Option | Type | Default | Description |
    |---|---|---|---|
    | `create_autocmd` | `boolean?` | `false` | Optionally load all snippets when opening a file. Only needed if not using `nvim-cmp`. |
    | `create_cmp_source` | `boolean?` | `true` | Optionally create a `nvim-cmp` source. Source name will be `snippets`. |
    | `friendly_snippets` | `boolean?` | `false` | Set to true if using `friendly-snippets`. |
    | `ignored_filetypes` | `string[]?` | `nil` | Filetypes to ignore when loading snippets. |
    | `extended_filetypes` | `table?` | `nil` | Filetypes to load snippets for in addition to the default ones. `ex: {typescript = {'javascript'}}` |
    | `global_snippets` | `string[]?` | `{'all'}` | Snippets to load for all filetypes. |
    | `search_paths` | `string[]` | `{vim.fn.stdpath('config') .. '/snippets'}` | Paths to search for snippets. |