Add `ScratchToggle` Command
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?
Does :Scratch do what you want?
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 :)
I see. :Scratch should have the behavior you describe if you add g:scratch_insert_autohide = 0 to your config.
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:
- If you are not in a Scratch panel, open / jump to it
- If you are in a Scratch panel, close it.
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.