Ok, after more googling, I found that the following lua command works:
vim.keymap.set(
'n',
'<leader><leader>r',
function ()
return ':ToggleTerm<cr>' .. 'my_interpreter ' .. vim.fn.expand("%") .. '<cr>'
end,
{ expr = true}
)
Put this in your user specific init.lua
, or do :lua the_command_from_above
inside nvim.
See also the neovim docs on vim.keymap.set.
And as per Friedrich's request in the comments, something about how this works, or why it's equivalent to the other answer:
- the
'n'
means the mapped key works in normal mode, and vim.keymap.set
is automatically nore
- the anonymous function is called every time
- and
expr = true
tells vim that this is a map-expression
But to be fair, I'm not entirely sure why this must be a function, and not a string. If I just use a string, then I get undefined variable ":ToggleTerm"
🤷♂️
:terminal
or:echo
in their mapping.%
specially::help cmdline-special