Handmade Hero»Forums»Code
Piotr Madalinski
9 posts
Basics of modal editors.
I think Casey is misconstruing the notion of 'modal editor', and sadly, nobody pointed it out to him.

I have written a longer post about it but it was lost due to some error during posting - so this time around I will just post a link to an article describing how modal editors (well... at least vi) work:

http://www.viemu.com/a-why-vi-vim.html

I just don't think that Casey's approach of just mapping emacs keybindings into 'modes' without making vi-style DSL for text-object manipulation out of them will lend itself to pleasant editing experience.
511 posts
Basics of modal editors.
tl;dr: you shouldn't be thinking of modal editor like having separate modes but instead of a command architecture where inserting text is a part of a special command.
Mārtiņš Možeiko
2559 posts / 2 projects
Basics of modal editors.
This is excellent post about how to use vim! One of the best I have read.
511 posts
Basics of modal editors.
It also has a good hint that you should have a key that will always take you to command mode.

Casey just has a toggle at the moment. Which of course means that it's hard to know which mode you are in and don't know which mode you just switched to after you press the mode switch key.
jeringa
19 posts
Basics of modal editors.
Was going to add a topic to cover this, but you guys beat me to it...
My two cents was going to be vi

You get into the habit of hitting <ESC> twice just to make sure you are always in command mode

That habit is now so strong with me that I end up doing in whatever editor I'm using and sometimes on command lines when in a shell :)
Casey Muratori
801 posts / 1 project
Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.
Basics of modal editors.
At the moment, it's less about misconstruing the meaning of modal editing and more just me trying to have a modal thing that is the same as the way I used to use Emacs. And I've basically got that, so that's a good thing (TM).

However, I definitely agree that now that I've made the leap, I should go ahead and try to go a bit further and get some of the nice VI editing power. I haven't had a chance to study it enough to really get how it all works yet, but I will try to do so over time!

- Casey
jeringa
19 posts
Basics of modal editors.
*hehe* vi the is best god-awful editor you will ever use

The only reason I went through the pain of learning it was the fact that it was
a) Installed on almost every *nix version I ever worked on
b) Would work on the nastyest crustyest dumb terminal that has every been made
which was a requirement when stuck in a machine room with a dead machine
at 3am and the only thing you could find to connect to the serial console
was the mankyest dumb terminal that no one else wanted and thus was on
the DC crash cart

Not a good reasons to use an editor these days I'll admit; but back when dinosaurs ruled the earth it made sense :)
jeringa
19 posts
Basics of modal editors.
Edited by jeringa on
Just realised that my last post made it sound like I don't like vi ... In fact I do & it's still my go to editor of choice.

If you want to look at one of the nice features and one of my personal favourites is the :% command as it allows a regex/sed style editing along the lines of

:%s/some text/other text/g
which does as expected a replacement of some text with other text globally in the file; this on its own is worth the price of admittance

other handy ones are
the dot command will repeat any command, use / to find and n to find next
the ability to put a number in front of a command is also nice
eg 99dd
where dd deletes a line so 99dd will delete 99 lines
then navigate to the desired location in the code and p to paste it in
use yy to yank a line into the cut/paste buffer

Not sure if any of that is something that would fit with what you want in 4coder, but a regex engine as ugly and nasty as they are, are definitely useful additions
Piotr Madalinski
9 posts
Basics of modal editors.
Casey, I know what you are trying to do, it's just that I'm surprised that it had worked for you as well as it did. If I had to think about which mode I'm in (fancy visual indicator notwithstanding), it would drive me nuts. Maybe it's not such an issue for you.

Regardless, here are some thoughts about the differences between modal and non-modal editors. Maybe it will be useful to you:

Workflow:

In Emacs you do (as far as I understand) a "mark, move, command", while in VI it is a "command, move", and the (conceptual) mark is set implicitly to the cursor starting position. The advantages of it being:
-you hardly ever need to place marks (although they are still useful for jumping inside, and between files)
-It is a more natural way to think about edits. "Delete from here to there" instead of "From there to here delete".

On the downside are... well... the modes, or - to be more precise - the need to quit the insert mode after typing in some text. On the flip side, it also makes the edits transnational, and hence repeatable (the ubiquitous '.' command).

Some modal editor niceties (in the decreasing order of importance):

1. The ability to move the cursor between interesting places in the text as a single motion is the key feature of modal editors (e.g: "2w" - move 2 words forward, "3Fx" - move 3 occurrences of 'x' backwards, '}' - move to the end of the block, '/Foo' - find "Foo" and move to it, and so on).

2. Many commands should do something, and then drop you into the insert mode (e.g: 'c' - change, 'a' - append, 'I' - insert at the beginning of the line, 'A' - append to the current line, and so on).

3. Text-objects: instead of looking for one interesting location in the text as a command's argument - look for two, around the cursor. Making the editor aware of concepts like function arguments, code blocks, and so on, is an awesome thing. Making it fully AST-aware would be more awesome still.

4. Vim's visual block mode is great. You can not only delete, but also prepend/append to a whole block of text at once. Way better than those pesky keyboard macros, if you can get away with it.