Occivink Snippets - how to use?

Hello,
I would like to try the snippets here:
https://github.com/occivink/kakoune-snippets
but I don’t quite know how.
In what file should the snippets go? Where should that file go? I think I am missing a few steps.
I’ve watched the asciinema demo but it does not show the keystrokes.
I ran the test mentioned in the readme, (kak -n -e ‘source test.kak_ ; quit’) and kakoune did not stay open so it seems to be installed correctly.

Thank you for any hints! (or other snippet suggestions for basic-level user).

Snippets with this plugin differs from those commonly implemented with vim type editors whose expansion mechanism fetches a template from a directory/file (often with one file per snippet definition).

Here, snippets are defined with set-option statements – you can for organization, place any or all of these directives in a separate .kak file and source them.

Hooks are the easiest way to associate a group of snippets with a particular filetype, e.g.

hook global BufSetOption filetype=markdown %{
	set-option ...
	set-option ...
	...
}

Define the snippet with

set-option -add buffer snippets "<name>" "<abbrev>" %{ snippets-insert <snippet content> }

where,
<name> is the snippet name to display when you enter the command ":snippets "
<abbrev> is the trigger string to cause snippet expansion in insert mode
<snippet content> is the snippet string. This can be a simple string within %{ … } or a generated string e.g. %sh{ … }

Positional parameters are defined within the snippet content with ${label1}, ${label2}, etc.

Altogether…

hook global BufSetOption filetype=markdown %{
	set-option -add buffer snippets 'date'   '@da' %{ snippets-insert %sh{ date '+## %A, %d %B %Y' | tr '[:upper:]' '[:lower:]' }}
	set-option -add buffer snippets 'img'    '@im' %{ snippets-insert %{![${heading}](/images/${file}.jpg) }}
}

In command mode, typing ":snippets " will bring up the snippet list “date img” (for markdown files) for selection and insert OR in insert mode, typing “@im” will immediately expand to its snippet content.

Hope this helps…

1 Like

Thank you so much. I’ll try this out!

With your hint, I was able to add some snippets. Thank you. Namely, I put the following line in my kakrc file:

set-option -add global snippets ‘MYSNIPPET’ ‘SN1’ %{ snippets-insert %{THIS IS MY SNIPPET TEXT} }

When I type ":snippets " at the command prompt, I get this snippet. But I’m not able to get your hook part to work. The snippet is just not listed. I tried using extension .md for markdown, and I tried other filetypes. Do you manually source your snippet file, or is that done from within the kakrc file? Thank you so much.

Check…

hook global BufSetOption filetype=<type> %{
	set-option -add buffer snippets ...
}

Note “-add buffer”, NOT “-add global”. The snippet is defined within the hook block.

Two: Not sure if it is just the formatting on this web page, but the quotes should be the standard ascii quotes (not the curly utf-8 punctuation glyphs) surrounding the snippet name and trigger.

If you want your snippet to be global for any buffers (no hooks required)…

set-option global snippets '<name>' '<trigger>' %{ snippets-insert %{ <string> }}

The %{ string } can easily be %sh{ shell output command }

I wonder if there is something wrong with my set up. When I put the following lines in a file (mysnippets.kak), source it, and then invoke :snippets-info, I get only one the snippet ‘CALL’, as shown in the screenshot.

set-option -add buffer snippets ‘SNIPNAME’ ‘CALL’ %{ snippets-insert %{abc xyz} }

set-option -add global snippets ‘SNIPPY’ ‘TRIG’ %{ snippets-insert %{hello world} }

hook global BufSetOption filetype=markdown %{
set-option -add buffer snippets ‘SNIP_A’ ‘CALL_A’ %{ snippets-insert %{AAA AAA} }
}

hook global BufSetOption filetype=asciidoc %{
set-option -add buffer snippets ‘SNIP_B’ ‘CALL_B’ %{ snippets-insert %{BBB BBB} }
}

set-option -add buffer snippets ‘SNIPNAME’ ‘CALL’ %{ snippets-insert %{abc xyz} }

should be within a “hook global BufSetOption …” block.

set-option -add global snippets ‘SNIPPY’ ‘TRIG’ %{ snippets-insert %{hello world} }

