9

I cannot convince vim to apply the coding-style I'd like to (cindent and friends).

However, integrating clang-format.py (which uses the .clang-format of my project) actually makes me totally happy.

Except that I have the reflex of using the = + motion-command. Which is actually very nice to indent a region or the whole file.

How can I instruct vim to override the default functionality? I read about equalprg, but I'm unsure how to set it to make it work with clang-format.

Patrick B.
  • 559
  • 1
  • 6
  • 16
  • 4
    How about installing Rhysd's vim-clang-format plugin, together with its requirements (especially vim-operator-user), and then map = to <Plug>(operator-clang-format)? Plugin link: https://github.com/rhysd/vim-clang-format – VanLaser Feb 17 '16 at 09:49
  • Thank you @VanLaser Do you also know how to make it work with the motion: == for one line and gg=G for the whole buffer? – Patrick B. Feb 17 '16 at 10:32
  • @VanLaser sorry for my reluctance - I was simply not believing: your comment works as expected. Make it an answer please. – Patrick B. Feb 17 '16 at 11:54

1 Answers1

9

If you have nothing against a plugin install, Rhysd's vim-clang-format may be a valid solution, instead of a custom equalprg.

As the documentation states, with Kana's vim-operator-user dependency installed, you can map = to <Plug>(operator-clang-format) (to function as a full operator) and use it with motions / text-objects such as == or =G.

You should create a buffer local mapping, only for C,C++ and Objective-C filetypes, to let default = otherwise untouched:

autocmd FileType c,cpp,objc map <buffer> = <Plug>(operator-clang-format)
VanLaser
  • 9,700
  • 2
  • 24
  • 34