Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
asdf8601 opened this issue May 11, 2023 · 3 comments
Open

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

asdf8601 opened this issue May 11, 2023 · 3 comments

Comments

@asdf8601
Copy link

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
Copy link

goddoe commented May 11, 2023

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
Copy link
Owner

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:

  • switch the slime_target (as per your own code)
  • unlet b:slime_config if it's set
  • delegate to slime#config (explicitly or implicit) to re-configure

Let me know how that goes

@lkhphuc
Copy link

lkhphuc commented Mar 22, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants