kakoune-snippets icon indicating copy to clipboard operation
kakoune-snippets copied to clipboard

Replace first placeholder with selection

Open akrivka opened this issue 6 years ago • 5 comments

A command like snippets-insert-replace-first-placeholder which would trigger a prompt, to which you would write the name of your snippets, which preferably only has one placeholder. Then, your current main selection would be the default value for the first placeholder.

A nice usecase would be \textbf{} and many other stuff in LaTeX.

akrivka avatar Mar 15 '19 17:03 akrivka

You can already do this with this simple proof-of-concept command

define-command snippets-insert-replace-first-placeholder %{
    prompt 'Snippet name: ' %{
        try %{
            eval -save-regs '"' %{
                exec -save-regs '' d
                snippets %val{text}
                exec R
            }
        }
    }
}

The prompt could be replaced by a menu or a command with completions without too much effort

occivink avatar Mar 15 '19 23:03 occivink

In UltiSnips Vim plugin this is called ${VISUAL} placeholder (because of visual mode). The desired selection is stored inside special variable, and gets pasted into next snippet which contains such placeholder. For example: We have such text ([] represent selection):

lorem ipsum [dolor sit amet]

We have such snippet tbf:

\textbf{${VISUAL}}`

When we select a text, and execute store-visual command (via some mapping), text disappears, and we can insert trigger somewhere:

lorem ipsum tbf[]

now we expand tbf snippet:

lorem ipsum \textbf{dolor sit amet}

If we expand it one more time:

lorem ipsum \textbf{dolor sit amet}
\textbf{}

${VISUAL} placeholder is empty, since we used it once, and didn't refilled it with new selection, so it acts as a tabstop.

This is useful in C++ code when we have munch of classes and want to put them to the namespace, we can select those, hit a key to store them, and expand a namespace snippet with desired classes inside. I've asked for these placeholders in this issue: https://github.com/occivink/kakoune-snippets/issues/1#issuecomment-448874940

andreyorst avatar Mar 17 '19 10:03 andreyorst

I don't really see the advantage over simply deleting, inserting the snippet and then either pasting/replacing (depending on whether there is some default text)

occivink avatar Mar 17 '19 11:03 occivink

In case of Vim it was automation. In case of Kakoune, we have R which will allow us to replace any selcted placeholder with stored text

andreyorst avatar Mar 17 '19 11:03 andreyorst

So is this something we should have? I don't see much benefit to it in kakoune

occivink avatar Mar 17 '19 14:03 occivink

The concept of a 'first placeholder' is no longer relevant.

occivink avatar Nov 13 '22 16:11 occivink