All Projects → rhysd → Vim Clang Format

rhysd / Vim Clang Format

Vim plugin for clang-format, a formatter for C, C++, Obj-C, Java, JavaScript, TypeScript and ProtoBuf.

Projects that are alternatives of or similar to Vim Clang Format

Vim Gofmt
Formats Go source code asynchronously with multiple Go formatters.
Stars: ✭ 11 (-98.69%)
Mutual labels:  vim-plugin, formatter
vim-ormolu
Plugin for formatting Haskell source code
Stars: ✭ 35 (-95.82%)
Mutual labels:  formatter, vim-plugin
clang-format-editor
Clang-Format Editor is a tool that helps you find the best Clang-Format Style for your C++, C#, Java, JavaScript, and Objective-C code.
Stars: ✭ 15 (-98.21%)
Mutual labels:  formatter, clang
Vimcompletesme
You don't Complete Me; Vim Completes Me! A super simple, super minimal, super light-weight tab completion plugin for Vim.
Stars: ✭ 752 (-10.16%)
Mutual labels:  vim-plugin
Sql Formatter
A whitespace formatter for different query languages
Stars: ✭ 779 (-6.93%)
Mutual labels:  formatter
Pfr
std::tuple like methods for user defined types without any macro or boilerplate code
Stars: ✭ 896 (+7.05%)
Mutual labels:  clang
Vim Healthcheck
Polyfill of Neovim's health-check for Vim
Stars: ✭ 26 (-96.89%)
Mutual labels:  vim-plugin
Vim Move
Plugin to move lines and selections up and down
Stars: ✭ 743 (-11.23%)
Mutual labels:  vim-plugin
Tmux Navigate
🥂 Seamless tmux/vim navigation (over SSH too!)
Stars: ✭ 23 (-97.25%)
Mutual labels:  vim-plugin
Translatedjs
Internationalization and localization for JavaScript and Node.js
Stars: ✭ 17 (-97.97%)
Mutual labels:  formatter
Vim Quickrepl
The Simplest Faster way to open your REPL for filetypes.
Stars: ✭ 17 (-97.97%)
Mutual labels:  vim-plugin
Vim Grammarous
A powerful grammar checker for Vim using LanguageTool.
Stars: ✭ 780 (-6.81%)
Mutual labels:  vim-plugin
Moment.php
Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
Stars: ✭ 900 (+7.53%)
Mutual labels:  formatter
Vimwiki
Personal Wiki for Vim
Stars: ✭ 6,970 (+732.74%)
Mutual labels:  vim-plugin
Git Gutter Vim
git gutter for vim
Stars: ✭ 23 (-97.25%)
Mutual labels:  vim-plugin
Prettier Atom
An atom package for the prettier formatter.
Stars: ✭ 750 (-10.39%)
Mutual labels:  formatter
Git Messenger.vim
Vim and Neovim plugin to reveal the commit messages under the cursor
Stars: ✭ 904 (+8%)
Mutual labels:  vim-plugin
Go Money
Go implementation of Fowler's Money pattern
Stars: ✭ 887 (+5.97%)
Mutual labels:  formatter
Online Markdown
A online markdown converter specially for Wechat Public formatting.
Stars: ✭ 812 (-2.99%)
Mutual labels:  formatter
Vim Diff Toggle
🛠 Vim plugin to speed up editing diff files
Stars: ✭ 17 (-97.97%)
Mutual labels:  vim-plugin

Format your C family code

Build Status

This plugin formats your code with specific coding style using clang-format.

Currently below languages are supported:

  • C
  • C++
  • Objective-C
  • JavaScript
  • Java
  • TypeScript
  • Protobuf

Screenshot

Screenshot

Requirements

  • clang-format command (3.4 or later), which is bundled in Clang extra tools
  • vim-operator-user(highly recommended)
  • vimproc.vim(recommended in Windows)

Installation

Copy plugin, doc and autoload directories into your ~/.vim or use :packadd in Vim8. Or please use your favorite plugin manager to install this plugin. I recommend latter.

Usage

