In this section we walk through a list of basic concepts in design.
To get a better understanding on movement commands in NORMAL state, it’s important to know what a selection is. We call it selection to distinguish from region.
A selection is a region with the direction (forward/backward), the type (char/word/line/etc…) and an expandable flag.
For example, we can say we have a selection which is
- from point 7 to 12
- with forward direction
- with the type word
- and is expandable.
And there are 3 rules that decide the behavior of a movement
- RULE 1 commands except for char/word/symbol have no opinion on direction, they will follow the current direction when meet same selection type.
- RULE 2 a selection will be expanded if the movement and current selection have the same type, and the current selection is expandable.
- RULE 3 these commands create an expandable selection:
meow-*-expand
meow-mark-word
meow-mark-symbol
meow-line
It sounds complicated at first. But it will be seamless once you get used to it.
A few cases to help you understand.
CASE 1 To select 3 lines, do meow-line
3 times. meow-line
will create an expandable line selection.
CASE 2 To reverse select 3 lines, do negative-argument
, then meow-line
3 times. line
will follow the direction.
CASE 3 To select the previous, current and next words, do meow-mark-word
, meow-back-word
and meow-next-word
.
Back and next will expand, because meow-mark-word
creates an expandable word selection.
CASE 4 To select the next word, do meow-next-word
twice.
The current word won’t be selected because meow-next-word
will create a nonexpandable word selection.
Emacs has built-in secondary selection support. By default, you create a secondary selection by dragging with Alt + left mouse button. It can be a range or just a single point.
Since the secondary selection has no relation to the cursor, there are a lot features built on top of it.
FEATURE 1 For text swap. See command meow-swap-grab
and meow-sync-grab
.
FEATURE 2 To represent a range for kmacro application. See Meow BEACON state.
FEATURE 3 To mark a text to be inserted in the minibuffer prompt later. See meow-grab-fill-commands
.
There are many ways to multiedit in Emacs. Following are the parts I know:
- pros: supports simple transformation, no extra keys
- cons: not for complex transformation, hard to undo
counter example:
1 2 3 => [| "1" |] [| "2" |] [| "3" |]
You can do it, but it’s not as simple as it should be.
- pros: flexible, great compatibility
- cons: verbose, bad visual feedback
Same counter case as with kmacro
- pros: no lag on recording, great compatibility, flexible, arbitrary operation, macros can be used later
- cons: verbose, can only be applied to a single position, or each line in a region, no visual feedback, hard to undo
counter example:
x-y-foo-bar-baz => x_y_foo_bar_baz
More keys (record, finish, call) make it useless in a very simple case.
- pros: simple, interactive application (y/n/!), fastest, good visual feedback (with anzu package)
- cons: not flexible, have to type more keys
counter example:
foo-1-bar foo-2-bar foo-3-bar => bar-1-foo bar-2-foo bar-3-foo
You can do it with query-replace, but typing regexp foo-\([0-9]\)-bar
and bar-\1-foo
requires more keys.
- pros: easy to use for insertion
- cons: not flexible
counter example:
foo bar foo bar foo bar => foo baz foo baz foo baz
Just can’t do things like this
- pros: less keys, easy to specify affected range, good visual feedback
- cons: no arbitrary transformation, only for same occurs
counter example:
1 2 3 => "1" "2" "3"
- pros: flexible, good visual feedback, intuitive
- cons: lag for many cursors, operation not re-useable, bad compatibility
counter example: whenever number of cursors > 100
After each time you type, multiple cursors has to run hooks, backup/restore variables for all cursors
Meow embraces kmacro, and tries to improve the experience by collapsing undo boundary and introducing BEACON state.
(text-mode is used here, no additional setup required, assuming meow-setup for Qwerty is used)
1 2 3 => [| "1" |] [| "2" |] [| "3" |]
- select the whole content, then activate a secondary selection with
G(meow-grab)
b(back-word)
to create fake cursors at each word beginningF3
to start macro recording- type
F4
to end macro recording and apply to all fake cursors
x-y-foo-bar-baz => x_y_foo_bar_baz
- select the whole content, then activate a secondary selection with
G(meow-grab)
-(negative-argument) f(meow-find) -
to search backwards for the character-
, which will create a fake cursor at each-
- quick start recording and switch to insert state with
c(meow-change)
(the character under the current cursor is deleted) - type
_
ESC
to go back to NORMAL, then the macro will be applied to all fake cursors.
foo-1-bar foo-2-bar foo-3-bar => bar-1-foo bar-2-foo bar-3-foo
- select the whole content, then activate a secondary selection with
G(meow-grab)
x(meow-line)
to create fake regions at each lineF3
to start macro recording (default fake cursors are in the same column)- select bar with
w(mark-word)
, then activate a secondary selection withG(meow-grab)
- select foo, swap with the secondary selection with
R(meow-swap-grab)
. F4
to end macro recording and apply to all fake cursors
foo bar foo bar foo bar => foo baz foo baz foo baz
- select the whole content, then activate a secondary selection with
G(meow-grab)
- move to bar, select it with
w(mark-word)
, create fake regions at each bar - quick start recording and switch to insert state with
c(meow-change)
(current bar is deleted) - type baz
ESC
to go back to NORMAL, then the macro will be applied to all fake regions.
Why another modal editing package in Emacs?
Emacs is the one editor with the most modal editing schemes in the world. Before I started working on Meow, there were a few options (listed at the end).
Unfortunately, none of them satisfy me. I want a modal editing with the following features.
- Customizable command layout
- Using existing keymap (both buit-in and third party) without modifier keys
- A set of efficient commands
- Lightweight, fast startup time
customizable command layout | using existing keymap | efficient command set | Lightweight | |
---|---|---|---|---|
evil | no | no | yes | no |
xah-fly-keys | no | no | yes | yes |
boon | no | yes | yes | yes |
god-mode | yes | yes | no | yes |
modalka | yes | possible | no | yes |
ryo-modal | yes | possible | no | yes |
kakoune.el | no | no | yes | yes |
A complete Vim emulator in Emacs. Before Emacs, I was using Vim. So my Emacs journey started with Evil. However, Evil has a few problems.
- high cost on integration with other packages. Basically an editing-related package won’t play well with Evil if it doesn’t know Evil.
So there’s evil-collection and other evil-* packages.
- Communities like spacemacs and doom emacs prefer to organize keybindings with evil-leader. The result is easy to use, but it takes time to maintain.
Introducing another keybinding system usually results in a complex configuration.
- Vim is designed for the Qwerty keyboard layout. H/J/K/L is meaningless on other layouts.
- Vim is old, and there are some modern alternatives, like Kakoune. We are on Emacs and we have more choices.
- Evil is heavy, its startup time is 10X longer than other modal editing packages.
God mode is small, simple and easy to start with. If “no modifier keys” is all you want, god-mode is a good choice.
The only problem: it’s not a complete modal editing solution. God mode lacks a set of commands which is necessary for maximizing the benefits of sing-key commands.
Modalka allows the user to define their own command layout. It’s more flexible than god-mode and requires more configuration. It has the same problem as with God Mode, it’s not a complete modal editing solution.
The same problem as with modalka and God Mode.
Xah-fly-keys is declared to be more efficient than vim, or any keybinding set in history. Personally I don’t like DWIM (Do What I Mean) style commands. Of course DWIM can reduce the number of commands, but I’d rather make commands more orthogonal.
xah-fly-keys has a pre-defined leader keymap, I don’t think it’s enough for my case.
Boon has an efficient command layout, a good approach to executing commands without modifier keys. It’s very close to what I wanted before I started with Meow.
Boon integrates with expand-region, multiple-cursors, avy, etc. I think we can have a better way for these purposes.
Kakoune.el is a package trying to bring kakoune’s commands to Emacs.
I like how kakoune deals with selections, but it uses both alt & shift a lot. Since I am going to use modal editing, I prefer to avoid modifier as much as possible.