micro icon indicating copy to clipboard operation
micro copied to clipboard

Emacs/Vi keybindings / multi-key sequences

Open lassik opened this issue 8 years ago • 14 comments

How do you feel about supporting Emacs keybindings (as an aesthetic, social and technical issue)?

They could come standard (with a command line flag or configuration option to turn them on). Or if the keybinding system supported multi-key sequences (such as pressing Ctrl-x followed by Ctrl-f to issue the open-file command) perhaps us Emacs fans could simply remap the keys for ourselves.

lassik avatar May 01 '17 09:05 lassik

I'm not opposed to this and I think it would be nice if the keybinding system were more powerful (allowing for vim-like key bindings as well, although not by default) but I don't think it's a very high priority at the moment

zyedidia avatar May 02 '17 02:05 zyedidia

Option for nano keybindings would be also nice, especially for people converting from Nano to Micro

luciusmagn avatar May 04 '17 14:05 luciusmagn

I think it should mostly be possible to emulate nano keybindings with micro's current keybinding system.

For example you could put something like this in your ~/.config/micro/bindings.json to achieve nano keybindings:

{
    "CtrlO": "Save",
    "CtrlR": "Open",
    "CtrlY": "PageUp",
    "CtrlV": "PageDown",
    "CtrlK": "CutLine",
    "CtrlU": "Paste",
    "CtrlX": "Quit",
    "CtrlW": "Find"
}

zyedidia avatar May 04 '17 16:05 zyedidia

I see, I had the idea, but I was worried it might conflict with default keybindings and may require manual unbinding of all the conflicting keybindings. If that's not the case, then I am glad to know that

luciusmagn avatar May 04 '17 16:05 luciusmagn

I started working a bit on vim bindings in micro and added some basic movement motions. It was fairly simple, but to be useful i think the configuration system has to be extended. It would be nice to be able to script your mappings(e.g. with lua) just like you do in your vimrc(but with vimscript). It could be written in a way so that implementing other types of "event handlers" is easy as well.

@zyedidia If you got time i would love some feedback on some of the possible design decisions. I plan on doing this anyway for personal use & experience, but if we can discuss some design decisions then maybe it can be integrated into micro in the future.

linde12 avatar Jul 27 '17 11:07 linde12

It is possible to bind keys in the init.lua file as well. You can call the BindKey function:

BindKey("CtrlC", "Copy")

