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

Thoughts on using in ssh? #42

Open
dusty-phillips opened this issue Apr 14, 2023 · 12 comments
Open

Thoughts on using in ssh? #42

dusty-phillips opened this issue Apr 14, 2023 · 12 comments
Labels
help wanted Extra attention is needed

Comments

@dusty-phillips
Copy link

I often ssh into a system and run neovim inside the ssh terminal. As a result, pass_keys.py is unable to determine whether the ssh window is a vim window or not.

I use kitty + kitten ssh to ssh into the target system. The foreground_process contains a long and complex ssh command. But since I used kitty`s ssh kitten, I'm wondering if it might be possible to ask it what process is running inside the ssh session and let the keys pass through in that case. Know of any way to support this?

@knubie
Copy link
Owner

knubie commented Apr 15, 2023

Seems reasonable, but I don't know enough about kitty or the ssh kitten to offer any suggestions. I'd be more than happy to accept a pull request, though, if it's something you would like to work on.

@knubie knubie added the help wanted Extra attention is needed label Apr 15, 2023
@dusty-phillips
Copy link
Author

I’ve poked through the ssh sources and can’t find anything immediately applicable, but it feels like the plumbing is there to support it. Will try to do a deeper dive if I can find the time.

@gzagatti
Copy link

See discussion here which should give you some help: kovidgoyal/kitty#6229

@dusty-phillips
Copy link
Author

Tested a heavily customized (my titles use a lot of aliases...) variation on gzagatti's post and was able to get pass_keys.py to detect a vim window inside ssh.

However, now neighbouring_window.py, when called from the vim session inside ssh, is not able to pass the command out to kitty outside ssh. Will try to debug further this weekend.

@gzagatti
Copy link

I know what's happening.

The problem is that now your keys are being passed to nvim. However when you issue the command from nvim, the command fails. Under the hood, nvim is trying to call neighboring_window.py. You can emulate this call in nvim from the command line:

> :echo system("kitty @ kitten neighboring_window.py bottom")

If your kitty.conf file has allow_remote_control yes, then you'll likely get an error:

Traceback (most recent call last):                                                                                                            
  File "runpy.py", line 197, in _run_module_as_main                                                                                           
  File "runpy.py", line 87, in _run_code                                                                                                      
  File "kitty_main.py", line 7, in <module>                                                                                                   
  File "kitty/entry_points.py", line 206, in main                                                                                             
  File "kitty/entry_points.py", line 23, in remote_control                                                                                    
  File "kitty/remote_control.py", line 504, in main                                                                                           
  File "kitty/remote_control.py", line 314, in do_io                                                                                          
  File "kitty/utils.py", line 528, in __enter__                                                                                               
OSError: Failed to open controlling terminal: /dev/tty (identified with ctermid()) with error: No such device or address                      
                                                                                                                                              
Press ENTER or type command to continue  

The problem here is that nvim does not spawn commands connected to the tty. Check this discussion thread.

So what can you do? One option is to reverse bind the kitty socket to the remote server:

> kitty +kitten ssh ids-dgx1 -R ${KITTY_LISTEN_ON#unix:}:${KITTY_LISTEN_ON#unix:}

Then set the env variable KITTY_LISTEN_ON to point to the socket in the remote server. With all that up, you'll have the plugin working.

In that case, you might want to set allow_remote_control socket-only in your kitty.conf file.

Please let me know if you find a simpler setup.

@gzagatti
Copy link

Also see issue mrjones2014/smart-splits.nvim#103. You'll likely want to use user var as kitty has recently introduced it.

@kovidgoyal
Copy link

Just FYI, some recent additions to kitty to make this easier:

The ssh kitten can now automatically forward the kitty remote control socket by setting forward_remote_control yes in ~/.config/kitty/ssh.conf

And you dont need neighboring_window.py anymore, you can just do

kitten @ focus-window --match neighbor:left

Change left to right/top/bottom as needed. You will need to run from master/nightly for these till the next kitty release.

@gzagatti
Copy link

That's great! I'll test once the new Kitty version comes out.

@AniAggarwal
Copy link

Did you end up getting a working solution with the new kitty? I tried following along the various threads and discussions you were on but wasn't able to recreate something working myself 😔

@gzagatti
Copy link

gzagatti commented Jun 2, 2024

No, at the end I just kept my setup above because it was working pretty well already. I also did not quite figure out how to use the new feature.

@carlos-algms
Copy link

It works partially for me.
The navigation between Kitty panes still works, but not inside Neovim 😢. All moves inside neovim are gone on SSH, UP, RIGHT, DOWN, and LEFT.

@carlos-algms
Copy link

carlos-algms commented Nov 16, 2024

I've found a solution after considering the SetUserVar implementation from @kovidgoyal from this comment

We can set a variable when nvim opens, and unset when it closes via auto-cmd.

First set the user variable:

return {
    "knubie/vim-kitty-navigator",
    build = "cp ./*.py ~/.config/kitty/",
    config = function()
        vim.fn.system("kitten @ set-user-vars IS_NVIM=true")

        -- VimLeavePre event was also suggested by KovidGoyal
        vim.api.nvim_create_autocmd("VimLeavePre", {
            callback = function()
                vim.fn.system("kitten @ set-user-vars IS_NVIM=false")
            end,
        })
    end,
}

Second, adjust is_window_vim in the file pass_keys.py

def is_window_vim(window, vim_id):
    if window.user_vars and window.user_vars.get('IS_NVIM') == 'true':
        return True

    fp = window.child.foreground_processes
    return any(re.search(vim_id, p['cmdline'][0] if len(p['cmdline']) else '', re.I) for p in fp)

Let me know if that also works for someone else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants