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:

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 01:57:44 AM EST
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 01:59:13 AM EST
comment 3
Beaten to the punch :(
Comment by traubert [myopenid.com] Mon 15 Feb 2010 02:00:03 AM EST
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 02:28:59 AM EST
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 04:11:58 AM EST
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 04:28:23 AM EST
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 10:41:22 AM EST
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 01:01:13 PM EST
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 03:55:17 PM EST
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 06:25:16 PM EST
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 05:35:27 PM EST
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 01:16:51 PM EST