This spring I'm taking a class that does all its programming assignments in MIT Scheme. The last time I had to write Scheme for class, we used PLT Scheme, and thus the DrScheme editor. This time, with that option out, I've decided to do the assignments in Emacs. It is, after all, an editor built out of lisp.

I've used vim for the last five years or so. Some people ask me why this is so—I do go to MIT after all, where Emacs was originally written. The answer to that is simply that Debian got to me first, and at the time at least, the people I knew there used vim. I've heard Emacs is good, but up until this point every time I've tried it I've thrown it down in frustration and then couldn't figure out how to exit the damn thing. This time I was more determined, and made it through the initial hump of being completely useless at the editor.

Here are a few of my observations so far, after mucking around for a couple days over a period of about two weeks:

  • The movement commands seem much more awkward to me. Since I'm using the Colemak keyboard layout, I have backspace bound to Caps-Lock, and it is not available to be a Control key. It may be that I just need some more time to get used to emacs not being modal.

  • It's awkward to switch between split windows using C-x o when I have 4-6 open at a time. I'd like to be able to jump between specific ones directly.

  • C-x 3 will always split the window containing the current buffer evenly in two, which makes it difficult to, for example, create three equally sized vertical windows, side-by-side. Vim defaults to creating evenly sized windows even when you invoke a split twice in a row.

  • The paradigm of being able to have e.g. a scheme buffer or a shell as one of my split windows is nice, though I haven't yet gotten used to having to use emacs special bindings to interact with those buffers rather than what I'd use if I'd just invoked the command from a terminal.

  • I noticed that emacs automatically detects the VCS in my directory. It seems incredibly bizarre to me to interact with my VCS from my editor. Do people actually do this?

  • Emacs's use of the tab character to perform code indentation is interesting. It does solve the problem of code being auto-indented when you don't want it to, which sometimes happens to me in vim, but I wonder if it actually ends up saving keystrokes in the end, since I think not auto-indenting is the exception rather than the rule for me.

  • There seem to be two different methods for completion: "dynamic abbrev" expansion, which I believe is analagous to what I'm used to in vim, and M-TAB (what a horrible keybinding, as most window managers will grab this for window-switching), which seems to be some sort of language-specific completion. I haven't yet seen how this can be harnessed since the symbolic completion seems to be irrelevant for scheme.

  • Emacs's syntax highlighting gets at least one thing right about scheme that vim doesn't: extended comments between #| |# patterns.

  • Bookmarks seem waaay more verbose/awkward than vim's marks. C-x r m NAME huh? I like being able to have a very short pattern to mark and jump back to points in a file, like vim's m/` syntax for marks. Maybe I'll need to bind the bookmark functions to something else.

I'm pretty sure at this point that I'm still trying to bend emacs into being vim with different keybindings instead of figuring out how emacs wants to be used. It's also difficult to compare and contrast between the two editors in that my .vimrc contains years of tweaks and customizations whereas my .emacs started out as a clean slate.

There are two things I'm doing to try to mentally separate emacs from vim, to avoid my fingers' muscle memory from becoming confused:

  1. I'm always using emacs in an X window with dark-on-light text, whereas I always use vim in a terminal with light-on-dark text.
  2. I'm only writing scheme in emacs. No other languages so far.

I'm planning to keep working on these programming assignments in emacs, despite it being annoying slower than I would be in vim. Tips and pointers for improving my experience are welcome.

autoindent on carriage return

I have a huge pile of these in .emacs:

(defun crindents ()
  "Set CR to do newline-and-indent"
  (interactive)
  (local-set-key "\r" 'newline-and-indent))

(add-hook 'perl-mode-hook 'crindents)
(add-hook 'python-mode-hook 'crindents)
(add-hook 'emacs-lisp-mode-hook 'crindents)
;; repeat for every programming language I regularly use

Since you're used to vi(m), you may also want

(global-set-key [(control ?s)] 'isearch-forward-regexp)
(global-set-key [(control ?r)] 'isearch-backward-regexp)

which makes the interactive search prompt take a regular expression instead of a literal string, by default. (I don't remember why I used the extra-verbose key expressions, "\C-s" and "\C-s" would be equivalent.)

Comment by zwol [livejournal.com] Mon 15 Feb 2010 06:57:44 AM UTC
comment 2
Re auto-indentation: there's newline-and-indent, which by default is bound to C-j, but many people bind it to RET in specific programming modes or even globally.
Comment by traubert [myopenid.com] Mon 15 Feb 2010 06:59:13 AM UTC
comment 3
Beaten to the punch :(
Comment by traubert [myopenid.com] Mon 15 Feb 2010 07:00:03 AM UTC
Window movement / split / bookmarks

To move between windows more easily you can use windmove-mode (then shift-<arrow key> will move in the corresponding direction in your windows).

I don't think there's a built-in way to automatically balance windows when you split them, however it is trivial to change the behavior of the corresponding function using advice:

(defadvice split-window-horizontally (after auto-balance activate)
  (balance-windows))

About bookmarks: they're meant to be more of a long-term thing. If you just want to remember places in a buffer and move between them you should use the mark ring, C-SPC C-SPC to mark a spot, C-u C-SPC to move back (C-x C-SPC to allow jumps to other buffers).

Comment by orebokech.com Mon 15 Feb 2010 07:28:59 AM UTC
Control & M-Tab

About the control thing, have you considered remapping your keyboard slightly further? I've been very happy with Ctrl, Meta, Super, [Hyper] moving outwards symmetrically from the spacebar. This has you hitting Ctrl with your thumb.

And, about the M-Tab thing: Firstly, I'd recommend rebinding (I'm using F3 for some reason). Secondly, I'd point out that Escape counts as Meta - hit escape then Tab. Finally, have you tried hippie-expand? It seems to work pretty well for me (although my lisp experience is mostly common lisp, where symbol completion is C-c TAB).

Rupert

Comment by rswarbrick.blogspot.com Mon 15 Feb 2010 09:11:58 AM UTC
Easy window navigation

I use the following code to easily access windows:

 ;;;; window navigation
 (defun jao-first-window ()
   (interactive)
   (select-window (frame-first-window)))

 (defun jao-nth-window (n)
   (if (zerop n) 'jao-first-window
     `(λ ()
        (interactive)
        (jao-first-window)
        (other-window ,n))))

  ;;; keybindings: C-c 1, C-c 2 ...
  (mapc (lambda (n)
          (global-set-key (format "\C-c%s" (1+ n))
                          (jao-nth-window n)))
        '(0 1 2 3 4 5 6 7 8))

With that, C-c 1 goes to the first window, and so on, up to 9.

Comment by jao Mon 15 Feb 2010 09:28:23 AM UTC
About the Control Tab problem...

This mode :

http://nschum.de/src/emacs/window-numbering-mode/

is exactly made for you...

Comment by chmouel [myopenid.com] Mon 15 Feb 2010 03:41:22 PM UTC
default keybindings

Thanks for the wonderfully enlightening post. I have been using vi for most of my editing tasks, which are never very complicated. And I'm also delving into how to get by with only ed, also hoping ed will help me understand C a bit better. About keybindings, I have way to many different Window Managers and related minimalist programs, all installed with their default set(s) of function keys, to be able to even start thinking about making my own keybindings for convenience. I do also dabble with getting Gnome a KDE to work fairly well without always installing everything they offer. But they offer another set of behaviors to the mix, at least from what I've seen so far.... After a fresh install of emacs, while using dwm as windowmanager and a fairly basic 105 us keyboard, (I want to try colemak soon too) I find that CTRL is fairly consistent across the board with emacs, but meta is all over the place. with some bindings working as ALT and some working as Escape and still others working as CTRL ALT while some I can't find at all yet..Plus since you mentioned your only using the X version (to reduce confusion) I noticed there are some functions that work easier with the mouse than with the keyboard. And that can also make the transition a bit more frustrating, at least for me it does... And dwm (the window manager) has an extensive set of predefined keybindings, that I always have to watch and see if keystrokes are intercepted by dwm, as I don't know them all, or even most of them yet. I wanted to share one thing that I found while on a previous emacs journey back in sarge, The emacs psychotherapist. I have not scheduled many appointments with it to check my sanity, but I hope someday it will include a lintian type interface for just that purpose... But don't let it's tag line "Our doctor will help you feel better" lull you into a false sense of security, as it rarely helped to make me feel better, but was interesting none the less....

Unrelated, the minimalist web browser Conkeror also uses emacs type keybindings. Now if there were only a emacs Window Manager, but that might make switching to easy, if that is possible.....

Comment by vvill [myopenid.com] Mon 15 Feb 2010 06:01:13 PM UTC
comment 9
viper-mode. It's annoyingly not-like-vim in a number of ways, but it's got the basic vi nature and the modality, and then you can use the Emacs keybindings for eg. interacting with your VCS around that. Emacs -- just another IDE. (Though a fairly nice one, really.)
Comment by free-dissociation.com Mon 15 Feb 2010 08:55:17 PM UTC
comment 10

heh, I think you're in about the stage of life I was when I kicked the vim habit.

some notes:

first, my absolute number one suggest: USE IDO. TURN IT ON IMMEDIATELY, AND DON'T LOOK BACK. Basically, ido lets you open files and switch buffers by typing any (not necessarily contiguous) subsequence of its name, not just a prefix. It's really great.

re: C-x o: I use window-number.el with the prefix set to C-c, so that "C-c 4" takes me to the 4th window. Also, I bound C-x C-o to other-window as well because I always type that by accident :)

