Kakoune's editing power

Here and elsewhere, e.g.:

https://www.reddit.com/r/kakoune/comments/pxsl2c/is_kakoune_capable_of_everything_that_vim_can/

there has been some discussion of the relative editing power of Kakoune and Vim/Neovim. I always thought that anything that Kakoune does with multiple selections could be duplicated in Vim with search-and-replace plus macros.

Yet, I recently had to perform the following editing task: replace each occurrence of “execute-keys” by “exec”, but only in lines with “printf” inside.

In Kakoune, this is a piece of cake: select the whole buffer, split on line returns, keep only the lines with “printf”, select “execute-keys” and replace with “exec”.

But in Vim… how could one do this? (Suffering from PTSD = Post-Traumatic vimScript Disorder, I can barely recall anything about Vim.) Macros do not have conditionals inside, so would macros be of any help?

Perhaps some of you, ex-vimmers, could tell me.

1 Like

Found some information here:

By using the :g command, one could select all lines with “printf”, then execute a search-and-replace on each line.

Ok, so my task can be done in Vim after all. But what about:

replacing A with B in each line that contains C and D (not necessarily in this order)

or, better:

replacing A with B in every paragraph that contains C?

Again, a piece of cake in Kakoune. In Vim, however…?

Hmm… thinking twice, there always seems to be an equivalent with macros. It would be nice to find something doable in bare-bones Kakoune that cannot be done in bare-bones Vim, but pending this, we can at least note that the Kakoune solution is much easier (with constant visual feedback).

1 Like

Thinking aloud:

What about replacing A with B in every paragraph that contains C and D?

One cannot search for a C .+ D regex and then select the containing paragraph (because there is no guarantee that this paragraph is one and only one), then select A and replace with B.

1 Like

Another thought:

Replacing A with B in every line that contains C but not D.

(More generally, anything that depends on the extra power of structural regexes.)

Yet another thought:

Replacing by C the second occurrence of B that occurs in any line that contains A. (I think ex’s substitution command works only on the first occurrence or on all of them.)

Edit: not doable with :g, but doable with macros, I guess. My two previous suggestions do stand – at least I hope!

Rotating function arguments seems to be painful in all but the simplest cases in barebone vim.

For simple cases, in kakoune, we can just split on commas and rotate selection. For more complex cases, we can use an object selection mode ( or , followed by u for argument).

Painful, definitely, but still doable with regex searches and two named registers (one to save the first argument, the other to save the second argument).

Along my rambling, I think I found two valid examples that are a piece of cake in bare-bones Kakoune and impossible (not just painful) in bare-bones Vim:

(1) replacing A with B in every paragraph that contains C and D

(2) replacing A with B in every line that contains C but not D

The second example relies on Kakoune’s structural-regex facility for excluding the selections that do not match a given regex.

I hope I’ll get more feedback from the list about these examples, otherwise I might ask for help on a Vim/Neovim list.

This topic is important because it would establish that Kakoune’s model of selection first is objectively superior, in some significant respects, to Vim’s action-first. For a while I have been thinking of replying to the “Why not Kakoune” section in this post:

https://github.com/noctuid/dotfiles/blob/master/emacs/editing.org

but not before fixing the only valid point he or she makes, which is about key mappings. More on this later…