aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteven Arcangeli <stevearc@stevearc.com>2024-07-15 00:06:56 -0700
committerSteven Arcangeli <stevearc@stevearc.com>2024-07-19 08:41:31 -0700
commit739bda134ad74efc54ea5c63f100ad4ea42ebed6 (patch)
tree221fafb49a8d04b86579a5ab077c5561417662f9
parent8b0e62b731429ecd89cdb6c6b8f004f8468bcf71 (diff)
doc: add recipe for leaving visual mode after format
-rw-r--r--README.md1
-rw-r--r--doc/recipes.md18
2 files changed, 19 insertions, 0 deletions
diff --git a/README.md b/README.md
index 1db5529..3c5e826 100644
--- a/README.md
+++ b/README.md
@@ -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" })
+```