3

I'm thinking of migrating from Vim to NeoVim. I decided to begin with a bit of Python code in my .vimrc that sets the backup directory.

Using the documentation, I wrote the following script:

"------------------------------------------------------------
" Set swap file directory with Python
python << endpython
import neovim, os.path
from os import getcwd, environ
from hashlib import md5
cwd = getcwd()
m = md5()
m.update(cwd)
swd = m.hexdigest()
full_swd = os.path.join(environ['HOME'], '.config', 'nvim', '.swap', swd)
if not os.path.exists(full_swd):
    os.makedirs(full_swd)
nvim = neovim.attach('socket', path=environ['NVIM_LISTEN_ADDRESS'])
nvim.command('set backupdir=' + full_swd)
nvim.command('set directory=' + full_swd)
endpython

"------------------------------------------------------------

The problem I encountered is that this script hangs, even though I'm capable of repeating it via the command line. I tried to insert print statements in various places of the script, and it looks like this is the blocking line:

nvim.command('set backupdir=' + full_swd)

What am I doing wrong? Thank you very much.

2

Don't use the neovim interface for this, its designed to be used asynchronously and you are using it synchronously. (You are deadlocking neovim which is waiting for the nvim socket to respond but it will never respond because it is already in a blocking python call). You should probably use the old vim interface if you need to do stuff synchronously. (AFAIK it still works and isn't being removed)

python << endpython
import vim
import os
from hashlib import md5
cwd = os.getcwd()
m = md5()
m.update(cwd)
swd = m.hexdigest()
full_swd = os.path.join(os.environ['HOME'], '.config', 'nvim', '.swap', swd)
if not os.path.exists(full_swd):
    os.makedirs(full_swd)

# This should be preferred to manually constructing the string.
vim.options['backupdir'] = full_swd 
vim.options['directory'] = full_swd
endpython
  • Personally I don't recommend using python for doing settings. I would probably rewrite it in vimscript. – FDinoff Dec 27 '15 at 5:15
  • Thank you very much! And yes, the old Python interface works, I just thought that using neovim is more preferrable. Could you please give me some directions on how to construct such a string in Vimscript? Thank you very much. – art-solopov Dec 27 '15 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.