LintaoAmons/scratch.nvim

github github
utility
stars 172
issues 7
subscribers 4
forks 8
CREATED

2022-10-28

UPDATED

7 days ago


Create scratch file

Create temporary playground files effortlessly. Find them later without worrying about filenames or locations

  • With other nvim's goodies.
    • with treesitter to have syntax highlighting
    • with lsp and cmp to have auto-cmp, auto-format and other lsp goodies
    • with michaelb/sniprun or metakirby5/codi.vim to run scratch file

Scratch Intro

Install

using your favorate plugin manager, for example lazy.nvim

{
  "LintaoAmons/scratch.nvim",
  event = "VeryLazy",
}

Configuration

No need to config at the very begining, just install and explore the commands start with Scratch.

The way to config this plugin is a little difference(simpler) with other nvim plugin.

You can use ScratchEditConfig to edit the config once some new type popup your mind and the config will take effect immediately

Here's default config after you inited the plugin

NOTE: you can't have comment in your config, since only plain json supported right now

{
  "filetypes": ["xml", "go", "lua", "js", "py", "sh"], // you can simply put filetype here
  "window_cmd": "edit", // 'vsplit' | 'split' | 'edit' | 'tabedit' | 'rightbelow vsplit'. Can use rightbelow or topleft etc. as modifier
  "scratch_file_dir": "/you_home_path/.cache/nvim/scratch.nvim",
  "filetype_details": {
    "go": {
      // or, you can have more control here
      "filename": "main", // the filename of the scratch file in the new directory
      "cursor": {
        "location": [4, 2], // default location of cursor in the scratch file
        "insert_mode": true // default mode
      },
      "requireDir": true, // true, if each scratch file requires a new directory
      "content": ["package main", "", "func main() {", "  ", "}"] // default content in the scratch file
    },
    "yaml": {}, // for same filetype. you can have different postfix
    "k8s.yaml": {
      "subdir": "learn-k8s" // and put this type into a specific subdir
    },
    "json": {}, // empty object is fine
    "gp.md": {
      // create `gp.nvim` chat file
      "cursor": {
        "location": [12, 2],
        "insert_mode": true
      },
      "content": [
        "# topic: ?",
        "",
        "- model: {\"top_p\":1,\"temperature\":0.7,\"model\":\"gpt-3.5-turbo-16k\"}",
        "- file: placeholder",
        "- role: You are a general AI assistant.",
        "",
        "Write your queries after 🗨:. Run :GpChatRespond to generate response.",
        "",
        "---",
        "",
        "🗨:",
        ""
      ]
    }
  },
  "localKeys": [
    // local keymapping for specific type of file
    {
      "filenameContains": ["gp"],
      "LocalKeys": [
        {
          "cmd": "<CMD>GpChatRespond<CR>",
          "key": "<C-k>k",
          "modes": ["n", "i", "v"]
        }
      ]
    }
  ]
}

Init Configuration

  • This is triggered automaticlly at the first time you try to use Scrach's commands, and can be manually called to change the configuration file path, and this allows you:
    • Put your configuration anywhere you want and can be tracked along with your other configuration with git
    • Have multiple configuration, and switch the configuration by change the configuration filepath with this command
:ScratchInitConfig

Check current Configuration

:ScratchCheckConfig

Edit Configuration

:ScratchEditConfig

Note: Don't need require restart nvim after change the config.

show

Commands | Keymappings | Functions

No default keymappings, here's functions you can mapping to.

All commands are started with Scratch, here is one example to add your keybinding to the commands.

vim.keymap.set("n", "<M-C-n>", "<cmd>Scratch<cr>")
vim.keymap.set("n", "<M-C-o>", "<cmd>ScratchOpen<cr>")
Command Description
Scratch Creates a new scratch file in the specified scratch_file_dir directory in your configuration.
ScratchWithName Allows the creation of a new scratch file with a user-specified filename, including the file extension.
ScratchOpen Opens an existing scratch file from the scratch_file_dir.
ScratchOpenFzf Uses fuzzy finding to search through the contents of scratch files and open a selected file.
ScratchCheckConfig Prints the current configuration to confirm that custom settings are active.
ScratchEditConfig Opens the configuration file for editing, with changes taking effect without needing to restart Neovim.
ScratchPad A specific file designed for continuous information recording, likely acting as an ongoing note-taking or log file.

Functions

functions can be required from scratch, check ./lua/scratch/init.lua to get the functions you can use

Jump to scratch file from terminal

nvim -c 'lua require("scratch").scratchByType("md")'

NOTE: you can't lazyload the plugin if you want make the scratch plugin accessible at the init of nvim

CONTRIBUTING

Don't hesitate to ask me anything about the codebase if you want to contribute.

You can contact with me by drop me an email or telegram

FIND MORE USER FRIENDLY PLUGINS MADE BY ME