When in embedded terminal, use current neovim instance as EDITOR

After switching to Neovim, I have been using the embedded terminal quite a lot. It’s really an awesome productivity boost to be able to press <Leader>g and open lazygit in an embedded terminal in a new tab. (nnoremap <Leader>g :tabnew<CR>:terminal lazygit<CR>i)

One small issue I’m having though - Any time I run a command that edits something in an external editor using the EDITOR environment variable, it opens a new Neovim instance, inside the embedded terminal, which is running inside a Neovim instance… It’s enough to make me dizzy.

image

Is there a value I can set EDITOR to inside the embedded terminal that will make commands like git open a new tab inside my existing Neovim instance, rather than spawning a new Neovim instance?

Even more awesome, what about a command that would spawn a buffer in a Neovim floating window on top of the embedded terminal? That would be friggin sweet.

Considering the crazy amount of awesome plugins that exists for Neovim, I would be surprised if this hasn’t already been done. Does anybody have any leads? :slightly_smiling_face:


On second though, this is probably something that could/should be built into Neovim. Any time you spawn a terminal, Neovim could set the EDITOR environment variable to be a command that would spawn a new tab in the current Neovim instance for editing. This should be hidden behind an option though, of course.

how about a script like this?

#!/bin/sh

if [ -n "$NVIM_LISTEN_ADDRESS" ] ; then
  nvr "$@"
else
  exec nvim "$@"
fi

and in your sh/bash/zsh export EDITOR and VISUAL as that script?

As I suspected, there was a perfect solution out there :grin: neovim-remote looks like exactly what I was looking for.

I played around with neovim-remote a bit, and this single line in my vimrc seems to completely solve my problem:

let $EDITOR='nvr --nostart --remote-tab-wait +"set bufhidden=delete"'

No need to write any scripts or edit any of my dotfiles. This causes EDITOR in embedded terminals to open a new tab in the current Neovim instance, and as soon as the tab is closed the buffer is deleted (thanks to set bufhidden=delete), so nvr returns and the application can continue. Tested and works perfectly with git commit.

:tada: :tada: :tada:

1 Like

I do need the script because I use ranger from within nvim, without ranger plugins. And when i try to launch a text document from within that ranger, I need it to launch using nvr. Which means I need VISUAL to be exported to nvr.

But at the same time I dont want ranger to use nvr when called from outside of nvim instance. So this is just a convenient check

Why not? It seems like nvr is written for this exact use case in mind. If NVIM_LISTEN_ADDRESS does not exist, it will launch a new nvim instance instead.

Well yes if the variable NVIM_LISTEN.. isnt there nvr launches nvim but it also prints this in console/stdout

[!] Can't connect to: /tmp/nvimsocket

    The server (nvim) and client (nvr) have to use the same address.

    Server:

        Expose $NVIM_LISTEN_ADDRESS to the environment before
        starting nvim:

        $ NVIM_LISTEN_ADDRESS=/tmp/nvimsocket nvim

        Use `:echo v:servername` to verify the address.

        Security: When using Unix domain sockets on a multi-user system,
        the socket should have proper permissions so that it is only
        accessible by your user.

    Client:

        Expose $NVIM_LISTEN_ADDRESS to the environment before
        using nvr or use its --servername option. If neither
        is given, nvr assumes "/tmp/nvimsocket".

        $ NVIM_LISTEN_ADDRESS=/tmp/nvimsocket nvr file1 file2
        $ nvr --servername /tmp/nvimsocket file1 file2
        $ nvr --servername 127.0.0.1:6789 file1 file2

    Use -s to suppress this message.

[*] Starting new nvim process using $NVR_CMD or 'nvim'.

    Use --nostart to avoid starting a new process.

That small script avoids that. No harm done with that output, just this seems cleaner

Wouldn’t the -s flag for nvr also fix it? :grinning_face_with_smiling_eyes: