ToggleTermSendCurrentLine
and ToggleTermSendVisualSelection
for Snacks.terminal
#397
LintaoAmons
started this conversation in
Ideas
Replies: 1 comment
-
You can use this as a starting point. The important part is that you can send anything to the terminal over a channel with local buf = vim.api.nvim_get_current_buf()
local repl = term.toggle(cmd, vim.tbl_deep_extend('keep', opts, repl_opts))
if send_key then
vim.keymap.set('v', send_key, function()
vim.schedule(function()
vim.api.nvim_chan_send(vim.b[repl.buf].terminal_job_id, table.concat(visual.get_vsel_text(), '\n') .. '\n')
visual.exit_vmode()
end, { desc = 'Send visual selection to REPL', buffer = buf })
end)
end I have --- Returns the text selected in visual mode in the current buffer.
--- @return string[]: The text selected in visual mode in the current buffer as a list of lines. You can call
--- `table.concat` to get the true text of the selection.
function M.get_vsel_text()
local vsel = M.get_vsel()
local pos = vsel.pos
if vsel.mode == 'v' then
return vim.api.nvim_buf_get_text(0, pos[1] - 1, pos[2] - 1, pos[3] - 1, pos[4], {})
elseif vsel.mode == 'V' then
return vim.api.nvim_buf_get_lines(0, pos[1] - 1, pos[3], true)
end
error('Invalid mode: ' .. vsel.mode, 2)
end and function M.exit_vmode()
local esc = vim.api.nvim_replace_termcodes('<esc>', true, false, true)
vim.api.nvim_feedkeys(esc, 'x', false)
end Good luck! I can answer any questions that you may have. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Those two function is super useful when I want to send lines to terminal
in markdown files
or random lines in a bash file
or languages REPL
Beta Was this translation helpful? Give feedback.
All reactions