this post was submitted on 16 Jul 2023
10 points (100.0% liked)

Neovim

496 readers
1 users here now

Neovim is a modal text editor forked off of Vim in 2014. Being modal means that you do not simply type text on screen, but the behavior and functionality of the editor changes entirely depending on the mode.

The most common and most used mode, the "normal mode" for Neovim is to essentially turn your keyboard in to hotkeys with which you can navigate and manipulate text. Several modes exist, but two other most common ones are "insert mode" where you type in text directly as if it was a traditional text editor, and "visual mode" where you select text.

Neovim seeks to enable further community participation in its development and to make drastic changes without turning it in to something that is "not Vim". Neovim also seeks to enable embedding the editor within GUI applications.

The Neovim logo by Jason Long is licensed under the Creative Commons Attribution 3.0 Unported License.

founded 3 years ago
MODERATORS
 

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.

top 5 comments
sorted by: hot top controversial new old
[–] catlover 7 points 11 months ago
[–] [email protected] 4 points 11 months ago* (last edited 11 months ago) (1 children)

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 (), enter command mode (:), then send the command to edit the vile. Ex: :e ~/cheatsheat.txt. ( 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 or nvim --listen ~/.nvim-server.pipe for easier usage with one file. This way the command will always be nvim --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 ":e [file-path]"The full command that worked for me:nvim --server /run/user/1000/nvim.78931.0 --remote-send ":e ~/msg.md"``

You could simplify this further with a script taking one parameter, concatenating "nvim --server ~/cache/nvim/server.pipe --remote-send ":e" the file name and "`" 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 now opnvim [filename]

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 method

Relevent 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.

[–] rjooske 3 points 11 months ago

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

[–] [email protected] 3 points 11 months ago (1 children)

:tabnew would open a new tab inside the existing instance, you can also use vsplit and split for a split inside the current tab.

[–] rjooske 4 points 11 months ago

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

load more comments
view more: next ›