Sublime Text style multiple cursor select/add mapping

sometimes it feels more natural to select word and jump to next occurence(s), so instead of doing <a-i>w*<s-n><s-n><s-n>... multiple times I use this command:

define-command -override -docstring "select a word under cursor, or add cursor on next occurrence of current selection" \
select-or-add-cursor %{ execute-keys -save-regs '' %sh{
    if [ $(expr $(echo $kak_selection | wc -m) - 1) -eq 1 ]; then
        echo "<a-i>w*"
    else
        echo "*<s-n>"
    fi
}}

map global normal '<c-d>' ': select-or-add-cursor<ret>' -docstring "add cursor on current word, and jump to the next match" 

Basically what it does, is if the selection is only one char, it selects current word. If selection is more than one char, then the same shortcut will select next occurrence. It canā€™t select single letter items though, but I never thought I need it, since I can *<s-n> which is just one key more. It also adds more control since selecting regions and then filtering it will select all occurrences, but here we have more control since we can skip some words if we need.


Iā€™ve decided to update this post with a more robust version that doesnā€™t use any shell expansions thus much faster an based on selection only:

define-command -hidden -docstring \
"select a word under cursor, or add cursor on next occurrence of current selection" \
select-or-add-cursor %{
    try %{
        execute-keys "<a-k>\A.\z<ret>"
        execute-keys -save-regs '' "_<a-i>w*"
    } catch %{
        execute-keys -save-regs '' "_*<s-n>"
    } catch nop
}

Iā€™m using this version for a long time already and it works much faster.

4 Likes

Thanks
I added this on my kakrc.
Donā€™t know if it should be add to Kakoune ?

No. It is fine to use this as a mapping.

Donā€™t want to sound controversial, but I kind of agree with @lauccode.
This is one of the features that made Sublime successful. Itā€™s there, itā€™s discoverable, and just works w/o configuration.

Iā€™d expect:

  • <c-N> to keep selecting inner word
  • <a-N> to skip the currently selected token and jump to the next matching one.
    Not married to the mappings, just providing an example.

If Kakouneā€™s goal is to really reduce the number of keystrokes and make multi cursor editing the default way of thinking, this would be a good candidate.

I can make a plugin of it, but one of goals of kakoune is to force users to experiment and build solutions of problems that they encountered. Like this one. Itā€™s fairly simple and gives me what I want. And kakoune has all needed building blocks to do such and more complex stuff. Unless this stuf is too complex to implement by semi-average user, such things will never really be in kakoune.

I donā€™t disagree with you, just saying that such a core feature being built in would potentially increase adoption, which is good for the editor and the community.

If everyone is going to implement it in the same way, it might make sense to be built in rather than re-implemented over and over.

Kakoune goal is also to have consistent set of normal command. Your proposal would break the orthogonality of the n command which follow the same pattern than other: shift extend and alt go backward. (same behavior for f, tā€¦)

For some unexperiment user of Kakoune and some persons which would like to have an editor ā€œkey in handā€ it should be nice to have a sort of set of plugins with associated kakrc ?
Maybe it is too early to speak of ā€œkakoune distributionā€.
This should avoid to break Kakoune goal.

In my experience every of such distributions were a bad thing. They are slow, overengineered and usually are not working how I really want it to. The power of Vim is that you can make it work how you like. The power of Emacs is that you can make it work how you like. The power of Kakouneā€¦ do I need to continue, itā€™s obvious.

The power of distribution on the other hand is that you donā€™t need to configure it, but thatā€™s not the kind of power I want. So distributions are bad thing in my opinion.

Iā€™ve heard a lot of thoughts in a way that ā€œif you want a distribution of editor instead of configuring editor, then this editor isnā€™t for youā€. I donā€™t fully agree with this, because such distribution can show some capabilities in editor customization, and user may switch to vanilla once he figures out what he wants, but generally it is true, since such users would be far more productive with a simpler editor where everything is done for you mainly by developers.

