format.nvim is an asynchronous code formatting plugin for neovim.
By using nvim_buf_get_lines
api, format.nvim is able to format the buffer which has not been saved.
On a formatter success It will update current buffer via nvim_buf_set_lines
, and marks, jumps, etc.
are all maintained after formatting.
With nvim-plug
require('plug').add({
{
'wsdjeg/format.nvim',
cmds = { 'Format' },
depends = {
{ 'wsdjeg/job.nvim' },
{ 'wsdjeg/notify.nvim' },
},
},
})
require('format').setup({
custom_formatters = {
lua = {
exe = 'stylua',
args = { '-' },
stdin = true,
},
},
timeout = 5000,
})
:Format
require('plug').add({
{
'wsdjeg/format.nvim',
config = function()
require('format').setup({
custom_formatters = {
lua = {
exe = 'stylua',
args = { '-' },
stdin = true,
},
},
})
end,
config_before = function()
vim.keymap.set('n', '<leader>lf', function()
local cf = vim.call('context_filetype#get')
if vim.o.filetype == 'markdown' and cf.filetype ~= 'markdown' then
local line1 = cf['range'][1][1]
local line2 = cf['range'][2][1]
vim.cmd(string.format('%s,%sFormat! %s', line1, line2, cf.filetype))
end
end, { silent = true, desc = 'format code block' })
end,
cmds = { 'Format' },
depends = { { 'Shougo/context_filetype.vim' } },
},
})
local augroup = vim.api.nvim_create_augroup('format_on_save', { clear = true })
vim.api.nvim_create_autocmd({ 'BufWritePre' }, {
pattern = { '*' },
group = augroup,
callback = function(_)
vim.cmd('FormatWrite')
end,
})
This feature is inspired by formatter.nvim, instead of using User autocmd, format.nvim uses functions.
require('format').format(bang, user_input, start_line, end_line, {
hooks = {
pre = function(buf) end,
post = function(buf) end,
},
})
Using FormatLock
or FormatWriteLock
commands instead of Format
or FormatWrite
, format.nvim will lock the buffer when formatting.
name | description | optional/required |
---|---|---|
exe |
string , formatter executable or path |
required |
args |
table<string> , list of arguments |
optional |
stdin |
boolean , send data to the stdin of the formatter |
optional |
If you want to read the runtime log of format.nvim, you need to install logger.nvim.
require('plug').add({
{
'wsdjeg/format.nvim',
depends = {
{
'wsdjeg/job.nvim',
},
{
'wsdjeg/notify.nvim',
},
{
'wsdjeg/logger.nvim',
},
},
},
})
[ 15:02:25:277 ] [ Info ] [ format.nvim ] using custom formatter:stylua
[ 15:02:25:277 ] [ Info ] [ format.nvim ] running formatter: stylua
[ 15:02:25:331 ] [ Info ] [ format.nvim ] formatter: stylua exit code:0 single:0
[ 15:03:59:481 ] [ Info ] [ format.nvim ] using default formatter:prettier
[ 15:03:59:482 ] [ Info ] [ format.nvim ] running formatter: D:\Scoop\apps\nodejs\current\bin\prettier.CMD
[ 15:04:00:340 ] [ Info ] [ format.nvim ] formatter: prettier exit code:0 single:0
language | formatters |
---|---|
c | uncrustify, clangformat, astyle |
rust | rustfmt |
markdown | prettier |
json | prettier |
python | yapf |
go | gofmt |
asm | asmfmt |
elixir | mix-format |
java | astyle |
javascript | js-beautify |
Like this plugin? Star the repository on GitHub.
Love this plugin? Follow me on GitHub.
If you encounter any bugs or have suggestions, please file an issue in the issue tracker