Avoiding modifier chording


#1

I’ve been noticing that, even though Kakoune uses less keystrokes, they are often more awkward. For example, Shift+Alt+C (which doesn’t seem to work for me anyway), requires me to use right Alt and right Shift. I’m really happy with the structural regex stuff and the order of operations, so I’m not leaving Kakoune, but I’m wondering what to do about it…

The ideas I have so far are to introduce user modes. I can do one for “extend selection”, which I guess is just kind of like Caps Lock, which is pretty much like v. Hmm, that’s pretty cheap. I’ll try that.

The other idea would be a new one-shot mode for Alt. This could be either my keyboard or a Kakoune user-mode mapping. This still feels awkward.

Some interesting background: Since getting my ErgoDox, I’ve trained myself to use left and right control, and left and right shift correctly. This is kind of not normal, I know (the MacBook keyboard doesn’t have a right control!), but it’s very nice on an ortholinear keyboard. It means I never have to reach and pivot my hands the way I used to for like, Ctrl+C and Ctrl+X (and this was the thing that made emacs so painful when I first tried it, which is why I started using Vim in like 2000).

This works flawlessly 95% of the time. There are times when it doesn’t, like when I’m pairing with a colleague using emacs and I need to type <c-c><c-k>, which is right-ctrl C, left-ctrl K. I tend to type the left and right ctrl keys overlapping, which confuses my ErgoDox (since the control keys are dual-purpose).


#2

I absolutely agree – in Vim I avoided any use a chording because I loathe it. I have accepted a lot more of it in Kakoune, but I don’t love it here either. I very often used <a-i> and I don’t love it. The one shot mode, or even just like alt-toggle mode – sort of reminds me of god-mode in emacs.


#3

Some random ideas how to avoid shift/alt chords:

Spacemacs like mode

There’s no much room left in normal mode, even when you count chords. Spacemacs has a lot functionality hidden under <space> leader key, and it’s easy to do this in Kakoune:

map global normal <space> ,
map global mymode <space> <space> #double space to deselect

Boom, you have new, easily accessible “normal” mode. Space is big, easy to hit and usually you don’t do much with your thumbs. First keys I’d bind in such mode are inner/outer object selection.

Plugins

Those two plugins are perfect combo. Vertical selection will replace C in many cases. Text object is probably my favorite plugin (just before kakoune-auto-percent). Take a look at selector mode - it’s might be what you are looking for.


#4

Ok, so this is my first post.

I too have an ergodox, but my control and alt are mapped to where CAPS and L-Shift are respectively. However, I still prefer avoiding chords. It got me thinking though, if the reason we love vim, evil/god-mode, and kakoune is the idea of modal editing, why not extend that model?

Word Mode

declare-user-mode word
map global normal w ':enter-user-mode -lock word<ret>'
map global word w w -docstring 'select to next word start'
map global word W W -docstring 'extend to next word start '
map global word b b -docstring 'select to previous word start'
map global word B B -docstring 'extend to previous word start'
map global word e e -docstring 'select to next word end'
map global word E E -docstring 'extend to next word end'
map global word q <a-w> -docstring 'select to next WORD start'
map global word Q <a-W> -docstring 'extend to next WORD start'
map global word v <a-b> -docstring 'select to previous WORD start'
map global word V <a-B> -docstring 'extend to previous WORD start'
map global word r <a-e> -docstring 'select to next WORD end'
map global word R <a-E> -docstring 'extend to next WORD end'

def -hidden select-next-subword %{
      exec /[A-Z][a-z]+|[A-Z]+|[a-z]+<ret>
}
def -hidden extend-next-subword %{
      exec ?[A-Z][a-z]+|[A-Z]+|[a-z]+<ret>
}
def -hidden select-prev-subword %{
      exec <a-/>[A-Z][a-z]+|[A-Z]+|[a-z]+<ret>
}
def -hidden extend-prev-subword %{
      exec <a-?>[A-Z][a-z]+|[A-Z]+|[a-z]+<ret>
}
map global word s :select-next-subword<ret> -docstring 'select to next subword'
map global word S :extend-next-subword<ret> -docstring 'extend to next subword'
map global word a :select-prev-subword<ret> -docstring 'select to previous subword'
map global word A :extend-prev-subword<ret> -docstring 'extend to previous subword'

