mtth / scratch.vim

Unobtrusive scratch window
MIT License
395 stars 32 forks source link

Add `ScratchToggle` Command #41

Open alichtman opened 9 months ago

alichtman commented 9 months ago

I wanted to set up a scratch buffer that is toggle-able (like :ScratchPreview) and automatically focuses my cursor on the buffer when opening it. I was expecting a :ScratchToggle command, but did not find one.

Here's my current config for scratch in lazy.nvim:

return {
  "mtth/scratch.vim",

  keys = {
    { "<leader>sp", ":ScratchPreview<CR>", mode = "n", noremap = true, silent = true },
    { "<leader>sp", ":ScratchSelection<CR>", mode = "v", noremap = true, silent = true },
  },

  init = function()
    vim.g.scratch_no_mappings = 1
    vim.g.scratch_persistence_file = "~/scratchpad/vim-scratchpad.txt"
    vim.g.scratch_autohide = 0
  end,
}

When I want to open a scratch window, I use <leader>sp. This opens a scratch buffer, but I have to manually move the cursor there.

Any chance this could be added to the plugin?

mtth commented 9 months ago

Does :Scratch do what you want?

alichtman commented 8 months ago

No, because the scratch pad doesn't stay open when I change windows back to where I'm working. I guess this detail wasn't exactly clear from my original post :)

mtth commented 8 months ago

I see. :Scratch should have the behavior you describe if you add g:scratch_insert_autohide = 0 to your config.

alichtman commented 8 months ago

This almost gets there, but the Scratch window doesn't close when I run the same key binding again. I'd like :ScratchToggle to have the following behavior:

  1. If you are not in a Scratch panel, open / jump to it
  2. If you are in a Scratch panel, close it.
mtth commented 8 months ago

OK, I see. There is no built-in command with this behavior. You could implement it by checking for the existence of the __Scratch__ buffer (very similar to preview's underlying logic). If you are interested in contributing it to this repo, I would be happy to review a PR which adds a :ScratchPreview bang argument to tweak its behavior accordingly.