I love shell-dwim for shells; I just hit it and it takes me to one of my shell buffers, and hitting it more times cycles through them; C-u before shell-dwim opens a new shell in the current directory.

I do interact with VCS from my editor but not using the "VC" (per-file autodetected thing)... I tend to use things like magit.el (or git.el), psvn.el, etc, which give a buffer for a given working copy that I can use to run VCS commands. If nothing else, magit lets me mostly ignore the concept of the git index.

I essentially only use dabbrev (M-/). One nice thing about it is that since I typically have a single emacs session open for weeks and rarely kill buffers, almost all the symbols I care about tend to be in some open buffer, so dabbrev works great.

Comment by davidglasser [livejournal.com] Mon 15 Feb 2010 11:25:16 PM UTC
comment 11
FWIW, there's an obscure feature of the scheme syntax highlighting.. "let is_mzscheme=1" will enable "mzscheme-specific" syntax highlighting features, which include #| |#.
Comment by go-team-ari [livejournal.com] Tue 16 Feb 2010 10:35:27 PM UTC
moving between windows

Put (windmove-default-keybindings) in your .emacs file and you can move between windows with shift-arrow keys. See the documentation on that function (C-h f windmove-default-keybindings RET) if you want to use a modifier besides shift.

C-x v v -- yes people actually use their VCS from the editor. Also C-x v d DIR RET to see the changed files in a VCS repository and do different operations on them.

Don't quit emacs just because you're done editing that one file. It is meant to be started and then just worked in. Leave it open and load a file when you need to edit another one.

Comment by hexmode [openweblog.com] Thu 18 Feb 2010 06:16:51 PM UTC