-
-
Save datsfilipe/d885e3a44de560ab8ff629275e2551a7 to your computer and use it in GitHub Desktop.
Format all files in directory using Neovim keymap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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