Equivalent of vim ftplugin?

Hi all! vim’s ftplugin capability allows you to stick <filetype>.vim files in .vim/ftplugin and are called whenever a buffer’s filetype matches (e.g. .vim/ftplugin/sql.vim is loaded when a buffer’s filetype is set to sql).

I know kakoune supports “doing stuff” only on certain filetypes (e.g. How to Run Terminal Command on the Active File on Write?) but do all these statements go into a single kakrc?

To be explicit, I’m asking one and a half questions: is it idiomatic to have a single kakrc, if not, is there an equivalent to vim’s ftplugin? :slight_smile:
Thanks!

As explained in the FILES section of man kak, on startup Kakoune reads all of the *.kak files/scripts present in your autoload directory (or any subdirectory of it). If you have no custom autoload directory, then Kakoune falls back on the default, system-wise autoload directory, which you can probably find as /usr/share/kak/autoload on your system.

Filetype-specific scripts are to be found somewhere in the autoload directory. Have a look at any of the files provided under /usr/share/kak/autoload (e.g., python.kak) and you’ll understand how they work. These files include a detection section:

hook global BufCreate .*py %{ set-option buffer filetype python }

[Edit: I had to tweak the actual regex, because for some reason it does not show up correctly on screen.]

with a hook (type :doc hooks inside Kakoune to learn about hooks) that will fire whenever you open a file named .*py (the parameter after BufCreate is a regex).

These files also include an initialization section with all of the file-type specific stuff you want to use:

hook global WinSetOption filetype=python %{ …

This usually includes further hooks and commands for filetype-specific indentation, etc.

In sum, there is no need for a specific ftplugin subdirectory of autoload. All filetype-specific commands are activated by filetype-specific hooks.

(Of course, physically speaking, you could cram all of this in a single kakrc file, but no one would recommend it :-))

2 Likes

thanks @ftonneau!

Probably because normal text is parsed as Markdown, and Markdown’s special characters overlap with regex special characters.

If you start a line with four spaces:

    hook global BufCreate .*[.](py) %{

…or use triple-backticks to start and end a block

```
hook global BufCreate .*[.](py) %{
    set-option buffer filetype python
}
```

…then you can put whatever characters you like and Markdown won’t mess with them.

1 Like