:ClangFormat command is available. If you use it in normal mode, the whole code will be formatted. If you use it in visual mode, the selected code will be formatted. It is more convenient to map :ClangFormat to your favorite key mapping in normal mode and visual mode.

If you install vim-operator-user in advance, you can also map <Plug>(operator-clang-format) to your favorite key bind.

:ClangFormatAutoToggle command toggles the auto formatting on buffer write. :ClangFormatAutoEnable command enables the auto formatting on buffer write. Useful for automatically enabling the auto format through a vimrc. :ClangFormatAutoDisable turns it off.

What is the difference from clang-format.py?

clang-format.py is Python script to use clang-format from Vim, which is installed with clang-format. The usage is here. Against clang-format.py, vim-clang-format has below advantages.

  • Style options are highly customizable in .vimrc. clang-format.py requires .clang-format file to customize a style.
  • vim-clang-format provides an operator mapping.
  • vim-clang-format doesn't need python interface.

In short, vim-clang-format has better Vim integration than clang-format.py.

Customization

You can customize formatting using some variables.

  • g:clang_format#code_style

g:clang_format#code_style is a base style. llvm, google, chromium, mozilla is supported. The default value is google.

  • g:clang_format#style_options

Coding style options as dictionary.

An example is below:

let g:clang_format#style_options = {
            \ "AccessModifierOffset" : -4,
            \ "AllowShortIfStatementsOnASingleLine" : "true",
            \ "AlwaysBreakTemplateDeclarations" : "true",
            \ "Standard" : "C++11",
            \ "BreakBeforeBraces" : "Stroustrup"}

For config information, execute clang-format -dump-config command.

  • g:clang_format#command

Name of clang-format. If the name of command is not clang-format or you want to specify a command by absolute path, set this variable. Default value is clang-format.

  • g:clang_format#extra_args

You can specify more extra options in g:clang_format#extra_args as String or List of String.

  • g:clang_format#detect_style_file

When this variable's value is 1, vim-clang-format automatically detects the style file like .clang-format or _clang-format and applies the style to formatting.

  • g:clang_format#auto_format

When the value is 1, a current buffer is automatically formatted on saving the buffer. Formatting is executed on BufWritePre event.

  • g:clang_format#auto_format_on_insert_leave

When the value is 1, inserted lines are automatically formatted on leaving insert mode. Formatting is executed on InsertLeave event.

  • g:clang_format#auto_formatexpr

When the value is 1, formatexpr option is set by vim-clang-format automatically in C, C++ and ObjC codes. Vim's format mappings (e.g. gq) get to use clang-format to format. This option is not comptabile with Vim's textwidth feature. You must set textwidth to 0 when the formatexpr is set.

  • g:clang_format#enable_fallback_style

When the value is 0, -fallback-style=none option is added on executing clang-format command. It means that vim-clang-format does nothing when .clang-format is not found. The default value is 1.

Vimrc Example

let g:clang_format#style_options = {
            \ "AccessModifierOffset" : -4,
            \ "AllowShortIfStatementsOnASingleLine" : "true",
            \ "AlwaysBreakTemplateDeclarations" : "true",
            \ "Standard" : "C++11"}

" map to <Leader>cf in C++ code
autocmd FileType c,cpp,objc nnoremap <buffer><Leader>cf :<C-u>ClangFormat<CR>
autocmd FileType c,cpp,objc vnoremap <buffer><Leader>cf :ClangFormat<CR>
" if you install vim-operator-user
autocmd FileType c,cpp,objc map <buffer><Leader>x <Plug>(operator-clang-format)
" Toggle auto formatting:
nmap <Leader>C :ClangFormatAutoToggle<CR>

Auto-enabling auto-formatting

autocmd FileType c ClangFormatAutoEnable

For More Information

$ clang-format -help
$ clang-format -dump-config

clang-format's documentation and API documentation is useful in some cases. In particular, the following link is useful to know the information of a key and its value of a style setting. CLANG-FORMAT STYLE OPTIONS

License

The MIT License (MIT)

Copyright (c) 2013 rhysd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].