Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Any way to get Vim to format my comments to comply with Google cpplint?

enter image description here

Top section is not compliant, to comply 5 changes are required.

  1. keep this comment at the same level
  2. Add a space after //
  3. add minimum of two spaces between code and comment
  4. align comments
  5. add space after //

(Note I am also asking this question here, it may be possible to do with Tabular https://github.com/godlygeek/tabular/issues/44)

EDIT: As per recommendation below, this works:

Plugin => https://github.com/rhysd/vim-clang-format

.nvimrc or .vimrc Configuration:

let g:clang_format#code_style='google'

let g:clang_format#style_options = {
            \ "AccessModifierOffset" : -3,
            \ "Standard" : "C++03"}
share|improve this question
3  
clang-format has a googlestyle setting. I believe there is a plugin that you can use to run it from vim. – FDinoff Feb 11 at 20:38
    
@FDinoff, awesome it works, if you answer, I will select it as solution... – user1135541 Feb 11 at 22:19
    
I don't understand (1)--they both have four spaces before the first /, right? – Kyle Strand Feb 12 at 4:52
    
@KyleStrand: I believe (1) has to do with not indenting that line to match the other two comments (i.e. OP is specifying an edge case that should not change). – Kevin Feb 12 at 5:02
    
@Kevin Ah, I thought it was saying it should be at the same indent-level as something on the previous or next line, but that makes sense. Thanks. – Kyle Strand Feb 12 at 5:05
up vote 11 down vote accepted

clang-format is a command line tool that formats c++ code. One of the default formatters is google.

clang-format -style=google

There are plugins that would allow you to run clang-format in vim. One such plugin is https://github.com/rhysd/vim-clang-format (I have not used it, it was the first I found)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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