GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - sbdchd/neoformat: :sparkles: A (Neo)vim plugin for formatting code.

BranchesTags
Open more actions menu

Latest commit

 

History

356 Commits
356 Commits

Folders and files

NameName
Last commit message
Last commit date
autoload
autoload
 
 
doc
doc
 
 
plugin
plugin
 
 
test
test
 
 
.editorconfig
.editorconfig
 
 
.gitignore
.gitignore
 
 
.kodiak.toml
.kodiak.toml
 
 
.nvmrc
.nvmrc
 
 
.travis.yml
.travis.yml
 
 
.vintrc.yaml
.vintrc.yaml
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 

Repository files navigation

Neoformat Build Status

A (Neo)vim plugin for formatting code.

Neoformat uses a variety of formatters for many filetypes. Currently, Neoformat will run a formatter using the current buffer data, and on success it will update the current buffer with the formatted text. On a formatter failure, Neoformat will try the next formatter defined for the filetype.

By using getbufline() to read from the current buffer instead of file, Neoformat is able to format your buffer without you having to :w your file first. Also, by using setline(), marks, jumps, etc. are all maintained after formatting.

Neoformat supports both sending buffer data to formatters via stdin, and also writing buffer data to /tmp/ for formatters to read that do not support input via stdin.

Basic Usage

Format the entire buffer, or visual selection of the buffer

:Neoformat

Or specify a certain formatter (must be defined for the current filetype)

:Neoformat jsbeautify

Or format a visual selection of code in a different filetype

Note: you must use a ! and pass the filetype of the selection

:Neoformat! python

You can also pass a formatter to use

:Neoformat! python yapf

Or perhaps run a formatter on save

augroup fmt
  autocmd!
  autocmd BufWritePre * undojoin | Neoformat
augroup END

The undojoin command will put changes made by Neoformat into the same undo-block with the latest preceding change. See Managing Undo History.

Install

The best way to install Neoformat is with your favorite plugin manager for Vim, such as vim-plug:

Plug 'sbdchd/neoformat'

Current Limitation(s)

If a formatter is either not configured to use stdin, or is not able to read from stdin, then buffer data will be written to a file in /tmp/neoformat/, where the formatter will then read from

Config [Optional]

Define custom formatters.

Options:

name description default optional / required
exe the name the formatter executable in the path n/a required
args list of arguments [] optional
replace overwrite the file, instead of updating the buffer 0 optional
stdin send data to the stdin of the formatter 0 optional
stderr capture stderr output from formatter 0 optional
no_append do not append the path of the file to the formatter command, used when the path is in the middle of a command 0 optional
env list of environment variable definitions to be prepended to the formatter command [] optional
valid_exit_codes list of valid exit codes for formatters who do not respect common unix practices [0] optional
try_node_exe attempt to find exe in a node_modules/.bin directory in the current working directory or one of its parents (requires setting g:neoformat_try_node_exe) 0 optional

Example:

let g:neoformat_python_autopep8 = {
            \ 'exe': 'autopep8',
            \ 'args': ['-s 4', '-E'],
            \ 'replace': 1, " replace the file, instead of updating buffer (default: 0)
            \ 'stdin': 1, " send data to stdin of formatter (default: 0)
            \ 'env': ["DEBUG=1"], " prepend environment variables to formatter command
            \ 'valid_exit_codes': [0, 23],
            \ 'no_append': 1,
            \ }

let g:neoformat_enabled_python = ['autopep8']

Configure enabled formatters.

let g:neoformat_enabled_python = ['autopep8', 'yapf', 'docformatter']

Have Neoformat use &formatprg as a formatter

let g:neoformat_try_formatprg = 1

Enable basic formatting when a filetype is not found. Disabled by default.

" Enable alignment
let g:neoformat_basic_format_align = 1

" Enable tab to spaces conversion
let g:neoformat_basic_format_retab = 1

" Enable trimmming of trailing whitespace
let g:neoformat_basic_format_trim = 1

Run all enabled formatters (by default Neoformat stops after the first formatter succeeds)

let g:neoformat_run_all_formatters = 1

Above options can be activated or deactivated per buffer. For example:

    " runs all formatters for current buffer without tab to spaces conversion
    let b:neoformat_run_all_formatters = 1
    let b:neoformat_basic_format_retab = 0

