Skip to content

Instantly share code, notes, and snippets.

@brandonwillard
Forked from bfredl/nvim-terminal-edit.py
Last active October 6, 2017 21:35
Show Gist options
  • Save brandonwillard/216e8a6b3e873f795d9035b5ca63a6ba to your computer and use it in GitHub Desktop.
Save brandonwillard/216e8a6b3e873f795d9035b5ca63a6ba to your computer and use it in GitHub Desktop.
Edit file in host Neovim instance from a :terminal buffer
#!/home/bwillard/apps/anaconda3/envs/neovim3/bin/python
"""Edit a file in the host nvim instance."""
from __future__ import print_function
import os
import sys
from neovim import attach
args = sys.argv[1:]
addr = os.environ.get("NVIM_LISTEN_ADDRESS", None)
if not addr:
os.execvp('nvim', ['nvim'] + args)
# If no arguments are given, it will simply exit terminal mode.
#if not args:
# print("Usage: {} <filename> ...".format(sys.argv[0]))
# sys.exit(1)
nvim = attach("socket", path=addr)
def _setup():
nvim.input('<c-\\><c-n>')
chid = nvim.channel_id
nvim.command('augroup EDIT')
nvim.command('au BufEnter <buffer> call rpcnotify({}, "n")'.format(chid))
nvim.command('au BufEnter <buffer> startinsert')
nvim.command('augroup END')
for arg in args:
if os.path.isfile(arg):
file_name = os.path.realpath(arg)
nvim.command('exe "drop {}"'.format(file_name))
def _exit(*args):
nvim.command('augroup EDIT')
nvim.command('au!')
nvim.command('augroup END')
nvim.stop_loop()
nvim.run_loop(_exit, _exit, _setup)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment