» npm install prettier-plugin-sh
Videos
Most languages have their own fmt command.
Prettier is for web languages that are run by committees and don't have the same kind of leadership pushing forward practical developer tools.
For shell languages use shfmt and shellcheck:
- shfmt source: https://github.com/mvdan/sh
- shfmt cheatsheet: https://webinstall.dev/shfmt/
- vim-shfmt cheatsheet: https://webinstall.dev/vim-shfmt/
- shellcheck source: https://github.com/koalaman/shellcheck
- shellcheck cheatsheet: https://webinstall.dev/shellcheck/
- vim-shellcheck cheatsheet: https://webinstall.dev/shellcheck/
These follow Google's shell style and linting guidelines. Not that Google is perfect, but this is good stuff - it taught me proper shell scripting 10x better in just one year of using them than after 15+ years of doing it here and there without them.
Prettier can, with the setting --embedded-language-formatting=auto (see here), format templates.
However, the languages it can format (from it's extension page on VSCode) are:
JavaScript · TypeScript · Flow · JSX · JSON
CSS · SCSS · Less
HTML · Vue · Angular
GraphQL · Markdown · YAML
So I'm afraid no, this cannot be accomplished by Prettier as bash or shell are not on this list.
» npm install prettier-plugin-embed
Hi,
I tried to post this in the weekly thread, but I get errors. (I assume it is due to comment length, but there is literally just an empty error)
EDIT: I have solved the issue and every time I try to post the solution (either by editing or adding a comment) I get that same empty error from reddit. for solution, see 2 comments below.
I have null-ls setup to run prettierd for html files. (that works like a charm)
Now I would like it to format html files with go templates and it does not seem pick up my prettierd config in the root of my project:
// <project_root>/.prettierrc
{
"plugins": ["prettier-plugin-go-template"],
"overrides": [
{
"files": ["*.html"],
"options": {
"parser": "go-template",
},
},
],
} NullLsInfo shows prettierd is active, but not the supported list is empty.
How do I make sure that the .prettierrc is used?
I used lazy.nvim to setup null-ls see setup below.
Any help would be appreciated
return {
"jose-elias-alvarez/null-ls.nvim", -- configure formatters & linters
event = { "BufReadPre", "BufNewFile" },
config = function()
-- import null-ls plugin
local null_ls = require("null-ls")
local null_ls_utils = require("null-ls.utils")
-- for conciseness
local formatting = null_ls.builtins.formatting -- to setup formatters
local diagnostics = null_ls.builtins.diagnostics -- to setup linters
-- to setup format on save
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
-- configure null_ls
null_ls.setup({
-- add package.json as identifier for root (for typescript monorepos)
root_dir = null_ls_utils.root_pattern(".null-ls-root", "Makefile", ".git", "package.json"),
-- setup formatters & linters
sources = {
-- to disable file types use
-- "formatting.prettier.with({disabled_filetypes: {}})" (see null-ls docs)
--formatting.prettier.with({
-- extra_filetypes = { "svelte" },
--}), -- js/ts formatter
formatting.prettierd.with({
filetypes = {
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"vue",
"css",
"scss",
"less",
"html",
"json",
"jsonc",
"yaml",
"markdown",
"markdown.mdx",
"graphql",
"handlebars",
"gotmpl",
},
}),
formatting.stylua, -- lua formatter
formatting.gofumpt, -- golang formatter
-- formatting.goimports, -- golang imports
diagnostics.eslint_d.with({ -- js/ts linter
condition = function(utils)
return utils.root_has_file({ ".eslintrc.js", ".eslintrc.cjs" }) -- only enable if root has .eslintrc.js or .eslintrc.cjs
end,
}),
},
-- configure format on save
on_attach = function(current_client, bufnr)
if current_client.supports_method("textDocument/formatting") then
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
buffer = bufnr,
callback = function()
vim.lsp.buf.format({
filter = function(client)
-- only use null-ls for formatting instead of lsp server
return client.name == "null-ls"
end,
bufnr = bufnr,
})
end,
})
end
end,
})
end,
}