Skip to content

Instantly share code, notes, and snippets.

@datsfilipe
Forked from inclooder/vim_format_in_dir
Last active January 12, 2025 21:08
Show Gist options
  • Save datsfilipe/d885e3a44de560ab8ff629275e2551a7 to your computer and use it in GitHub Desktop.
Save datsfilipe/d885e3a44de560ab8ff629275e2551a7 to your computer and use it in GitHub Desktop.
Format all files in directory using Neovim keymap
vim.keymap.set('n', '<leader>f', function()
local ext = vim.fn.expand '%:e'
local confirm =
vim.fn.input('proceed with formatting ' .. ext .. ' files? [y/n] ')
if confirm:lower() ~= 'y' then
print 'operation cancelled'
return
end
local ignore_dirs = { '.git', 'node_modules', 'target' }
local exclude_args = table.concat(
vim.tbl_map(function(dir)
return '-E ' .. dir
end, ignore_dirs),
' '
)
local cmd = string.format('fd -e %s %s', ext, exclude_args)
local file_list = vim.fn.systemlist(cmd)
if #file_list == 0 then
print 'no matching files found'
return
end
vim.cmd('args ' .. table.concat(file_list, ' '))
vim.cmd 'silent argdo execute "normal gg=G" | update'
end, { silent = true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment