Prepare vim environment to work with clang-format

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.


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>