All Projects → sbdchd → Neoformat

sbdchd / Neoformat

Licence: bsd-2-clause
✨ A (Neo)vim plugin for formatting code.

Projects that are alternatives of or similar to Neoformat

Iris.vim
📫 Simple mail client for Vim.
Stars: ✭ 148 (-84.85%)
Mutual labels:  plugin, neovim
Vim Sneak
The missing motion for Vim 👟
Stars: ✭ 2,467 (+152.51%)
Mutual labels:  plugin, neovim
Vim Svelte
Vim syntax highlighting and indentation for Svelte 3 components.
Stars: ✭ 158 (-83.83%)
Mutual labels:  plugin, neovim
Nvim Lspconfig
Quickstart configurations for the Nvim LSP client
Stars: ✭ 3,410 (+249.03%)
Mutual labels:  plugin, neovim
Comfortable Motion.vim
Brings physics-based smooth scrolling to the Vim world!
Stars: ✭ 543 (-44.42%)
Mutual labels:  plugin, neovim
Vim Laravel
Vim support for Laravel/Lumen projects
Stars: ✭ 128 (-86.9%)
Mutual labels:  plugin, neovim
Aerojump.nvim
Aerojump is a fuzzy-match searcher/jumper for Neovim with the goal of quick keyboard navigation
Stars: ✭ 184 (-81.17%)
Mutual labels:  plugin, neovim
Sniprun
A neovim plugin to run lines/blocs of code (independently of the rest of the file), supporting multiples languages
Stars: ✭ 93 (-90.48%)
Mutual labels:  plugin, neovim
Vimpyter
Edit your Jupyter notebooks in Vim/Neovim
Stars: ✭ 308 (-68.47%)
Mutual labels:  plugin, neovim
Gen tags.vim
Async plugin for vim and neovim to ease the use of ctags/gtags
Stars: ✭ 288 (-70.52%)
Mutual labels:  plugin, neovim
Nvim Toggleterm.lua
A neovim lua plugin to help easily manage multiple terminal windows
Stars: ✭ 102 (-89.56%)
Mutual labels:  plugin, neovim
Markdown Preview.vim
⚠️ PLEASE USE https://github.com/iamcco/markdown-preview.nvim INSTEAD
Stars: ✭ 764 (-21.8%)
Mutual labels:  plugin, neovim
Kibana Object Format
A Kibana plugin for displaying objects and arrays of objects.
Stars: ✭ 100 (-89.76%)
Mutual labels:  plugin, formatter
Google Java Format Gradle Plugin
Stars: ✭ 147 (-84.95%)
Mutual labels:  plugin, formatter
Close Buffers.vim
📖 Quickly close (bdelete) several buffers at once 📕
Stars: ✭ 99 (-89.87%)
Mutual labels:  plugin, neovim
Vim Subversive
Vim plugin providing operator motions to quickly replace text
Stars: ✭ 168 (-82.8%)
Mutual labels:  plugin, neovim
Vim Crates
Handle Cargo dependencies like a Rustavimean.
Stars: ✭ 54 (-94.47%)
Mutual labels:  plugin, neovim
Vim Yoink
Vim plugin that maintains a yank history to cycle between when pasting
Stars: ✭ 225 (-76.97%)
Mutual labels:  plugin, neovim
Gina.vim
👣 Asynchronously control git repositories in Neovim/Vim 8
Stars: ✭ 587 (-39.92%)
Mutual labels:  plugin, neovim
Vim Dirvish
Directory viewer for Vim ⚡️
Stars: ✭ 929 (-4.91%)
Mutual labels:  plugin, neovim

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

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

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

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

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].