I thought about possible vim keybinding support in the form of a plugin but in the end it required too much effort for too little value (I don't really see too much reason for a vim user to use micro over vim, or really any other editor over vim).

zyedidia avatar Jul 27 '17 13:07 zyedidia

@zyedidia I also thought of doing it with plugins, which would allow emacs/vi/vis/etc. bindings, but i don't think lua is optimal for this kind of state machine.

As for the reason i would switch from vim to something like micro( + vim bindings) is primarily because of speed & "freshness".

Micro has a larger core than vim's truly minimal core, which to me is a good thing as most of the plugins i use in vim are essential and (imo) should have been included to begin with.

Every time i open vim it has to load 10-20 vimscript plugins to get the behavior i could get with micro out of the box. Micro starts in milliseconds while vim/nvim can take seconds. I also think that vimscript is terrible, and lua is a good improvement.

I like how xi-editor works with the separate frontend and backend processes. Plugins can be any kind of executable written in any language and just pipe JSON RPC-style messages to the core to interact with it. This way it becomes easy for a developer to interact with e.g. the cursor using their language of choice. I've illustrated how this works below:

                                       +----------------+
           +-------------------------> |Frontend process|
           |                           +--------+-------+
           |                                    |
+----------+----+                               |
|               |                               |
|               | <-----------------------------+
|               |
|               |
|Backend process|             RPC EVENT
|               |     {"type":"keypress", key:"h"}            +-------------------------+
|               +-------------------------------------------> |    vim plugin process   |
|               |                                             +-----+-------------------+
|               |                                                   |
+---------------+ <-------------------------------------------------+
                                RPC RESPONSE
                             {"cmd":"CursorLeft"}

However adding something like this is no petty task, so i'm not entirely sure how to proceed with this.

linde12 avatar Aug 02 '17 12:08 linde12

Error

Unknown keybinding: CtrlXCtrlS

Press enter to continue

avelino avatar Feb 26 '18 14:02 avelino

@zyedidia I could look into this. Could you point me at where to get started? If I took as the first example, pressing Ctrl-x followed by Ctrl-f to issue the open-file command, where in the code would I start to add multikey support?

glycerine avatar Mar 10 '18 19:03 glycerine

I just opened up https://github.com/zyedidia/micro/pull/1299 which supports multi-key bindings. To give it a try, pull my branch, build the code, and place this in ~/.config/micro/bindings:

{
    "Ctrl-a": "StartOfLine",
    "Ctrl-e": "EndOfLine",
    "Ctrl-x Ctrl-c": "Quit",
    "Ctrl-z": "Suspend",
    "Ctrl-x Ctrl-s": "Save",
    "Ctrl-x Ctrl-f": "OpenFile",
    "Ctrl-s": "Find",
    "Ctrl-r": "FindPrevious",
    "Ctrl-g": "Escape",
    "Ctrl-k": "CutLine",
    "Alt-b": "WordLeft",
    "Alt-f": "WordRight",
    "Ctrl-x o": "NextSplit",
    "Ctrl-x 0": "Unsplit",
    "Ctrl-x 2": "HSplit",
    "Ctrl-x 3": "VSplit",
    "Ctrl-j": "JumpLine",
    "Ctrl-y": "Paste",
    "Ctrl-v": "CursorPageDown",
    "Alt-v": "CursorPageUp",
    "Ctrl-_": "Undo",
    "Esc >": "CursorEnd",
    "Esc <": "CursorStart",
    "Alt-x": "CommandMode",
}

This is just a start of key bindings that are similar to the default emacs key bindings. This has allowed me to try out micro with "rewiring" my brain ;).

Note that each key binding is separate by a single space character since I thought this would be more intuitive.

brimworks avatar Mar 27 '19 14:03 brimworks

@zyedidia

I thought about possible vim keybinding support in the form of a plugin but in the end it required too much effort for too little value (I don't really see too much reason for a vim user to use micro over vim, or really any other editor over vim).

As a fairly seasoned vim user, my use-case to venture out of vim are: (1) simple curiosity about other tools and design philosophies, especially if they are interesting like micro; (2) usually there are a few features that while in most cases you could closely approximate with vim, they don't feel as natural as the editor/IDE that was supports it from the ground up.

Having the bindings available (doesn't really matter if batteries-included or a plugin), lowers the biggest barrier of entry and makes it so I can ease into it without having to rationalize learning a whole new mental model, Nothing wrong with that, but it'd be like learning a new language every time you watched a foreign movie; sometimes subtitles are nice.

In my particular case, I have a multi-machine/OS setup, and I don't need my elaborate dotfile vim configuration on each one, yet a vanilla install would also lack some of the niceties I want. I found micro recently as a good candidate for what I look for in a modern, elegant, opinionated, lightweight out-of-the-box editor, and so I was motivated to dedicate some time into it. Yet, I know I could "hit the ground running" with vim bindings, which is the only thing holding me back when you have a looming deadline and you're reading the docs to figure out how to switch a tab.

This is just in response to your question above, and not commentary about micro as a whole. Wanting this feature is very much a "me" issue, I was just hoping to elucidate maybe why a vim user would ever want to use micro or another editor for that matter. I'll be following this project :].

ebigram avatar Aug 04 '20 14:08 ebigram

Thanks for your message, I have decided that it would be beneficial to have vim keybinding support and have been working on upgrading the keybinding system to support key sequences on the keybindings branch. Hopefully once that is complete, vim or emacs keybindings could be supported through a plugin. I think this is the highest priority feature at the moment, although I have been moving slowly on it so far.

zyedidia avatar Aug 04 '20 17:08 zyedidia

Any update on this topic? interested to try it out with vim binding

helman avatar Jul 11 '22 01:07 helman