tagged: nvim

Nvim: Externally update a buffer keeping scroll position using msgpack and Python

Let's say you want to see the changes in a buffer, changes made externally, without pressing anything in nvim, while keeping your scroll position.
You can use the msgpack api using Python to send a load of text to the buffer.
First here's the python code to attach to your running nvim instance, with the socket name you got from running echo $NVIM_LISTEN_ADDRESS in nvim:

from neovim import attach; 
nvim = attach("socket", path="YOUR ADDRESS HERE");

Now let's get the current line number--using the vim function line--for the top of the screen and where the cursor is:


Now let's delete the entire buffer, ending up in insert mode (\033 means press escape):


Finally let's return to the position we were in:


But we still haven't inserted the text, which we would do before the previous line above.
Let's use the command line to cat a file, and send it to our python code via xargs:

cat YOURFILE | xargs -d '\003' -I {} python3 -c \
'from neovim import attach; nvim = attach("socket", path="YOUR ADDRESS HERE"); \
 current=nvim.funcs.line("."); \
 top=nvim.funcs.line("w0"); \ 
 nvim.input("\033ggVGdi"); \ 
 nvim.input("""{}"""); \

And voila. You obviously need some way to run the above command automatically, but putting it in a thread waiting for a file to change is fairly trivial.


Nvim: Pains while moving from vim

I'll probably use nvim now since we can remotely control it with python or what have you.
However, there are annoyances in moving over (I'm on 0.1.3).
  1. By default, the ruler isn't there. You don't automatically get the current line number.
  2. By default, if you attempt to use your mouse to copy some text, it'll modify nvim's visual mode. I just want to paste some text to the X clibboard buffer (there are probably better way but meh).
  3. By default, there's an annoying white statusline at the bottom of the page.
  4. By default, set list doesn't show the eol character.
You can avoid all this by putting this in yout .config/nvim/init.vim file:

set ruler
set laststatus=0
set mouse=v
set listchars=tab:-\ ,trail:-,nbsp:+,eol:$

In addition, in gnu screen or tmux, the escape key is harder to press. You can put maptimeout 10 in your .screenrc to fix in this gnu screen. It's something to do with nvim using a new library for user input, I believe.
Anyway, we're on 0.1.3, so thing will change.


Nvim: Setup and basic msgpack control via python

The best thing about nvim is the ability to control it remotely.
Apparently, this'll mean applications and embed nvim as an editor and send it commands and receive the resulting text etc.
Let's first install nvim.

add-apt-repository ppa:neovim-ppa/unstable
apt-get update
apt-get install neovim

It uses--for perfectly good reasons I'm sure--XDG the .config directory for its configuration. So move your .vimrc file to ~.config/nvim/init.vim Or don't if you're only messing around: hello.
A useless aside: I really have no idea why we're using `.config` these days. Aren't all the directories prefixed with `.` for configuration? Anyway--as with all of life's great challenges--I guess I'll acclimatise.
Right, so now we've got vim up and running. We need to find the socket we need to connect to it. So run this in normal mode and copy the result. It'll likely be a file in /tmp/.


Now we've got that address let's control neovim from python. Let's install python, pip3 and the neovim python module:

apt-get install python3-pip
pip3 install neovim

Now we've got python3 (python2 works as well I hear, but let's live on the bleeding edge), we can attach to the instance and send a command to nvim. Run python3 and issue this load of commands:

>>> from neovim import attach
>>> nvim = attach('socket', path='WHATEVER THE ABOVE SOCKET NAME WAS')
>>> nvim.command('echo "yalright there eh"')

And--behold--neovim is bidding our every command. REJOICE!


Page 1 of 1