Whenever I start moving around with w, b, and e, I don’t usually do a one off word-wise movement, but rather execute a few in succession before finally editing text. w locks the user into a word-mode, thanks to Kakoune’s -lock flag. It’s a bit harder to get mnemonic mappings, but not all keybindings have to be mnemonic. In this example, they’re spatially related. v and V are mapped to <a-b> and <a-B>, respectively, as an example.

We can now enhance “word-mode” without needing to come up with akward chords a la emacs or excessive keystrokes a la spacemacs. So for instance, in my example, I added subword movements, courtesy of Alex. The entire keyboard freed up for us to add one-key-press movements. That’s powerful.

We just freed up W, <a-w>, <a-W>, e, E, b, B, <a-e>, <a-E>, <a-b>, <a-B>. This starts giving us more economy in the long run, because b/B can now be mapped to buffer operations, for example, as suggested by @Delapouite

Other potential modes
How about a line mode? C-l can enter line mode, then j and k and move lines. o and O can add lines after/before. You can now freely add options to select line excluding leading/trailing whitespaces.

How about a buffer mode? Trigger with b. n and p cycle between buffers.

Paragraph mode! In vim I used to enjoy { and } movement, which moves to the next/prev empty line. Kak needs [p and ]p. [p doesn’t even go to the empty line. j and k can transpose paragraphs up/down?

Brace mode? Now it should be easier to expand, inner block, outer block, etc;

Economics

Determining whether a mode should exist would largely depend on the average amount of related key presses. Something like a “window-mode” probably wouldn’t make sense because if you execute new-window, you’re probaby just doing it once. However, maybe that’s just a limit of my imagination.

Enhancements to kakoune to make custom user modes more powerful

All this said, my current implementation of a “word-mode” isn’t as comfortable as I would like. There are several new features that can make this work better. Some more essential, while others being more experimental;

  • Passthrough option - Perhaps a -passthrough flag which lets keys not defined in your user mode to act like normal mode keys.
  • Exclude keys option - Define specific keys for passthrough. If the passthrough flag is used, then the opposite effect is achieved; keys do are blocked from passing through.
  • One shot binding in lock mode - Ideally, some mechanism would exist to designate certain keys to execute and leave the mode. This could either be a command like :exit-user-mode or a flag like map global _word_ -leave d ':do-something<ret>'
  • Mode stacks - One idea we can experiment is having modes stack on top of each other, either through config or keybindings. While I can’t think of any uses right now, it’s something that can be explored.

Conclusion
I don’t think I mentioned anything revolutionary here. It’s been stated by others in various other ways and these ideas already exist. Hopefully I was able to summarize these concepts in a useful way and present ways to enhance the kakoune program to make modal editing even more advanced.


#5

One idea we can experiment is having modes stack on top of each other, either through config or keybindings. While I can’t think of any uses right now, it’s something that can be explored.

Kakoune already has the concept of stacking modes, at least for insert and normal modes.If you start in normal mode and type i<a-;>i, you wind up with a stack of four modes:

  • the regular base normal mode
  • insert mode
  • a temporary normal mode that exits after one command
  • another insert mode

Unfortunately, user modes (in the sense of declare-user-mode and enter-user-mode) don’t currently participate in the mode stack for historical reasons (see #2569), which means that (among other things) you can’t put <esc> in a user mode mapping to exit the mode, even though that works in an insert mode mapping.

I like the idea of a passthrough option, but I wonder how it would work with mappings in outer modes. For example, if mode X had a mapping with an <esc> to exit mode X, and you activated mode Y on top with passthrough, then you pressed the mode X mapping, would the <esc> exit mode Y instead? Or both X and Y?


#6

Yeah, passthrough might get complicated. I think the preferred alternative is the leave-user-mode option. Or better yet, the ability to call “enter-normal-mode” or enter any user mode from any user mode. I also think that specifying custom keys for passthrough or block would get ugly in terms of syntax, whereas a simple leave-user-mode will fit right in with the current flow.


#7

I used to have an implementation (I got rid of it because the “helper” was more annoying than setting up shortcuts) of a mode with passthrough in gdb.kak. It was back when user-mode didn’t exist.
It’s a bit hacky, so I’m not sure if you want to do this