Is it possible to trigger a snippet with a custom keymap instead of physically typing the trigger words? #389
Replies: 2 comments 7 replies
-
Oh yes, that's very possible :D
First a module that maps key -> snippet. -- in lua/keymap_snippets.lua
-- add snippet function-definitions first.
ls.setup_snip_env()
return {
"snip1" = parse("trigger", "expanded with <C-D>")
} Then a helper-function for the keymaps. -- in your config:
local function map_snippet(keys, snippet_key)
vim.keymap.set("i", keys, function()
require("luasnip").snip_expand(require("keymap_snippets")[snippet_key])
end)
end
map_snippet("<C-D>", "snip1") You could also use the keys for accessing the snippets as key-combinations ( |
Beta Was this translation helpful? Give feedback.
-
Hoping I can get some help with this. I'm also trying to get this working, but has something perhaps changed in the api since last posted? ~/.config/nvim/lua/tjex/keybind-snips.lua: ls.setup_snip_env()
return {
s(
"print",
fmt(
[[
fmt.Println({})
]],
{ i(1) }
)
),
}
require("luasnip.loaders.from_lua").load { paths = "~/.config/nvim/snippets/" }
-- other configs
local function map_snippet(keys, snippet_key)
vim.keymap.set("i", keys, function()
require("luasnip").snip_expand(require("tjex.keybind-snips")[snippet_key])
end)
end
map_snippet("<C-k>", "print") But I get this error message:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First of all I'd like to thank LuaSnip developers for the Amazing Plugin!
I've been wondering for a while is it possible to trigger a snippet with a custom keymap.
For example, in Lua: "local xxx = yyy". It would be much easier to press a key combination like instead of physically typing out the trigger to expand the snippet!
Maybe we can implement a function like:
callLuaSnippet("trigger")
With this type of custom mapping available, we could map any key combinations we want! And reduce our cognitive load!
Beta Was this translation helpful? Give feedback.
All reactions