should be “set-option global …” (remove “-add”, and outside of any “hook global BufSetOption …” block.

The hook blocks look syntactically correct. Check the *debug* buffer… possibly an error affected the hook block. Again, if you are cutting and pasting from this web page, make sure the single/double quotes are the ascii quotes and not the curly utf-8 glyphs.

EDIT the above to remove the ‘-add’ is incorrect. If removed it will override any global snippets previously defined i.e. only one global snippet will be defined.

Hello, I’m still not able to get the hook part to work. I tried sourcing file with only your example snippets. I see that what I pasted into the post had curly quote marks but those are not in my actual file. I try here to post it:

hook global BufSetOption filetype=markdown %{
        set-option -add buffer snippets 'date'   '@da' %{ snippets-insert %sh{ date '+## %A, %d %B %Y' | tr '[:upper:]' '[:lower:]' }}
        set-option -add buffer snippets 'img'    '@im' %{ snippets-insert %{![${heading}](/images/${file}.jpg) }}
}

I don’t get any error messages and the debug buffer is empty.
Is there a way to query in kakoune what filetype one has loaded?
I wonder if the filetype is not being detected properly.

thank you

Try:

hook global BufSetOption .* %{
        set-option -add buffer snippets 'date'   '@da' %{ snippets-insert %sh{ date '+## %A, %d %B %Y' | tr '[:upper:]' '[:lower:]' }}
        set-option -add buffer snippets 'img'    '@im' %{ snippets-insert %{![${heading}](/images/${file}.jpg) }}
}

which should work for any filetype.

If you check my snippets.kak,
this can hopefully shed some further light.

Thanks for the suggestion of using .* for any filetype. This also doesn’t work on my side.

Also, when I type ":snippets-menu ", I get an error message.

Could it be because I didn’t install things correctly?
I didn’t use any plug-in manager. I just put the snippets.kak file
from Occivink in my autoload folder.
The snippets work if they are not in a hook command.
Thank you.

Having the snippets.kak file in the autoload directory should be fine.

BUT you appear to be missing the “menu” module which should be found in the ~/.config/kak/autoload/rc directory as part of the Kakoune package.

Re-installing Kakoune may be in order to make sure you aren’t missing any of the base package.

If the snippet issue persists, pare down the kakrc config to just sourcing your snippets to confirm the plugin works.

Ah, thank you. I installed Kakoune using homebrew on the mac. Is it expected that ‘menu’ be part of that installation or do we install it separately ourselves?
I haven’t tried the option of compling Kakoune from the source so far.
The directory you mention [ ~/.config/kak/autoload/rc ] is in our home directory, is that right? That directory is empty except what I put in it (the snippets.kak file from Occivink).
thank you…
(happy TGD)…

Yes, the menu module is part of the Kakoune package.

If you check the git repo you will see that you are missing all the rc directory modules… including the file detection modules (hence, your markdown filetype issues).

A complete kakoune install will probably solve your problems :slight_smile: i’m not on the mac… but you may need to do a manual install from the git repo if homebrew doesn’t have the missing modules available separately. NOTE the MacPorts install instructions on the git page.

Good luck.

(and, yes, ~/.config/kak is in your home directory).

If you have created ~/.config/kak/autoload/ and it contains only rc/snippets.kak, then that’s the only file that will be loaded. Kakoune’s “standard library” should be installed in a system-wide location (you can check it with :info %val{runtime} inside Kakoune) but Kakoune will only read it if the per-user autoload directory doesn’t exist, or if there’s a symlink in the per-user autoload directory pointing at the standard library.

See Installing Plugins on the wiki for more information.

Hello. Thanks for this advice. I deleted my autoload folder and put the snippets file in the same folder as the file I am editing, for now, to test. Now the menu command works. But the hook command is working oddly. After sourcing the following snippet file, all 3 snippets are listed and available.

hook global BufSetOption .* %{
 set-option -add buffer snippets 'SNIP1' 'T1' %{ snippets-insert %{ HELLO }}
 set-option -add buffer snippets 'SNIP2' 'T2' %{ snippets-insert %{ BYE }}
}
set-option -add buffer snippets 'SNIP3' 'S1' %{ snippets-insert %{THIS IS MY SNIPPET }}

But without the last line, none of the snippets are listed. That is, starting again and sourcing the following:

hook global BufSetOption .* %{
 set-option -add buffer snippets 'SNIP1' 'T1' %{ snippets-insert %{ HELLO }}
 set-option -add buffer snippets 'SNIP2' 'T2' %{ snippets-insert %{ BYE }}
}

neither snippet is listed.

Also, setting filetype=markdown, the snippets are not read either way.
Now the issue seems to be with the hook command?
Thanks for your advice!

If i understand you correctly, you now have no autoload directory.

Without autoloading the Kakoune modules for filetypes, etc. your config is missing (i am assuming) the file detection required for markdown (and other filetypes).

Is there a kak “rc” directory elsewhere on your system? e.g.

find /usr -type d -name kak

or perhaps from a “homebrew” root. Assuming you have a “kak/…/rc” directory (a directory containing /detection, /filetype, /tools, and /windowing module subdirectories), then

ln -s the_kak_rc_directory ~/.config/kak/autoload/rc

(The filetype/markdown.kak module sets the markdown filetype for “markdown, md and mkd” files, as well as, highlighting, etc.)

I’m not familiar with the homebrew install so don’t know where it places Kakoune’s modules and whether any modules are autoloaded. snippets.kak is obviously able to “require” the menu module (which suggests it is located off the /usr directory).

See if fixing the autoload of the standard Kakoune modules clears up the irregular snippet behaviour you’ve been experiencing.

Hello, to clarify - I deleted the autoload folder in my home folder. Using the info command I got

and this has:

% ls *
kakrc

autoload:
detection	filetype	tools		windowing

colors:
base16.kak			palenight.kak
black-on-white.kak		plain.kak
default.kak			red-phoenix.kak
desertex.kak			reeder.kak
github.kak			solarized-dark-termcolors.kak
greyscale.kak			solarized-dark.kak
gruvbox-dark.kak		solarized-light-termcolors.kak
gruvbox-light.kak		solarized-light.kak
kaleidoscope-dark.kak		tomorrow-night.kak
kaleidoscope-light.kak		zenburn.kak
lucius.kak

doc:
buffers.asciidoc		hooks.asciidoc
changelog.asciidoc		keymap.asciidoc
command-parsing.asciidoc	keys.asciidoc
commands.asciidoc		mapping.asciidoc
execeval.asciidoc		modes.asciidoc
expansions.asciidoc		options.asciidoc
faces.asciidoc			regex.asciidoc
faq.asciidoc			registers.asciidoc
highlighters.asciidoc		scopes.asciidoc

rc:
detection	filetype	tools		windowing

To autoload the standard Kakoune modules then…

ln -s /usr/local/Cellar/kakoune/2024.05.18/share/kak/rc ~/.config/kak/autoload/rc

If you removed your autoload directory, first…

mkdir ~/.config/kak/autoload

Thank you. I recreated the autoload directory and linked it using your ln -s command. Here are the results I’m getting now.

sourcing:

hook global BufSetOption filetype=markdown %{
 set-option -add buffer snippets 'SNIP1' 'T1' %{ snippets-insert %{ HELLO }}
 set-option -add buffer snippets 'SNIP2' 'T2' %{ snippets-insert %{ BYE }}
}
set-option -add buffer snippets 'SNIP3' 'S1' %{ snippets-insert %{THIS IS MY SNIPPET }}

yields

Sourcing:

hook global BufSetOption filetype=markdown %{
 set-option -add buffer snippets 'SNIP1' 'T1' %{ snippets-insert %{ HELLO }}
 set-option -add buffer snippets 'SNIP2' 'T2' %{ snippets-insert %{ BYE }}
}

yields

Sourcing:

hook global BufSetOption .* %{
 set-option -add buffer snippets 'SNIP1' 'T1' %{ snippets-insert %{ HELLO }}
 set-option -add buffer snippets 'SNIP2' 'T2' %{ snippets-insert %{ BYE }}
}
set-option -add buffer snippets 'SNIP3' 'S1' %{ snippets-insert %{THIS IS MY SNIPPET }}

yields

Sourcing:

hook global BufSetOption .* %{
 set-option -add buffer snippets 'SNIP1' 'T1' %{ snippets-insert %{ HELLO }}
 set-option -add buffer snippets 'SNIP2' 'T2' %{ snippets-insert %{ BYE }}
}

yields

In the first two examples, the markdown snippets don’t show up.
In the second two examples, the availability of the .* snippets in the hook command are not available unless another snippet is defined after the hook command. Is there another syntax for the hook command to try?
Thank you.

When you open your test.md markdown file, what does

:echo %opt{filetype}

yield? If not “markdown” (then the rc/filetype/markdown.kak appears not to be autoloading).

Try adding to your kakrc, in this case,

hook global BufCreate .*[.](md|mkd) %{ set-option buffer filetype markdown }

and recheck the filetype after opening your test file.

Assuming the file is correctly recognized as a markdown file, retest your snippet markdown hook block.