Vim is a powerful text editor, which can be scaled up to a fully-fledged IDE, especially for Python. Though, if you are reading this blog post, I guess that you already know what Vim is and I do not have to tell you about it. What I want to share though, is how to build Vim with Python 3 on macOS, when using Anaconda Python 3. If you want to use some nice plugins of Vim for Python (e.g. Pymode), then your Vim should have only Python 3 enabled. That is, you should have: 

-python
+python3

There are some solutions out there with MacVim, proposing different tricks to enable Python 3 of MacVim when Vim starts. I have tried many of those, but none of them worked for me. So, if you agree with me up to here, then you also have tried to build Vim on your macOS. And, I guess, when you are trying to check if your Vim has properly functioning support of Python 3, by (e.g.) doing :py3 pass, then you get the super informative error: 

Vim: Caught deadly signal ABRT
Vim: Finished

Abort trap: 6

I struggled with this almost a week, and even at the GitHub of Vim cannot provide the solution. But, I finally found the solution and I want to share it, so other people will be helped.

I will not go into the details of the flags that one can use when configuring the building process of Vim. You can check what flags are there by using the --help argument of the configure script. I will use some simple flags and describe the process, so you can have a fully Python 3 built Vin on your macOS.

Finally, there are two ways to build Vim. One is with static linking of Python files with Vim, and the other with dynamic linking of Python files and Vim. The first one )static linking) has the drawback of needing some extra process when (and if) you will change your Python set-up. I haven't thought of a reason to choose static linking, still I include it here for reference. I would suggest you to check the dynamic linking way. 

So, let’s go!

  1. Download the latest stable version of Vim. You can find that by either checking the GitHub repo of Vim or going to the download page of Vim. At the moment of this blog post, the latest stable version of Vim is 8.2. Download and expand the code of the latest stable version of Vim, and then open your terminal and navigate inside of the directory of the code of Vim. I will refer to this directory as vimroot for the rest of this text. 
  2. On your terminal and in the vimroot directory. Issue the command:
    $ export CPPFLAGS=-I/Users/< your home directory >/opt/anacoda3/include/python3.7m
  3. Now you have to configure the building process, and you have to coose if you will have a static or dynamic linking of Vim and Python. If you choose to have a static linking, then you should change the --enable-python3interp=dynamic to --enable-python3interp=yes. For dynamic linking, use the command 
    $ ./configure --enable-python3interp=dynamic \
    --prefix=< vim installation path > \
    --enable-fail-if-missing \
    --with-vim-name=< optional, name/name prefix for vim binaries >

    As prefix, you can use the /usr/local.

    As name for Vim, you can use the version, for example at the moment of writing this, I use the vim.8.2.0.

    Then, hit enter and wait a bit.

  4. If you have chosen the dynamic linking, then you can skip to step 5. If you have chosen the static linking, then you have to make the shared libraries for Python available to Vim. Vim tries to find those at /usr/local/lib. To make them available, you have to issue the command:
    $ sudo ln -s ~/opt/anaconda3/lib/libpython3.7m.a /usr/local/lib/
    and
    $ sudo ln -s ~/opt/anaconda3/lib/libpython3.7m.dylib /usr/local/lib/
  5. Then, you can issue
    $ make
    followed by
    $ sudo make install
  6. Finally, if you have chosen the dynamic linking, you have to put the following at the top your .vimrc
    set pythonthreedll=/Users/< your user name >/opt/anaconda3/lib/libpython3.7m.dylib
    set pythonthreehome=/Users/< your user name >/opt/anaconda3/
    If you use a specific environment from anaconda, then you should use the same file libpython3.7m.dylib but from the environment that you use. For example, let's say that your conda env is called myenv, then you should use the following at your .vimrc
    set pythonthreedll=/Users/< your user name >/opt/anaconda3/envs/myenv/lib/libpython3.7m.dylib
    set pythonthreehome=/Users/< your user name >/opt/anaconda3/envs/myenv/
    Then, source the new .vimrc by doing at your Vim
    :source %
    And it is done! 

You can validate that your building is correct by opening your newly built vim with

$ vim --clean

and then issuing in Vim

:py3 pass

or

:py3 print(‘OK’)

or

:py3 import subprocess

Hopefully, all will be OK! 

Enjoy your Vim with anaconda Python 3, on macOS!