Convert Figma logo to code with AI

rhysd logovim-clang-format

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

1,096
125
1,096
43

Top Related Projects

1,826

clangd language server

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.

1,832

A client/server indexer for c/c++/objc[++] with integration for Emacs based on clang.

2,350

C/C++ language server supporting multi-million line code base, powered by libclang. Emacs, Vim, VSCode, and others with language server protocol support. Cross references, completion, diagnostics, semantic highlighting and more

3,952

C/C++/ObjC language server supporting cross references, hierarchies, completion and semantic highlighting

6,825

A maintained ctags implementation

Quick Overview

vim-clang-format is a Vim plugin that integrates the clang-format tool into Vim, allowing users to automatically format their C, C++, Objective-C, Java, JavaScript, TypeScript, and Protocol Buffer code according to predefined or custom style guidelines. It provides seamless integration with Vim's normal, visual, and insert modes, making it easy to format code on-the-fly or in bulk.

Pros

  • Easy integration with Vim and Neovim
  • Supports multiple programming languages
  • Customizable formatting options
  • Can be used with various version control systems

Cons

  • Requires clang-format to be installed separately
  • May conflict with other formatting plugins
  • Limited to languages supported by clang-format
  • Can be slow on large files or when formatting entire projects

Getting Started

  1. Install the plugin using your preferred Vim plugin manager. For example, with vim-plug:
Plug 'rhysd/vim-clang-format'
  1. Ensure clang-format is installed on your system.

  2. Add the following to your .vimrc or init.vim:

let g:clang_format#auto_format = 1
let g:clang_format#code_style = "llvm"
  1. Use the following commands in Vim:
    • :ClangFormat to format the current buffer
    • <Leader>cf to format the current selection in visual mode
    • :ClangFormatAutoToggle to toggle automatic formatting on save

Competitor Comparisons

1,826

clangd language server

Pros of clangd

  • Provides comprehensive language server functionality for C/C++, including code completion, diagnostics, and navigation
  • Offers broader IDE integration beyond just Vim, supporting multiple editors and IDEs
  • Continuously updated and maintained by the LLVM community

Cons of clangd

  • Requires more setup and configuration compared to vim-clang-format
  • May have higher resource usage due to its full language server capabilities
  • Not specifically tailored for Vim integration like vim-clang-format

Code Comparison

vim-clang-format:

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

clangd:

{
  "clangd.arguments": [
    "--header-insertion=iwyu",
    "--suggest-missing-includes",
    "--background-index"
  ]
}

The vim-clang-format example shows configuration options for formatting style, while the clangd example demonstrates command-line arguments for advanced features. clangd offers more extensive functionality beyond just formatting, but vim-clang-format provides a simpler, Vim-specific solution for code formatting.

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.

Pros of llvm-project

  • Comprehensive suite of tools and libraries for compiler development
  • Actively maintained by a large community of developers
  • Includes the complete LLVM toolchain, offering more functionality beyond just code formatting

Cons of llvm-project

  • Significantly larger and more complex, requiring more resources to set up and use
  • Not specifically designed for integration with Vim, potentially requiring additional configuration
  • May be overkill for users only interested in code formatting functionality

Code comparison

vim-clang-format:

let g:clang_format#command = 'clang-format'
let g:clang_format#style_options = {
            \ "BasedOnStyle" : "LLVM",
            \ "IndentWidth" : 4,
            \ "ColumnLimit" : 100}

llvm-project (using clang-format directly):

clang-format -style="{BasedOnStyle: llvm, IndentWidth: 4, ColumnLimit: 100}" -i file.cpp

Summary

vim-clang-format is a lightweight Vim plugin specifically designed for integrating clang-format into Vim, making it easy to format code within the editor. llvm-project, on the other hand, is a comprehensive suite of compiler and toolchain technologies, which includes clang-format among many other tools. While llvm-project offers more functionality and is more actively maintained, it may be excessive for users solely interested in code formatting within Vim.

1,832

A client/server indexer for c/c++/objc[++] with integration for Emacs based on clang.

Pros of rtags

  • Provides advanced code navigation and refactoring capabilities
  • Offers real-time indexing of C/C++ projects
  • Integrates with multiple editors and IDEs beyond Vim

