jpalardy / vim-slime

A vim plugin to give you some slime. (Emacs)
http://technotales.wordpress.com/2007/10/03/like-slime-for-vim/
MIT License
1.87k stars 227 forks source link

idea: I would like to switch between tmux and nvim #378

Open mmngreco opened 1 year ago

mmngreco commented 1 year ago

Hi there!

Thank you for the amazing work done here. I'm wondering if there's something that I could do to switch between tmux and nvim in the same session.

My using case is I'm start working in nvim then I start ipython, sending commands and then I have doubts if a code will work the same in my server so I create a new tmux pane, sshing to the server, start a new ipy and then I would like to send the same code block to this new ipython.

I though that it could be done with this:

local function slime_use_tmux()
  vim.g.slime_target = "tmux"
  vim.g.slime_bracketed_paste = 1
  vim.g.slime_python_ipython = 0
  vim.g.slime_no_mappings = 1
  vim.g.slime_default_config = {socket_name="default", target_pane=":.2"}
  vim.g.slime_dont_ask_default = 1
end

local function slime_use_neovim()
  vim.g.slime_target = "neovim"
  vim.g.slime_python_ipython = 0
  vim.g.slime_bracketed_paste = 1
  vim.g.slime_default_config = nil
  vim.g.slime_no_mappings = 1
  vim.g.slime_dont_ask_default = 0
end

vim.g.slime_target = "neovim"
local function toggle_slime_target()
  if vim.g.slime_target == "neovim" then
    slime_use_tmux()
  else
    slime_use_neovim()
  end
end
vim.api.nvim_create_user_command('SlimeToggleTarget', toggle_slime_target, { nargs = 0 })

however, I can't make it works. The first time I've used neovim but when I switch to tmux it raises the following error:

Error detected while processing function slime#send_range[6]..slime#send[13]..<SNR>94_SlimeDispatch[6]..<SNR>94_TmuxSend[24]..<SNR>94_TmuxCommand:
line    1:
E716: Key not present in Dictionary: "socket_name"
line    6:
E121: Undefined variable: l:socket
line    7:
E121: Undefined variable: l:socket_option
E116: Invalid arguments for function system
Error detected while processing function slime#send_range[6]..slime#send[13]..<SNR>94_SlimeDispatch[6]..<SNR>94_TmuxSend:
line   26:
E716: Key not present in Dictionary: "target_pane"
E116: Invalid arguments for function shellescape
E116: Invalid arguments for function <SNR>94_TmuxCommand
line   34:
E716: Key not present in Dictionary: "target_pane"
E116: Invalid arguments for function shellescape
E116: Invalid arguments for function <SNR>94_TmuxCommand
Press ENTER or type command to continue

Can be this done? I'm wasting time with this?

goddoe commented 1 year ago

Good idea. I'm new to Neovim as well, and I also want to solve this issue.

I tried what you did and encountered the same error. I realized that this happens when the required keys for each setting in the slime_default_config are not present.

I'm not sure about the initialization process of slime_default_config or when it is initialized.

This solution below will fix the error you reported, but it's not a perfect solution. You will still need to specify the target_pane or jobid by using ctrl+c, ctrl+v when you switch.

vim.opt.shell = "zsh"
vim.g.slime_target = "tmux"
vim.g.slime_python_ipython = 1
vim.g.slime_default_config = {socket_name="default", target_pane="", jobid=""}

function toggle_slime_target()
  if vim.g.slime_target == "neovim" then
    vim.g.slime_target = "tmux"
  else
    vim.g.slime_target = "neovim"
  end
end

vim.keymap.set('n', '<Leader>sl', toggle_slime_target)
jpalardy commented 1 year ago

Hi there,

I think the relevant pieces are

  1. the binding that prompts for config explicitly

Screen Shot 2023-05-11 at 21 17 05

  1. The "config resolver" that might grab config for a few places

Screen Shot 2023-05-11 at 21 20 26

  1. Eventually, a specific target configurator

Screen Shot 2023-05-11 at 21 18 14


I would say the key to switching target/config:

Let me know how that goes

lkhphuc commented 6 months ago

For those interested, I create this user command which I put in the init function of lazy.nvim:

      vim.api.nvim_create_user_command('SlimeTarget', function (opts)
        vim.b.slime_config = nil
        vim.b.slime_target = opts.args
      end, { desc = "Change Slime target", nargs = '*'})

You can use this as SlimeTarget neovim or SlimeTarget wezterm which will be put into the buffer local vim.b.slime_target. After this, you can run SlimeConfig again to be prompted for the new target.