Is there a way to avoid opening a nested neovim instance inside a terminal buffer and instead open it as a new buffer inside the already running instance? I’m thinking something like what Fugitive does, but more general and that works for any shell commands.
this may be relevant: https://github.com/willothy/flatten.nvim
Oh shit, the following is not what you wanted it seems. This is for opening a normal file, not integrating git with neovim in that fashion. Apologies, but no. This would be possible to create, and might exist for git, but that would have to be created per command. One would have to make a new command that opened a file up, let you write out your commit, and then took from that temp file piping your message to the commit like fugitive did. Apologies, I’ll leave this up though incase anyone else finds it useful
Failed answer
Yup, all is possible with a little bit of know how and skill. It’s a bit inconvienient though. Simple solution is at the end, but heres a full explanation:
full explination
First, you have to know what server you’re attached to. This can be done with
:echo v:servername
or listing/run/user/1000/nvim*
if you only have one open. The files named nvim.some-numbers will be your running servers.Then, you simply run the command
nvim --server /run/user/1000/nvim.[server].0 --remote-send "<c-\><c-n><ESC>:e [file-path]<cr>"
This will exit terminal mode (cntrl+\cntrl+n), exit any other mode (<esc>), enter command mode (:
), then send the command to edit the vile. Ex::e ~/cheatsheat.txt
. (<cr> is the enter key. Think of this as remotely typing in each character, with anything in angle brackets being a special character)This can be made easier though if you launch nvim with the command
nvim --listen ~/.cache/nvim/server.pipe
ornvim --listen ~/.nvim-server.pipe
for easier usage with one file. This way the command will always benvim --server ~/cache/nvim/server.pipe --remote-send "<c-\><c-n><ESC>:e [file-path]<cr>"
and an alias can be created like this:alias svnvim="nvim --server ~/cache/nvim/server.pipe"
.If you then add the remapping
tnoremap <ESC> <c-\><c-n>
the command in terminal mode shrinks to ````svnvim --remote-send "<ESC>:e [file-path]<cr>"The full command that worked for me:
nvim --server /run/user/1000/nvim.78931.0 --remote-send “<ESC>:e ~/msg.md<cr>”``You could simplify this further with a script taking one parameter, concatenating "nvim --server ~/cache/nvim/server.pipe --remote-send “<esc>:e” the file name and “<cr>`” but I’m too lazy to do this right now.
Bash Script
Nevermind, that was incredibly easy. When opening nvim with the
--listen ~/server.pipe
flag, this will work:#!/bin/sh eval 'nvim --server ~/server.pipe --remote-send "<c-\><c-n><esc>:e ${1}<cr>"'
Put that in a file, perhaps called
opnvim
, place that into a folder in your path, and there you go. That command is nowopnvim
huh, that’s neat. It’s a one liner, and if I get a bit less lazy I’ll add in the ability to automatically find all nvim servers and send the command.
Simpler answer
To simplify, with the following aliases in your
.bashrc
:alias nvim="nvim --listen ~/.cache/nvim/server.pipe alias svnvim="nvim --server ~/cache/nvim/server.pipe"
and then the following remapping in your init.vim:
tnoremap <ESC> <c-\><c-n>
you’ll only deed to run
svnvim --remote-send "<esc>:e filename<cr>
in the terminal to open it in nvim as a new buffer. Or, look at the “Bash Script” section for a more concise methodRelevent Docs
https://neovim.io/doc/user/remote.html
https://neovim.io/doc/user/starting.html#--listen
Post Notes
It’s likely possible to fix this up and make it more automatic. Tmux has a tool called tmux-open-nvim which is pretty nice. you only need to run
ton ~/msg.md
in order for it to work so I’d look at this small script if you wanted to try your hand at making this esier. That will work if you’re willing to move away from nvim as your terminal however.Thanks for the detailed answer! (or your attempt at answering 😅) I never knew neovim worked like that and that I could control it from outside. Definitely keeping this in my toolbox
:tabnew would open a new tab inside the existing instance, you can also use vsplit and split for a split inside the current tab.
That’s not exactly what I want. If you run
git commit
in a terminal buffer for example, git opens a new instance of neovim inside the terminal buffer (assuming you’ve configured git to do so), which is what I’m trying to avoid. I want git here to open a new buffer inside the currently running neovim instance as a place to edit the commit message. Sure, I can use Fugitive instead, but that doesn’t work for commands other than git