10

I installed clang-format-3.8 via apt-get. Now I try to use it in gVim, but it is not working. I checked and clang-format-3.8 exists in the folder /usr/share/vim/addons/syntax.

But when I enter :pyf /usr/share/vim/addons/syntax/clang-format-3.8.py in my vim command line, it returns:

E319: Sorry, the command is not available in this version.

I use gVim 7.4 under Ubuntu 16.04.

2
  • Vim in Ubuntu is compiled with Python 3 instead of Python 2. You need to compile your own vim with Python 2 to get that plug in work
    – Danh
    Commented Sep 14, 2016 at 12:50
  • 1
    s/:pyf/:py3f/ works for me
    – malat
    Commented Sep 3, 2020 at 6:17

4 Answers 4

18

Dahn's answer is correct that the Vim binary that ships with Ubuntu 16.04 is compiled with Python 3 rather than Python 2. The clang-format-3.8.py script in the Ubuntu 16.04 clang-format-3.8 package is not compatible with Python 3.

But the latest clang-format.py does work with Python 3. You can get it here:

https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/clang-format.py

I think it was just a matter of putting parentheses around the print statements.

Save this file somewhere on your computer such as /usr/local/share/vim/addons/syntax/.

This script uses clang-format as the binary name, so you'll want to install the clang-format package, which installs the clang-format command as a symlink to clang-format-3.8.

Since Vim is now loading a Python 3 script, replace your :pyf (not available) command with :py3f:

:py3f /usr/local/share/vim/addons/syntax/clang-format.py

4
  • Thank you for your answer. I installed clang-format via apt-get. It created a clang-format.py file in my chosen path: /usr/share/vim/addons/syntax. I overwrote it with the one from the link and tried it on gvim with :pyf3 /usr/share/vim/addons/syntax/clang-format.py. It did not work and gave me the message Not an editor command .... What did I do wrong?
    – Natjo
    Commented Sep 30, 2016 at 9:28
  • My mistake. It should be :py3f, not :pyf3. Editing my answer to reflect this.
    – suncho
    Commented Sep 30, 2016 at 16:09
  • Also, be careful about deciding to save/install anything under /usr outside of /usr/local: linuxfromscratch.org/blfs/view/svn/introduction/position.html If you save it in /usr outside of /usr/local, your freshly-downloaded clang-format.py could be overwritten by the distribution when you upgrade.
    – suncho
    Commented Sep 30, 2016 at 16:20
  • clang-format-4.0 seems to be shipping with a python 3 version of clang-format.py, so my vim config for binding control+k to format the line was as simple as map <C-K> :py3f /usr/share/clang/clang-format-4.0/clang-format.py<cr> Commented Aug 16, 2018 at 14:58
5

The Vim binary shipped with Ubuntu 16.04 is compiled with Python 3. The vim addons of clang-format is written by Python 2.

You need to either:

  • write your own addon by Python 3
  • Compile your own vim with Python 2, which is the easiest way

The instructions to build vim with Python can be found by Google.

2
  • I expect I'm not the only one with this problem, as I don't want to compile with py2 (I'm pretty sure this will bring up new conflicts with other plugins), are there any addons for Python 3? I can't find any that work
    – Natjo
    Commented Sep 15, 2016 at 9:18
  • Check your vimrc first. In my case, there're no conflict when switch to Python 2. Unfortunately, afaik, there is no available add on yet. Make it yourself
    – Danh
    Commented Sep 15, 2016 at 9:33
4

In addition to the above answers I had to do a few more things. I downloaded a new python file and changed the key mapping recommended by in the clang python file to the following in my .vimrc:

    map <C-I> :py3file <path-to-this-file>/clang-format.py<cr>
    imap <C-I> <c-o>:py3file <path-to-this-file>/clang-format.py<cr>

This solved the E319 problem I was getting.

2

I solved the problem differently using a combination of bash and vim commands.

First, I installed clang-format package

# apt-get install clang-format-3.5

(I choose version 3.5 but you can choose a different one)

Second, Test if clang-format is working

$ clang-format-3.5 -style=Google  test.cpp

Then, run vim

$ vim test.cpp

vim allows to run an external command and print it's output into current buffer

:r ! clang-format-3.5 -style=Google  %

(more details about external commands in vim https://www.linux.com/learn/vim-tips-working-external-commands)

This will append the output of clang-format into current buffer. To replace current buffer, which is the desirable effect, specify the lines to output into

:%! clang-format-3.5 -style=Google  %

(the first % means all line in current file)

You can make this process more productive by defining new commands in vim (using command) for both visual and command-line modes.

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