Have Neoformat only msg when there is an error

let g:neoformat_only_msg_on_error = 1

When debugging, you can enable either of following variables for extra logging.

let g:neoformat_verbose = 1 " only affects the verbosity of Neoformat
" Or
let &verbose            = 1 " also increases verbosity of the editor as a whole

Have Neoformat look for a formatter executable in the node_modules/.bin directory in the current working directory or one of its parents (only applies to formatters with try_node_exe set to 1):

let g:neoformat_try_node_exe = 1

Adding a New Formatter

Note: you should replace everything {{ }} accordingly

  1. Create a file in autoload/neoformat/formatters/{{ filetype }}.vim if it does not already exist for your filetype.

  2. Follow the following format

See Config above for options

function! neoformat#formatters#{{ filetype }}#enabled() abort
    return ['{{ formatter name }}', '{{ other formatter name for filetype }}']
endfunction

function! neoformat#formatters#{{ filetype }}#{{ formatter name }}() abort
    return {
        \ 'exe': '{{ formatter name }}',
        \ 'args': ['-s 4', '-q'],
        \ 'stdin': 1
        \ }
endfunction

function! neoformat#formatters#{{ filetype }}#{{ other formatter name }}() abort
  return {'exe': {{ other formatter name }}
endfunction

Managing Undo History

If you use an autocmd to run Neoformat on save, and you have your editor configured to save automatically on CursorHold then you might run into problems reverting changes. Pressing u will undo the last change made by Neoformat instead of the change that you made yourself - and then Neoformat will run again redoing the change that you just reverted. To avoid this problem you can run Neoformat with the Vim undojoin command to put changes made by Neoformat into the same undo-block with the preceding change. For example:

augroup fmt
  autocmd!
  autocmd BufWritePre * undojoin | Neoformat
augroup END

When undojoin is used this way pressing u will "skip over" the Neoformat changes - it will revert both the changes made by Neoformat and the change that caused Neoformat to be invoked.

Supported Filetypes

  • Arduino
    • uncrustify, clang-format, astyle
  • Assembly
    • asmfmt
  • Astro
    • prettier, prettierd
  • Bazel
    • buildifier
  • Beancount
    • bean-format
  • Blade
    • blade-formatter
  • Bib
    • bibtex-tidy
    • bibclean
  • C
    • uncrustify, clang-format, astyle
  • C#
    • uncrustify, astyle, clang-format csharpier
  • C++
    • uncrustify, clang-format, astyle
  • Cabal
    • cabal-fmt
  • Caddyfile
    • caddy fmt
  • CMake
    • cmake_format
  • Crystal
    • crystal tool format (ships with crystal)
  • CSS
    • css-beautify (ships with js-beautify), prettydiff, stylefmt, stylelint, csscomb, prettierd, prettier, topiary
  • CSV
    • prettydiff
  • Cue
    • cue fmt
  • D
    • uncrustify, dfmt
  • D2
    • d2 fmt
  • Dart
    • dartfmt
    • dart format
  • Dhall
    • dhall format
  • dune
    • dune format
  • Ebuild
    • shfmt
      let g:shfmt_opt="-ci"
  • Elixir
    • mix format
  • Elm
    • elm-format
  • Eruby
    • htmlbeautifier
  • Erlang
    • erlfmt
  • Fennel
    • fnlfmt
  • Fish
    • fish_indent
  • Fortran
    • fprettify
  • F#
    • fantomas
  • GDScript
    • gdformat
  • Gleam
    • gleam format
  • Go
    • gofmt, goimports,

      gofumpt, gofumports

  • GLSL
    • clang-format
  • GN
    • gn
  • GraphQL
    • prettierd, prettier
  • Haskell
    • stylishhaskell, hindent, hfmt, brittany, sortimports, floskell ormolu
      let g:ormolu_ghc_opt=["TypeApplications", "RankNTypes"]
    • You must use formatter's name without "-"
      " right
      let g:neoformat_enabled_haskell = ['sortimports', 'stylishhaskell']
      " wrong
      let g:neoformat_enabled_haskell = ['sort-imports', 'stylish-haskell']
  • HCL
    • hclfmt
  • Toml
    • taplo, topiary
  • Puppet
    • puppet-lint
  • PureScript
    • purs-tidy
    • purty
  • HTML
    • html-beautify (ships with js-beautify), prettierd, prettier, prettydiff
  • HTMLDjango
    • djlint
  • Jade
    • pug-beautifier
  • Java
    • uncrustify, astyle, prettierd, prettier
  • JavaScript
    • js-beautify, prettierd, prettier, prettydiff, clang-format, esformatter, prettier-eslint, eslint_d, standard, semistandard, deno fmt, biome format
  • JSON
    • js-beautify, prettydiff, prettierd, prettier, jq, fixjson, deno fmt, topiary, biome format
  • JSONC (JSON with comments)
    • prettierd, prettier, deno fmt, biome format
  • jsonnet
    • jsonnetfmt
  • just
    • just
  • KDL
    • kdlfmt
  • Kotlin
    • ktlint, prettierd, prettier
  • LaTeX
    • latexindent
      let g:latexindent_opt="-m"
  • Less
    • csscomb, prettydiff, prettierd, prettier, stylelint
  • Lua
    • luaformatter
    • lua-fmt
    • lua-format
    • stylua
  • Markdown
    • remark, prettierd, prettier, deno fmt, mdformat
  • Matlab
    • matlab-formatter-vscode
  • Nginx
    • nginxbeautifier
  • Nickel
    • topiary
  • Nim
    • nimpretty (ships with nim)
    • nph
  • Nix
    • nixfmt
    • nixpkgs-fmt
    • alejandra
  • Objective-C
    • uncrustify, clang-format, astyle
  • Objective-C++
    • uncrustify, clang-format, astyle
  • OCaml
    • ocp-indent, ocamlformat, topiary
  • OpenSCAD
    • openscad-format
  • Pandoc Markdown
    • pandoc
  • Pawn
    • uncrustify
  • Perl
    • perltidy
    • perlimports
  • PHP
    • php_beautifier, php-cs-fixer, phpcbf, prettierd, prettier laravel-pint,
  • PowerShell
    • PSScriptAnalyzer, PowerShell-Beautifier
  • Prisma
    • prettier
  • Proto
    • clang-format
  • Pug (formally Jade)
    • pug-beautifier
  • Python
    • yapf, autopep8, black, pydevf, isort, docformatter, pyment, ruff
  • R
    • styler, formatR
  • Reason
    • refmt
    • bsrefmt
  • Rego
    • opa fmt
  • Ruby
    • rufo, ruby-beautify, rubocop, standard prettier
  • Rust
    • rustfmt, topiary
  • Sass
    • sass-convert, stylelint, csscomb
  • Sbt
    • scalafmt
  • Scala
    • scalariform, scalafmt
  • SCSS
    • sass-convert, stylelint, stylefmt, prettydiff, csscomb, prettierd, prettier
  • Shell
    • shfmt
      let g:shfmt_opt="-ci"
    • topiary
  • Solidity
    • prettierd, prettier, forge fmt
  • SQL
    • sqlfmt, sqlformat (ships with sqlparse), pg_format (ships with pgFormatter), sleek sql-formatter
  • Starlark
    • buildifier
  • SugarSS stylelint
  • Svelte
    • prettierd, prettier-plugin-svelte
  • Swift
    • Swiftformat
  • Terraform
    • terraform,
  • TypeScript
    • tsfmt, prettierd, prettier, prettier-eslint, tslint, eslint_d, clang-format, deno fmt, biome format
  • Typst
    • typstfmt typstyle
  • V
    • v fmt (ships with v)
  • VALA
    • uncrustify
  • Vue
    • prettierd, prettier
  • XHTML
    • tidy, prettydiff
  • XML
    • tidy, prettydiff, prettierd, prettier, xmllint
  • YAML
    • pyaml, prettierd, prettier, yamlfmt (by @mmlb), yamlfmt (by @google), yamlfix
  • zig
    • zigformat zig fmt
  • zsh
    • shfmt
      let g:shfmt_opt="-ci"

About

✨ A (Neo)vim plugin for formatting code.

Topics

formatterneovimpluginvim

Resources

Readme
BSD-2-Clause license

Contributing

Contributing
Activity

Stars

2.0k stars

Watchers

8 watching

Forks

181 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.