This repository has been migrated to the self-hosted ari-web Forgejo instance: https://git.ari.lt/ari/vim-codefmt
codefmt is a utility for syntax-aware code formatting. It contains several built-in formatters, and allows new formatters to be registered by other plugins.
- Python
- Haskell
- Bazel
- C
- C++
- Java
- JavaScript
- JSON
- Objective-C
- Protobuf
- C#
- Clojure
- Cmake
- Dart
- FISH
- GN
- Go
- CSS
- SASS
- SCSS
- LESS
- HTML
- Kotlin
- Lua
- Nix
- OCaml
- Markdown
- YAML
- JSX
- MDX
- Vue
- Ruby
- Rust
- Sh
- Bash
- Mksh
- TypeScript
Use :FormatLines
to format a range of lines or use :FormatCode
to format
the entire buffer. Use :NoAutoFormatBuffer
to disable current buffer
formatting.
Before:
int foo(int * x) { return * x** x ; }
After running :FormatCode
:
int foo(int* x) { return *x * *x; }
These are only few examples
call vundle#begin()
" ...
" Add maktaba and codefmt to the runtimepath.
" (The latter must be installed before it can be used.)
Plugin 'google/vim-maktaba'
Plugin 'TruncatedDinosour/vim-codefmt'
" ...
call vundle#end()
call neobundle#begin(expand('...'))
" ...
" Add maktaba and codefmt to the runtimepath.
" (The latter must be installed before it can be used.)
NeoBundle 'google/vim-maktaba'
NeoBundle 'TruncatedDinosour/vim-codefmt'
" ...
call neobundle#end()
" Add maktaba and codefmt to the runtimepath.
" (The latter must be installed before it can be used.)
call plug#begin("...")
" ...
Plug 'google/vim-maktaba'
Plug 'TruncatedDinosour/vim-codefmt'
" ...
call plug#end()
$ cd ~/.vim/bundle
$ git clone https://github.com/google/vim-maktaba
$ git clone https://github.com/TruncatedDinosour/vim-codefmt
Make sure you have updated maktaba recently. Codefmt depends upon maktaba to register formatters.
Want to just sit back and let autoformat happen automatically? Add this to your
vimrc
(or any subset):
autocmd FileType * silent FormatCode
Most formatters have some options available that can be configured with variables but you can also use Glaive
You can get a quick view of all codefmt flags by executing :Glaive codefmt
, or
start typing flag names and use tab completion. See :help Glaive
for usage
details.
Codefmt defines several built-in formatters. The easiest way to see the list of
available formatters is via tab completion: Type :FormatCode <TAB>
in vim.
Formatters that apply to the current filetype will be listed first.
To use a particular formatter, type :FormatCode FORMATTER-NAME
. This will
either format the current buffer using the selected formatter or show an error
message with basic setup instructions for this formatter. Normally you will
trigger formatters via key mappings and/or autocommand hooks. See
vroom/main.vroom to learn more about formatting features, and see
vroom/FORMATTER-NAME.vroom to learn more about usage for individual formatters.
Assume a filetype myft
and a formatter called MyFormatter
.
-
Create a new file in
autoload/codefmt/myformatter.vim
See `autoload/codefmt/buildifier.vim for an example. This is where all the logic for formatting goes. -
Register the formatter in plugin/register.vim with:
call s:registry.AddExtension(codefmt#myformatter#GetFormatter())
-
Create a flag in instant/flags.vim
"" " The path to my formatter executable. call s:plugin.Flag('myformatter_executable', 'myformatter')
-
Update the README.md to mention your new filetype
Thats it! Now make a pull request.