I’m Paul Herron, a full-stack developer and technical manager.
I focus mainly on back-end development with tools like Symfony
and devops with tools like Docker

Opening a file in the current Neovim window

by Paul Herron on 18 May 2020

I’ve been using Neovim-Qt in Gnome for a while now and it’s been fantastic. It feels a lot snappier than GVim.

One thing I only just got around to sorting was that opening a file from the file browser would launch a new instance of Neovim, rather than opening the file as a new buffer in the current window. Thanks to this answer on StackExchange it turned out to be quite quick to do.

The steps to set this up are:

pip3 install neovim-remote

Then you can check it’s working by manually opening a file in the existing Neovim window like this:

# Open Neovim-Qt in 'remote' mode
NVIM_LISTEN_ADDRESS=/tmp/nvimsocket nvim-qt

# Open the desired file in the existing window
nvr --remote file.txt

Gnome Files

To get that working via Files (the default file browser in Gnome) you can add an item into the ‘Open With…’ by putting a shortcut in /usr/share/applications.

cd /usr/share/applications
sudo cp nvim-qt.desktop nvim-remote.desktop
sudo vim nvim-remote.desktop

And paste in the following:

[Desktop Entry]
Name=Neovim Remote
Comment=Open file in existing nvim session
MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;
Exec=nvr --remote %F
Icon=nvim-qt
Type=Application
Terminal=false
Categories=Utility;TextEditor;

Then you can amend the existing Nvim-Qt application shortcut, setting an environment variable for it so it’ll always be open in remote ‘listening’ mode:

sudo vim nvim-qt.desktop
[Desktop Entry]
Name=Neovim-Qt
Comment=Qt GUI for Neovim text editor
MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;
Exec=env NVIM_LISTEN_ADDRESS=/tmp/nvimsocket nvim-qt -- %F
Icon=nvim-qt
Type=Application
Terminal=false
Categories=Utility;TextEditor;

This lets you go to the file browser, right-click on a text file and go Properties > Open with and choose that Neovim Remote shortcut.

Open file with Nvim Remote

Back to homepage Back to blog listing

Next: