Last active
September 4, 2020 05:32
-
-
Save tarruda/37f7a3e22996addf8921 to your computer and use it in GitHub Desktop.
Edit file in host Neovim instance from a :terminal buffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Edit a file in the host nvim instance.""" | |
import os | |
import sys | |
from neovim import attach | |
args = sys.argv[1:] | |
if not args: | |
print "Usage: {} <filename> ...".format(sys.argv[0]) | |
sys.exit(1) | |
addr = os.environ.get("NVIM_LISTEN_ADDRESS", None) | |
if not addr: | |
os.execvp('nvim', args) | |
nvim = attach("socket", path=addr) | |
def _setup(): | |
nvim.input('<c-\\><c-n>') # exit terminal mode | |
chid = nvim.channel_id | |
nvim.command('augroup EDIT') | |
nvim.command('au BufEnter <buffer> call rpcnotify({0}, "n")'.format(chid)) | |
nvim.command('au BufEnter <buffer> startinsert'.format(chid)) | |
nvim.command('augroup END') | |
nvim.vars['files_to_edit'] = args | |
for x in args: | |
nvim.command('exe "drop ".remove(g:files_to_edit, 0)') | |
def _exit(*args): | |
nvim.vars['files_to_edit'] = None | |
nvim.command('augroup EDIT') | |
nvim.command('au!') | |
nvim.command('augroup END') | |
nvim.session.stop() | |
nvim.session.run(_exit, _exit, _setup) |
Is it possible to alter this script to also function outside of a terminal emulated within nvim? That way, anywhere someone might run nvim-terminal-edit path/to/file
from any terminal, and it'll tell the exiting nvim to open it
This does not appear to work for me. I get
AttributeError: 'CompatibilitySession' object has no attribute 'run'
Is the gist stale, or have I done something wrong?
updated version: https://gist.github.com/bfredl/e5b05193304a3340e29e998662ffff76#file-nvim-terminal-edit-py
This also works on systems where python is python3 (python2 compatibility remains)
nvim.command('au BufEnter <buffer> call rpcnotify({0}, "n")'.format(chid))
What is intended by sending an n
over the rpc-channel to the nvim instance?
Traceback (most recent call last):
File "/usr/bin/edit", line 40, in <module>
nvim.session.run(_exit, _exit, _setup)
AttributeError: 'CompatibilitySession' object has no attribute 'run'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
path of files should concat with
os.getcwd()
, otherwise the path will be incorrect aftercd
.