Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-format not working under gVim

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.

like image 789
Natjo Avatar asked Sep 14 '16 12:09

Natjo


People also ask

How do you invoke a clang-format?

You can install clang-format and git-clang-format via npm install -g clang-format . To automatically format a file according to Electron C++ code style, run clang-format -i path/to/electron/file.cc . It should work on macOS/Linux/Windows.

How do I change the file format for clang?

Configuring Style with clang-format clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the . clang-format or _clang-format file in the project directory.

Where is clang-format stored?

clang-format is located in clang/tools/clang-format and can be used to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.

Can clang-format break code?

Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code.


3 Answers

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

like image 125
suncho Avatar answered Nov 16 '22 00:11

suncho


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.

like image 39
Danh Avatar answered Nov 16 '22 00:11

Danh


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.

like image 31
debo Avatar answered Nov 16 '22 00:11

debo