Recently, lenormf started an ā€˜out of the boxā€™ project which aims to cover basic needs common to most Kakoune users: GitHub - lenormf/out-of-the-box: A single-file collection of useful configuration options/mappings/commands for Kakoune users

1 Like

I quite like that approach, itā€™s more practical than the scattered snippets from the wiki (download a single file and comment stuff as needed), easier to maintain, and itā€™s an good way for new users to discover functionality with small pieces.

Like said, Iā€™m not married to the mappings. Would love to keep the conversation on if this should be a core feature

Is that a goal? I hope it is not. A focus on consistency and orthogonality and hence a degree guess-ability ā€“ that is what brought me to Kakoune and what keeps me here. Whenever @mawww has to break something to make it more consistent or more orthogonal I am always in favor of it.

I think that Iā€™ve started to think this way after seeing this comment: https://github.com/mawww/kakoune/issues/1038#issuecomment-269011973

My understanding is that Kakouneā€™s values are generally minimalism and orthogonality and extensibility over completeness and easiness and integration.

Kakoune is often weird and surprising, but not gratuitously. Itā€™s not like a puzzle in a video game, deliberately obscure for the sake of obscurity, but because Kakoune is meticulously following its own rules. It may be different to other editors, or even objectively worse than other editors, but Kakoune is doing the Most Kakoune Thing.

I read that as a nod to minimalism, Kakoune wonā€™t do everything. That said, if it doesnā€™t do ā€œenoughā€ Kakoune Distributions will become a thing ā€“ and down that path is pain. I think there is some stuff Kakoune has that seems crazy (but awesome) to my vim tastes like alignment. The line between fundamental and extraneous is always personal, but you have to try to strike a sane balance.

With current approach Kakoune is editor for tinkerers. You get decent experience out of the box and a lot of building blocks to customize it.

Itā€™s not over-complicated like Emacs and It has sane defaults in contrary to Vim. If we ever see Kakoune distribution it will probably be targeted at users who donā€™t want to tinker and customize, just get job done.

I doubt it will huge monstrosity like Spacemacs tho. If Iā€™d like to create SpaceKakoune today, targeting users who donā€™t like to tinker I would bundle:

  • plug
  • expandtab
  • surround
  • create directory on save
  • tab, shift-tab to cycle through completions
  • system clipboard support
  • line numbers by default
  • modeline-parse and editorconfg-load on buffer load

Itā€™s quite short list I think

And then we all doomed. People are complaining about things, but when distribution arrives people complain about distribution and then new distribution arrives and people complain aboutā€¦ Itā€™s never ending. There are already Spacemacs, Doom Emacs, Prelude, Frontmacs, Emacs-live, Purcellā€™s .emacs.d, Emacs24 Starter Kit, Oh-My-Emacs, Cabbage, Graphene, Ohai Emacs, Emacs-Bootstrap, Scimax, better-defaults and possibly more of *macs distros. Itā€™s great for diversity but it is essentially the same as sharing your configuration file, and believe me, no way your particular config will be comfortable for everyone. Usually distributions are bad thing, that breaks community. Vim has less distributions because it is less hackable compared to Emacs and it is more complete than Emacs out of the box. I think thatā€™s the direction that should be adopted by Kakoune.

And by the way, thatā€™s really bothers me when you call a distribution of some editor a SpaceEritor. Thatā€™s wrong approach. Spacemacs And SpaceVim are called this way only because they use space key as centric way of interaction with commands. Itā€™s unnecessary to call Kakoune distribution ā€œSpaceKakā€ unless it uses Space and mnemonic shortcuts like in Spacemacs or SpaceVim. For Kakoune to become a SpaceKak, it doesnā€™t even need to have all this fancy stuff like plugin management and other tweaks - only the space key as a global shortcut leader and a mnemonic groving narrowing system for them.

I think kakoune is already well done and the philosophy too.
It make me discover tmux and ranger for example. And it is right, why lost time to develop things which are already well done if we can use it.
Maybe continue the wiki to explain how to make it as an IDE with the most used tools should be enough.