2

I'm configuring toggleterm.nvim, and in particular I'm putting this keybinding in my config file, as suggested by the plugin author here:

-- Send motion to terminal
vim.keymap.set("n", "<leader>im", function()
  set_opfunc(function(motion_type)
    require("toggleterm").send_lines_to_terminal(motion_type, false, { args = vim.v.count })
  end)
  vim.api.nvim_feedkeys("g@", "n", false)
end)

My understanding is that <leader>imw should send a word to the terminal.

But when I issue <leader>im I get:

E5108: Error executing lua: .../USERNAME/.config/nvim/lua/plugins/config/toggleterm.lua:83: attempt to call global 'set_opfunc' (a nil value)
stack traceback:
    .../USERNAME/.config/nvim/lua/plugins/config/toggleterm.lua:83: in function <.../USERNAME/.config/nvim/lua/plugins/config/toggleterm.lua:82>
2
  • FWIW, in Vim you would do something like :set opfunc=functionName
    – D. Ben Knoble
    Feb 14 at 21:53
  • See :h 'opfunc' Feb 15 at 7:08

1 Answer 1

1

The set_opfunc need to be defined first:

local set_opfunc = vim.fn[vim.api.nvim_exec([[
  func s:set_opfunc(val)
    let &opfunc = a:val
  endfunc
  echon get(function('s:set_opfunc'), 'name')
]], true)]
-- Send motion to terminal
vim.keymap.set("n", "<leader>im", function()
  set_opfunc(function(motion_type)
    require("toggleterm").send_lines_to_terminal(motion_type, false, { args = vim.v.count })
  end)
  vim.api.nvim_feedkeys("g@", "n", false)
end)

See this post for more information.

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.