Back Original

Ergonomic SSH development

-- Complete words from all other tmux panes.
function _G.tmux_complete(findstart, base)
  if findstart == 1 then                         -- locate start of the keyword
    local line, col = vim.fn.getline("."), vim.fn.col(".") - 1
    local s = col
    while s > 0 and line:sub(s, s):match("%S") do s = s - 1 end
    return s
  end
  if not vim.env.TMUX then return {} end
  local words, seen = {}, {}
  local self = vim.env.TMUX_PANE                  -- skip nvim's own pane
  for _, pane in ipairs(vim.fn.systemlist({ "tmux", "list-panes", "-a", "-F", "#{pane_id}" })) do
    if pane ~= self then
      local text = vim.fn.system({ "tmux", "capture-pane", "-p", "-J", "-S", "-", "-t", pane })
      for w in text:gmatch("%S+") do
        if #w > 2 and not seen[w]
           and (base == "" or w:sub(1, #base):lower() == base:lower()) then
          seen[w] = true
          words[#words + 1] = { word = w, menu = "[tmux]", dup = 0 }
        end
      end
    end
  end
  return words
end

vim.o.completefunc = "v:lua.tmux_complete"
vim.opt.complete:append("F")   -- F = use 'completefunc' as a <C-n> source