In this section we walk through a list of basic concepts in design.
Selection
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.
Secondary 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
.
Multiedit
Ways to multiedit
There are many ways to multiedit in Emacs. Following are the parts I know:
Evil’s n.n.n.n. (Evil built-in)
- 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.
Evil’s :exe (Evil built-in)
- pros: flexible, great compatibility
- cons: verbose, bad visual feedback
Same counter case as with kmacro
kmacro (built-in)
- 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.
query-replace (built-in)
- 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.
rectangle-mark-mode (built-in)
- 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
iedit / evil-multiedit (package)
- 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"
multiple-cursors / evil-mc (package)
- 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
The answer from Meow
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)
case 1
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
case 2
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.
case 3
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
case 4
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 |
Evil
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