Cons of rtags

  • More complex setup and configuration process
  • Requires a separate daemon to run in the background
  • May have higher resource usage for large projects

Code comparison

vim-clang-format:

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

rtags:

#include <rtags/RTags.h>

int main(int argc, char **argv)
{
    RTags::initThreadPool();
    // Additional rtags-specific code
}

vim-clang-format focuses on code formatting within Vim, while rtags provides broader functionality for C/C++ development across multiple editors. vim-clang-format is easier to set up and use for simple formatting tasks, whereas rtags offers more advanced features but requires more configuration. The code examples show the different approaches: vim-clang-format uses Vim-specific configuration, while rtags involves C++ code integration for its functionality.

2,350

C/C++ language server supporting multi-million line code base, powered by libclang. Emacs, Vim, VSCode, and others with language server protocol support. Cross references, completion, diagnostics, semantic highlighting and more

Pros of cquery

  • Provides a more comprehensive C/C++ language server with advanced features like code completion, diagnostics, and navigation
  • Offers better performance and scalability for large codebases
  • Supports cross-references and symbol search across the entire project

Cons of cquery

  • Requires more setup and configuration compared to vim-clang-format
  • May have a steeper learning curve for users new to language servers
  • Less focused on code formatting, which is the primary function of vim-clang-format

Code Comparison

vim-clang-format:

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

cquery:

{
  "initializationOptions": {
    "cacheDirectory": "/path/to/cquery/cache",
    "indexWhitelist": ["/path/to/project"],
    "extraClangArguments": ["-std=c++17"]
  }
}

The code snippets show the configuration differences between the two projects. vim-clang-format focuses on formatting options, while cquery's configuration is more about project setup and indexing. cquery offers more advanced features but requires more complex configuration, whereas vim-clang-format is simpler and more focused on code formatting.

3,952

C/C++/ObjC language server supporting cross references, hierarchies, completion and semantic highlighting

Pros of ccls

  • Provides a full-featured C/C++/Objective-C language server, offering more comprehensive language support
  • Offers advanced features like code completion, go-to-definition, and find references across the entire project
  • Supports multiple build systems and compilation databases, making it more versatile for different project setups

Cons of ccls

  • Requires more setup and configuration compared to vim-clang-format's simpler integration
  • May have higher resource usage due to its more extensive functionality
  • Focuses on language server capabilities rather than specifically on code formatting

Code Comparison

vim-clang-format:

let g:clang_format#command = 'clang-format'
let g:clang_format#style_options = {
            \ "BasedOnStyle" : "LLVM",
            \ "IndentWidth" : 4,
            \ "ColumnLimit" : 100}

ccls:

{
  "initializationOptions": {
    "cache": {
      "directory": "/tmp/ccls"
    },
    "clang": {
      "extraArgs": ["-std=c++17"]
    }
  }
}

The code snippets show configuration examples for each tool. vim-clang-format focuses on formatting options, while ccls configuration includes broader language server settings.

6,825

A maintained ctags implementation

Pros of ctags

  • Supports a wide range of programming languages and file formats
  • Generates comprehensive tag files for efficient code navigation
  • Actively maintained with regular updates and improvements

Cons of ctags

  • Requires separate installation and configuration
  • Not specifically designed for code formatting like vim-clang-format

Code comparison

vim-clang-format:

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

ctags:

ctags -R --fields=+l --languages=python --python-kinds=-iv -f ./tags ./

Key differences

  • Purpose: vim-clang-format focuses on code formatting, while ctags is for generating tag files for code navigation
  • Language support: vim-clang-format is specific to C-family languages, ctags supports multiple languages
  • Integration: vim-clang-format is a Vim plugin, ctags is a standalone tool usable with various editors

Use cases

  • vim-clang-format: Ideal for developers working primarily with C-family languages who want automatic code formatting in Vim
  • ctags: Suitable for developers working with multiple languages who need efficient code navigation across large codebases

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Format your C family code

CI

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

Automatic formatting is provided for the following languages by default:

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

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#auto_filetypes

List of file types to which g:clang_format#auto_format, g:clang_format#auto_format_on_insert_leave, and g:clang_format#auto_formatexpr should be applied. The default value is ["c", "cpp", "objc", "java", "javascript", "typescript", "proto", "arduino"].

  • 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-2021 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.