Wednesday, March 28, 2007

Editor Brain Modes

I wound up tying in a long comment to "Coding Horror's" blog... so I thought I would post it here too since this is my official place for programmer musings and fun discussions.


I am a big fan of using the keyboard as much as possible. I have found that over the years I have formed "modes" in my brain for the different types of editors I use:

  • Mode # 1 - "Everyman" Editing:

    This is my brain mode for when I am in a standard windows text box, a notepad window, or editing something on someone else's editor and I want to have the best chance of things just "working". No fancy, feelin' cool editing tricks here. Just get the job done and use the experience to remind you of how much nicer the other mode's are.. (This message typed in Mode # 1).

  • Mode # 2 - "Faux Emacs":

    Ah, old school emacs. This is my favorite, because if you are trained right you can keep both hands on the keyboard for just about everything. No lifting your hand up to search for arrow keys or the home/end. Just keep those palms flat on the keyboard ergo-bench and type away. Emacs uses the CTRL and ALT keys as modifiers for all the basic editing commands, and has no qualms about, say, using Control-F to mean "move cursor forward" and Control-d to mean "delete character". There are some interesting conflicts that arise when you bring the default emacs commands to windows. The one that really freaks people out is when I have Page-Down mapped to Control-V. Heaven help the poor soul who tries to use my Visual Studio while its emacs profile is still engaged. They go to paste a bit of text and the *entire screen changes!* Heh heh... if I had a dime for every time a coworker yelped in dismay...

    I do have a somewhat hybrid set up for those apps that I can configure to use emacs key bindings - things like Shift + Control + F = Extend selection forward one char, Shift + Control + A = Extend selection to start of line, and Control + U = Page Up (took that from vi). I am actually more in tune with this "bastardized" hybrid than true emacs, but drop me in real emacs editor and I only start to choke when I have to remember those funky double-sequence commands like Control-X Control-S to save and the like. Or heaven help me if I need to remember how to manipulate kill regions or run a Meta-X search-for-string...
  • Mode # 3: "To The Pain" (g)VI(m) -

    Long ago I lived in worlds where sometimes emacs was not available. I bit the bullet and learned VI. It is just as powerful as emacs in its ability to let you keep those palms flat on the keyboard, but it does it in a different way. VI is a "mode-based" editor - meaning that you press a key ('i') and then everything you type until you hit the ESCAPE key is treated as text input. If you are not in 'insert' mode, then you're in 'command' mode, which is where you can move around ('ijkl') and enter "colon" commands like ':w ~/myfile.txt', ':wq' (write file then quit), or ':q!' (quit without saving, dammit).

    After literally years, I eventually reached the point where I could effortlessly switch into "vi" mode and do quie well. These days, I am even able to do basic search and replace without having to look it up. Don't believe me? Check this out:


    Looks like Sendmail control codes, or Perl gone right, eh? No sir, it is my version of "Replace every occurence of the word 'foobar' with 'Snickers' in the file. My mind tells me that this command is essentially broken down like this:

    ':1,$s/foobar' // search for the string foobar anywhere from
    // the 1st line to the end of file '$'.

    // If you find a foobar, replace it with Snickers please...

    'g' // Oh, and do it globally for all matches you find.
    // (Snickers really satisfies...).

    It turns out that depending on which vi you are using you may not need the 'g' part at all.. and frankly if you use me as your canonical reference for how to break down the vi command above you should have your brain checked for lesions.

    Well, anyway, that's my brain on editors after well over ten years of programming in Unix, Windows, and other seedy locales. Thank you for listening :-)


Martin Stone said...

Hi. I like to keep my hands on the keyboard, too, but the Windows key bindings are a bit too ingrained now, so I wrote a utility that lets you navigate with the home keys in all Windows programs. In other words, it lets you use vi-style cursor keys in your "mode # 1", without being mode-based like vi. You might like it!

Arnoori said...

Hi, I would like to use the emacs style key bindings in visual studio but I am not able to get a full list of the key bindings with emacs profile in vs. Can some body help me how to find the key bindings that are there and how to add new key bindings in the emacs profile.

thanks in advance.