How to translate this to vim.keymap.set?

Hi!

I’m trying to convert a couple maps from vim.cmd to vim.keymap. I’ve tried several things but haven’t been able to get them to work.

  vim.cmd([[imap <expr> <C-l> vsnip#available(1) ? '<Plug>(vsnip-expand-or-jump)' : '<C-l>']])
  vim.cmd([[smap <expr> <C-l> vsnip#available(1) ? '<Plug>(vsnip-expand-or-jump)' : '<C-l>']])

What I have tried so far that doesn’t work is

  vim.keymap.set({ "i", "s" }, "<C-l>", function()
    if vim.fn["vsnip#available"](1) == 1 then
      return "<Plug>(vsnip-expand-or-jump)"
    else
      return "<C-l>"
    end
  end, { buffer = bufnr, expr = true, remap = true })

I have also tried

vim.keymap.set({"i", "s"}, "<C-l>", [[vsnip#available(1) ? '<Plug>(vsnip-expand-or-jump)' : '<C-l>']], {buffer = bufnr, expr = true, remap = true})

Thanks for the help!

What did you pass as bufnr?

This is called inside the attach handler for nvim lsp, so it should be the buffer handle for the current buffer.

The mapping is definitely being called, it just either does nothing, or inputs <Plug>(vsnip-expand-or-jump) as text into the buffer.

Try the one below, I somewhat got it to work for vsnip#jumpable() function but should be the same for this one hopefully:

vim.keymap.set(
  {"i", "s"},
  "<C-l>",
  [[vsnip#available(1) ? "\<Plug>(vsnip-expand-or-jump)" : "\<C-l>"]],
  { buffer = bufnr, expr = true, replace_keycodes = true, remap = true }
)