zbirenbaum / nvterm

NvChad's Official Terminal Plugin ( Unmaintained but still usable and stable), wait for v3.0

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Send selected Lines/current line to terminal

DaitiDay opened this issue · comments

Hi everyone,
I'm kinda a noob trying to setup neovim so I apoligize if my question it's trivial/stupid, but I tried to solve my problem on my own with no results. As for the title, is there a way to send to the terminal selected lines of code (or the current line under the cursor)?
I tried searching for the "current line" alternative to "%" to use in ft_cmds but I found nothing so far.
Thank you for tour help and again, I'm sorry for the stupid question.

EDIT: I'm trying using getline('.') instead of expand('%') right now, if someone has a better alternative let me know

Your question isn't trivial or stupid at all, and the visual select portion doesn't even have a good answer atm, as marks are still on the api 'todo list' last I checked. Don't feel bad about asking questions like this!

I wrote code for both of the above, but ran into some annoying bug with the vselect part where the first time I do it, it only sends one of the lines (first or last) but if go to normal mode and then go back to visual and select the same lines it works fine, but this works perfectly well for single line sending based on where your cursor is.

I took the implementation for getting vselect range directly from nvim-treesitter, so I'm not sure what the issue there is. Feel free to debug it if you would like.

local a = vim.api
require("nvterm").setup()
local term = require("nvterm.terminal")

local send_line_to_term = function ()
  local line = a.nvim_win_get_cursor(0)[1]
  local line_text = a.nvim_buf_get_text(0, line-1, 0, line-1, -1, {})[1]
  term.send(line_text)
end

local send_vselect_to_term = function ()
  local function visual_selection_range()
    local _, csrow, cscol, _ = unpack(vim.fn.getpos "'<")
    local _, cerow, cecol, _ = unpack(vim.fn.getpos "'>")
    local start_row, start_col, end_row, end_col

    if csrow < cerow or (csrow == cerow and cscol <= cecol) then
      start_row = csrow
      start_col = cscol
      end_row = cerow
      end_col = cecol
    else
      start_row = cerow
      start_col = cecol
      end_row = csrow
      end_col = cscol
    end
    return start_row, start_col, end_row, end_col
  end
  local start_row, _, end_row, _ = visual_selection_range()
  local line_text = a.nvim_buf_get_text(0, start_row-1, 0, end_row-1, -1, {})
  local fmt_text = table.concat(line_text, ' \\\r')
  term.send(fmt_text)
end

vim.keymap.set({"v"}, "<C-l>", send_vselect_to_term, {noremap = true})
vim.keymap.set({"n"}, "<C-l>", send_line_to_term, {noremap = true})

Thank you very much for the help!

This seems to work for me:

-- Echo selected text
["<leader>tr"] = {
  function()
    -- yank into " register
    vim.cmd("normal! y")

    -- get the " register
    local text = vim.fn.getreg('"')

    local cmd = "echo " .. text
    require("nvterm.terminal").send(cmd, "vertical")
  end,
  "echo selection",
},