5

I recently decided to switch my init.vim to init.lua as I'm using Neovim and opted to use LuaSnip over UltiSnips - previously, CoC autocomplete and UltiSnips were clashing over the same tab triggers. I'm currently writing a script to convert some of my LaTeX snippets from UltiSnips to the LuaSnip format. Most of the snippets are easy to convert; however, I came across a couple issues with a few specific types of snippets:

  • Pre-expand snippets to expand tables and matrices. Theses use Python to generate the snippet based on input (e.g. I have (b|p|some other letters)mat[r].[c]a? to create a (optionally augmented) rxc matrix in LaTeX).
  • Contextual Snippets, which depend on the environment and have functions to evaluate the environment (e.g. I have snippets to generate bullet points in itemize and enumerate environments in LaTeX).
  • Auto-expand snippets (iA snippets in UltiSnips). Currently, snippets in LuaSnip require tab completion.

My question is: is it possible to move all these configurations to the LuaSnip format?

My thought is that instead of using Python to pre-expand snippets and to evaluate the environment, perhaps I could write the functions in Lua; however, I'm not sure if that is the correct approach. If anyone has done this before and has some insight, that would be great. Thanks!

1 Answer 1

2

After some Googling and some help on Reddit (huge thanks to L3MON4D3, u/Doltonius), here is what I have so far:

  • Pre-expand is possible; however, this needs to be configured. L3MON4D3 has a set of matrix/table snippets and I'll try to see if I can modify those for my use as well (e.g. adding augments) and update the post when I have something working.
  • For contextual snippets, this can be done using conditions. Creating a conditional will probably be quite similar to the Python functions used in UltiSnips, but now in Lua. Again, I'll update the post when I have created working functions.

Check if you are in a math environment:

local function math():
    return vim.api.nvim_eval('vimtex#syntax#in_mathzone()') == 1
end
  • Auto-expand snippets can be done (here for more info) Make sure to enable autoexpand in the config like so (just putting it here since I had a bit of a hard time finding out how to set the config):
ls = require "luasnip"
ls.config.set_config({
    enable_autosnippets = true,})
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.