Closed andrei0427 closed 3 months ago
you can, but overwriting a neovim keymap has some consecuences you would have to think about.
You can just use leader+f to format (which you also don't need, as this plugin automatically formats whenever you save your file)
It's worth learning what the default behavior is and what it's used for. I recommend :help =
and :help gq
. In brief, =
is used to trigger equalprg
(a function you define to run on =
), or if it's blank, it uses indentexpr
(a function you define to set the indent level). gq
, on the other hand, uses
formatexpr. So
=is typically used for setting the indentation, and
gqis used to format. There are some notes in the docs for how to use conform as the
formatexpr, and if you do that then
gqwill perform formatting. If, after all of this, you still want to override
=` to do formatting, then you can do so by setting a keymap
vim.keymap.set({ "n", "v" }, "=", function()
require("conform").format({ async = true, lsp_fallback = true }, function(err)
if not err then
-- If we formatted in visual mode, escape to normal mode after formatting
if vim.startswith(vim.api.nvim_get_mode().mode:lower(), "v") then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", true)
end
end
end)
end, { desc = "Format buffer" })
Hi @stevearc, I tried the settings you pasted above, it works perfectly on formatting entire buffer content, but once I tried to format a selected range, the behavior of =
and gq
are different, do you have any clue on it?
before
selected and format with =
(seems nothing changed)
selected and format with gq
(works correctly)
Hi, I'm a new nvim user coming from vscode (with vim bindings) and installed this plugin via the kickstart.nvim project.
Apologies if the answer to this question is obvious; but how do I change the mapping of the
=
button to call prettier via the plugin to format the selected range?I checked the docs and other past issues/questions but I couldn't find an answer. It seems that a different formatter altogether is being used when I use
=
.I haven't changed any defaults in the default config for conform:
Thanks!