0

I am using nvim-cmp and luasnips and I have a _skel snippet, I would like to create an autocmd to trigger this snippet for New Files:

I already have some code:

local status_ok, packer = pcall(require, "packer")
if not status_ok then
    return
end

if not packer_plugins['LuaSnip'] and not packer_plugins['LuaSnip'].loaded then
    --print('LuaSnip is loaded')
    return
end

function nvim_create_augroups(definitions)
    for group_name, definition in pairs(definitions) do
        vim.api.nvim_command("augroup " .. group_name)
        vim.api.nvim_command("autocmd!")
        for _, def in ipairs(definition) do
            local command = table.concat(vim.tbl_flatten({ "autocmd", def }), " ")
            vim.api.nvim_command(command)
        end
        vim.api.nvim_command("augroup END")
    end
end


local autocmds = {
    autoskel = {
        {"BufNewFile", "*.lua,*.sh", 'call feedkeys("i_skel\<tab>\<enter>")' },
    },
}

If I run this command it works, but only in command line:

:lua vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("i_skel<tab><enter>",true,false,true),'m',true)

this also works:

:call feedkeys("i_skel\<Tab>\<Enter>")

2 Answers 2

1

Of course, this won't work as buffer is active (i.e. has keyboard focus) only between BufEnter and BufLeave events. You can't just put code into a random autocmd and hope for the best.

So either catch BufEnter (but beware that it may occur multiple times), or don't use the dreaded feedkeys at all.

4
  • Instead of feedkeys what would you say is a good choice? I am trying to reproduce this solution using lua Jun 3, 2022 at 11:15
  • @SergioAraujo If the plugin provides api then use api. But, honestly, I don't understand what is good in pulling the huge plugin in if it only takes few lines to read skeleton file in and then apply evaluation procedure if some sort of "macro expansion" required.
    – Matt
    Jun 3, 2022 at 12:34
  • By the way, I run: vim test.lua. The advantage of snippets is that they are dinamic nos static, If I want for example add specific info on the scrip headlines. Jun 4, 2022 at 12:26
  • @SergioAraujo As I said, it's pretty easy to add dynamic evaluation of snippet text wthout external plugins. It takes about 1 command.
    – Matt
    Jun 4, 2022 at 12:38
0

here's how I do it:

vim.api.nvim_create_autocmd("BufNewFile", {
    callback = function()
        -- another autocmd so it doesn't conflict 
        -- with other plugins that like to insert text on file open
        vim.api.nvim_create_autocmd("VimEnter", {
            callback = function()
                -- if buffer is empty
                if vim.fn.line("$") ~= 1 or vim.fn.getline(1) ~= "" then
                    return
                end
                local snips = require("luasnip").get_snippets()[vim.bo.ft]
                for _, snip in ipairs(snips) do
                    if snip["name"] == "_skel" then
                        require("luasnip").snip_expand(snip)
                        return true
                    end
                end
            end,
        })
    end,
})

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.