Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepare vim environment to work with clang-format

Problem:

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

Problem
When I run vim environment and try reformat code (CTRL+K) i get this warning on the screen

Environment:
System: Mac OS X

Settings:
Vim:

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Nov 17 2016 20:17:27)
MacOS X (unix) version
Included patches: 1-91

python:

python --version: Python 3.5.2

What I do

  • Update vim: 7.3 -> 8.0
  • Update python: ~2.7 -> ~3.5
  • I updated the .vimrc file
    from
    map <C-K> :pyf .../clang-format.py<cr> imap <C-K> <c-o>:pyf .../clang-format.py<cr>
    to
    map <C-K> ggVG :py3f .../clang-format.py<cr> imap <C-K> <c-o>:py3f .../clang-format.py<cr>

  • Also I update clang-format file
    https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/clang-format.py

Thx,

like image 949
meernet Avatar asked Nov 18 '16 22:11

meernet


People also ask

How do I apply a clang file 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.

Does Clangd use clang format?

clangd embeds clang-format, which can reformat your code: fixing indentation, breaking lines, and reflowing comments. clangd respects your project's . clang-format file which controls styling options. Format-as-you-type is experimental and doesn't work well yet.

Where is clang format config file?

Configuring Style with clang-format clang-format file located in the closest parent directory of the input file.

Does Google use clang format?

Clang-Format Style Options are flags that are supported by the ClangFormat tool, which became the de-facto standard to format C++ code. Clang offers the option to use one of the predefined styles (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft) or to create a custom configuration by using the given flags.


2 Answers

Solution:

1. Go to .vimrc file and add commands

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

2.Add this https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/clang-format.py source code to clang-format.py file, you can save this file to i.e

<path-to-this-file>/clang-format.py

where path-to-this-file is path to direction where you put this file.

3.Save clang-format.py file and run vim. Check that the command works.

You can also install macvim for OSX. Instruction you can find here. How can I install MacVim on OS X?

PS: If you get "Sorry, the command is not available in this version" error, try:

map <C-K> :py3f <path-to-this-file>/clang-format.py<cr>
imap <C-K><c-o>:py3f <path-to-this-file>/clang-format.py<cr>
like image 64
meernet Avatar answered Oct 07 '22 01:10

meernet


Here

$ which clang-format

returns

/usr/local/bin/clang-format

Then

ls -ls /usr/local/bin/clang-format

points to

/usr/local/Cellar/clang-format/2018-04-24/share/clang/clang-format.py

So I do

$ vim ~/.vimrc

map <C-K> :pyf /usr/local/Cellar/clang-format/2018-04-24/share/clang/clang-format.py<cr>
imap <C-K> <c-o>:pyf /usr/local/Cellar/clang-format/2018-04-24/share/clang/clang-format.py<cr>

This works on vim (from terminal) but not on macvim.

like image 2
KcFnMi Avatar answered Oct 07 '22 01:10

KcFnMi