How to autocomplete Latex environment?

I am sorry if this has been asked before but I cannot find anything related. I tried reading through the docs but the documentation for hooks are very lacking.

I work with Latex and I would to autocomplete environment. I’d like Kakoune to do the following, when I type,

\begin{itemize

and press }, kakoune should execute the flowing <esc>Xypllwcend<esc>O that does the following

  • <esc> go to normal mode
  • X select the entire line, y copy it and p put it below.
  • ll to move to the first letter of begin on the pasted line.
  • ‘w’ to select begin, c to replace and get to edit mode, and put end
  • <esc> to go to normal mode and enter edit mode on the above line.

This has become routine and I do it continuously everyday. Can this be automated for adding any environment not just itemize? If so, please tell me how? Thank you.

hook global InsertChar '\}' %{
  try %{
    evaluate-commands -draft %{
      # match your criteria
    }
    # if passing, execute your commands
  }
}

You can add static_words for completion:

set-option global static_words itemize1 itemize2

I use kakoune snippets for this usecase.
The latex snippet which triggers on \begin would solve your problem.
Have a look at kakoune snippets collection

It seems that development of snippet plugin stopped. Are there an active fork or any plans on making one?

I’m not sure that development has stopped. It is true that there has not been any activity for a while, but there are not a lot of open pull requests or something.
I think occivink just does not have the time to work on it.

For myself, the plugin is still working fine, so I also do not feel the need to work on it.

Can you give the entire thing that I can paste in my kakrc? I don’t known kak’s script. Thanks.

hook global InsertChar '\}' %{
  try %{
    execute-keys -draft "<esc>x<a-k>\\begin\{\w+\}<ret>"
    execute-keys "<esc>Xypllwcend<esc>O"
  }
}

Thank you. This worked. What does <esc>x<a-k> do? Also, how to limit this to only tex files?

It selects the line and keeps the selection if it matches the regex \\begin\{\w+\}. If not, it drops this selection. If it is the only selection, it throws an error and the next command in this try block is not executed.

This page might be helpful:
https://delapouite.github.io/kakoune-explain/#<esc>x<a-k>\\begin\{\w%2B\}<ret>

hook global WinSetOption filetype=latex %{
  hook buffer InsertChar '\}' %{
    try %{
      execute-keys -draft "<esc>x<a-k>\\begin\{\w+\}<ret>"
      execute-keys "<esc>Xypllwcend<esc>O"
    }
  }
}

For the snippets, I updated my implementation. They rely on the filesystem and trigger in a matching pair, similarly to typed expansions.

There is currently support for Crystal, HTML, JavaScript, Kakoune, Markdown and sh.

I think I mentioned in one issue that I’m not particularly happy with the complexity of the plugin, and I haven’t integrated it nicely in my workflow so it is indeed stalled. I’m currently thinking back to a simpler state (see the trimmed branch) but I also understand that people rely on features. If somebody wants to keep a fork of the current state, go ahead.

Relying on an option to store the snippets make implementation harder I think, for escaping, to update the triggers on option change, handle aliases, to have both the current filetype and global snippets or compose with multiple user contributed snippets. Using the filesystem abstracts the complexity of the data structure, we just have to check if the snippet file exists in one of the directory locations; if we want auto-generate, alias or reuse certain snippets for other languages, it is just a file or a symbolic link.

Can I ask why not to support existing and widespread, understood by most editors and tools like LSP, snippet placeholder syntax?

I do not know about the full syntax. I’m not opposed to support it.

1 Like

Here’s the grammar for snippets as defined in Language Server Protocol. It is compatible with TextMate snippets, and mostly compatible with Vim UltiSnips, Emacs Yasnippet, fully supported by VSCode, has partial support by Atom, Kate, Gedit: https://github.com/microsoft/language-server-protocol/blob/master/snippetSyntax.md#grammar

It looks complicated.

because it is versatile. Placeholder design is simple, but not easy.