diff options
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | doc/recipes.md | 18 |
2 files changed, 19 insertions, 0 deletions
@@ -433,6 +433,7 @@ require("conform").formatters.shfmt = { - [Command to toggle format-on-save](doc/recipes.md#command-to-toggle-format-on-save) - [Automatically run slow formatters async](doc/recipes.md#automatically-run-slow-formatters-async) - [Lazy loading with lazy.nvim](doc/recipes.md#lazy-loading-with-lazynvim) +- [Leave visual mode after range format](doc/recipes.md#leave-visual-mode-after-range-format) <!-- /RECIPES --> diff --git a/doc/recipes.md b/doc/recipes.md index 97c3cdc..0c085f2 100644 --- a/doc/recipes.md +++ b/doc/recipes.md @@ -7,6 +7,7 @@ - [Command to toggle format-on-save](#command-to-toggle-format-on-save) - [Automatically run slow formatters async](#automatically-run-slow-formatters-async) - [Lazy loading with lazy.nvim](#lazy-loading-with-lazynvim) +- [Leave visual mode after range format](#leave-visual-mode-after-range-format) <!-- /TOC --> @@ -184,3 +185,20 @@ return { end, } ``` + +## Leave visual mode after range format + +If you call `conform.format` when in visual mode, conform will perform a range format on the selected region. If you want it to leave visual mode afterwards (similar to the default `gw` or `gq` behavior), use this mapping: + +```lua +vim.keymap.set("", "<leader>f", function() + require("conform").format({ async = true }, function(err) + if not err then + local mode = vim.api.nvim_get_mode().mode + if vim.startswith(string.lower(mode), "v") then + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", true) + end + end + end) +end, { desc = "Format code" }) +``` |