Rewrap text (e.g. after writing and switching on autowrap after the fact, or if editing a paragraph)

Is there a simple command to rewrap a selection similar to how one does gq<motion> in vim? I find myself clumsily reflowing edited paragraphs by hand and I’m not sure I want to set autowrap-enable on certain filetypes.

You can use fmt to wrap selection:

map global user W '|fmt --width 80<ret>' -docstring "Wrap to 80 columns"

Use it with <a-i>p to wrap whole paragraph.

Of course map this to any key you want :slight_smile:

Weird. I never knew about fmt (I’ve always used fold for this.) fmt seems a little nicer.

I map = to |fmt -w $kak_opt_autowrap_column so the one configuration option controls both auto wrapping and manual wrapping.

I also have:

add-highlighter global/ column '%opt{autowrap_column}' default,bright-black

…so there’s a subtle visual indicator of the wrap column, too. The only problem is that it highlights the wrap column itself, when what I’d really like is to highlight the column past the wrap column, like Vim’s set colorcolumn=+1, but to get that working I suspect I’d need to write a hook and some shell-script, so I haven’t gotten around to it.

1 Like

@Screwtapello could you use some shell arithmetic expansion?

add-highlighter global/ column '%sh{$(($kak_opt_autowrap_column + 1))}' default,bright-black

… or some such? I haven’t tested as I have to run momentarily.

@TeddyDD This is perfect. One of the things I love so much about kakoune is that it really embraces piping and external commands. I should have thought there would be something like fmt out there…

Just to add 2¢, I use par instead of fmt as it’s a bit smarter, especially when rewrapping comments. Though sometimes it gets too smart and outputs unexpected result =)

I clearly hadn’t thought about it too hard, but you’re right. I had to add an “echo” to prevent the shell trying to execute “81” as a command, but other than that, it worked fine!

Thanks!

How about re-flowing the current paragraph as you type with a one-shot insert command?

Something like a reflow command to automatically wrap the paragraph being edited for the whole insert session.

Something like Emacs auto-fill-mode would be cool thing to have. And support for different justifications is essential

What is it?

auto-fill-mode - a minor mode that automatically executes fill commands as you enter any word separator characters after fill-column and supports different justifications.

Kakoune’s autowrap plugin already has the autowrap_format_paragraph option, which reformats the whole paragraph you’re typing in, rather than just inserting a newline as you’re typing a long line.

It doesn’t have support for different justifications, of course, since that depends on whatever tool you’re using to do the reformatting.

auto-fill-mode, and all Emacs fill commands, are doing reformat too. That’s why I’ve mentioned those in the first place.

Still awesome to hear about the autowrap_format_paragraph option. That I think takes care of a lot of my use cases, and format commands or the fold/fmt/par commands as a backup. Would love to see this be a bit more intelligent though, especially considering kakoune’s focus is on being a good code editor not just a good text editor.

Actually, checking out the autowrap_format_paragraph option’s source I noticed that the autowrap_fmtcmd option let’s us replace fold (which it uses under the hood) with something else… so that covers even more… Expanding on the original response, instead of binding a key to format the selection with a custom command, we could use the $autowrap_fmtcmd option to pass the same tool in both instances.