Wednesday, December 30, 2009

Visual Studio Emacs Keybindings: Getting Indent to Work Properly

I'm a hard-core Emacs user, but sometimes I have to use Visual Studio. A friend of mine just pointed me to a brilliant blog post that describes how to set up Emacs key-bindings in Visual Studio. (Briefly: Go to Tools Options Environment Keyboard, and select Emacs from the list of key mapping schemes.)

The only problem I found with the procedure: when you hit enter to move to the next line, the cursor goes back to Column 1, and unlike in Emacs "Tab" does not automatically move you to the proper column to continue the block of code you're writing.

I found a way to fix this:

  1. Go to Tools Options Environment Keyboard
  2. In the dialog box labeled, "Show commands containing:", type "breakline". Wait a moment for the search to complete.
  3. Select Edit.EmacsBreakLineIndent.
  4. In the "Use new shortcut in:" menu, select "Text Editor". This tells Visual Studio you want the binding we're about to add to be active only while in the text editor.
  5. Click in the "Press shortcut keys" box, and hit Enter. The word "Enter" should appear.
  6. Click "Assign."
You'll get a warning that Enter is already assigned to Edit.EmacsBreakLine. You're reassigning it to EmacsBreakLineIndent. Now, when you hit enter, the cursor is automatically positioned at the correct column to continue typing a code block.

To switch back to the old behavior, follow the same procedure as above, except select Edit.EmacsBreakLine instead.

No comments:

Post a Comment