Neovim terminal and @edit

I often use julia inside the neovim terminal. One annoyance I have with this is that say @edit sin(1) will open that code in a new instance of neovim. Instead, I would prefer it to use the current instance. Is that possible?

2 Likes

I don’t believe there are any options to do this with the REPL as it is. But you may be able to define a new macro which essentially copies @edit, but instead of opening an editor it could use this library to connect to an existing neovim instance

1 Like

That is an interesting suggestion. I wonder how vscode handles this. If you do @edit in the repl there, it will open in the running vscode instance or not?

Perhaps clientserver - How can I use --servername and --remote in neovim? - Vi and Vim Stack Exchange might give a suggestion how to do this

1 Like

I know @kdheepak is an expert in this area; see also (but not Julia-specific) kdheepak - Vim, tmux and zsh — the productivity trio

1 Like

Thanks for pinging me @jd-foster!

@jw3126 it is mostly straightforward to do this:

  1. Install neovim-remote (I use pip install neovim-remote).
  2. Add the following to your ~/.bashrc
if [ -n "$NVIM_LISTEN_ADDRESS" ]; then
    export VISUAL="nvr -cc split --remote-wait +'set bufhidden=wipe'"
else
    export VISUAL="nvim"
fi

# Make neovim the default editor.
export EDITOR="$VISUAL"

Restart your terminal and you should be good to go!

Explanation

nvr is a python package to access a powerful feature of neovim: remote neovim command. And you can do many fancy things with it. It basically lets you remotely control any neovim instance if you know the socket address.

When you are inside Neovim, the NVIM_LISTEN_ADDRESS environment variable is set. So if that variable is set, use nvr to open a split. This will open the file as a buffer in the current neovim instance.

4 Likes

Thanks! I added

if [ -n "$NVIM_LISTEN_ADDRESS" ]; then
    export VISUAL="nvr -cc split --remote-wait +'set bufhidden=wipe'"
else
    echo "NVIM_LISTEN_ADDRESS not set"
    export VISUAL="lvim"
fi

to my .zshrc. However when I do :terminal in nvim I get:

NVIM_LISTEN_ADDRESS not set

It appears NVIM_LISTEN_ADDRESS has been deprecated: introduce $NVIM, unset $NVIM_LISTEN_ADDRESS by justinmk · Pull Request #11009 · neovim/neovim · GitHub

You’ll need to use NVIM instead. If I change my ~/.bashrc to this:

if [ -n "$NVIM" ]; then
    export VISUAL="nvr -cc split --remote-wait +'set bufhidden=wipe'"
else
    export VISUAL="vim"
fi

I’m able to use @edit to open in a horizontal split:

2 Likes

Awesome thanks a lot! This does already what I asked for. It works fine with vanilla neovim. Now I actually use lunar vim, And there it does not quite work. It does not create a spit instead it does open the correct file in the current window, but the window freezes.

That is strange. I can’t imagine why LunarVim would freeze. If it happens with vanilla LunarVim, you might want to open a issue on their repo. I do see a number of issues that have nvr or neovim-remote when I search on the issue tracker, but just quickly skimming through a few it didn’t seem like there were any problems that weren’t already solved.

If you share your configuration and it is reproducible on my machine, I might be able to help figure out what is going on.

Alternatively, you can try AstroNvim or LazyVim and see if the same problem exists there. I’m also happy to share my nvim configuration if you think that’ll be useful.

1 Like

Thanks a lot, that is a really kind offer! I will try what happens with vanilla lunar vim and post an issue or ask in their discord.

Seems to be an issue with my specific configuration, on vanilla lunar vim it works.

1 Like