Emacs Editor Keybindings in the RStudio IDE

Follow

You can enable Emacs keybindings within the RStudio IDE from the Code section of the Global Options dialog:

A base set of Emacs keybindings for navigation and selection are available, including:

  • C-p, C-n, C-b and C-f to move the cursor up, down left and right by characters

  • M-b, M-f to move left and right by words

  • C-a, C-e to navigate to the start, or end, of line;

  • C-k to ‘kill’ to end of line, and C-y to ‘yank’ the last kill,

  • C-s, C-r to initiate an Emacs-style incremental search (forward / reverse),

  • C-Space to set/unset mark, and C-w to kill the marked region.

There are some additional keybindings that Emacs Speaks Statistics (ESS) users might find familiar:

  • C-c C-v displays help for the object under the cursor,

  • C-c C-n evaluates the current line / selection,

  • C-x b allows you to visit another file,

  • M-C-a moves the cursor to the beginning of the current function,

  • M-C-e moves to the end of the current function,

  • C-c C-f evaluates the current function.

We've also introduced a number of keybindings that allow you to interact with the IDE as you might normally do in Emacs:

  • C-x C-n to create a new document,

  • C-x C-f to find / open an existing document,

  • C-x C-s to save the current document,

  • C-x k to close the current file.

Comments

  • Avatar
    Clark Richards

    This is a happy day! Thank you for adding this!

  • Avatar
    Clark Richards

    Thanks again for adding emacs keybindings. A couple things I've noticed:

    • the "undo" keybinding is only bound to C-x u. My fingers know one of the other aliases, C-S-_ (I like it because it doesn't require consecutive key hits), but when I tried modifying the shortcut, it doesn't work. In fact, after changing the "Undo" shortcut to C-S-_ and then closing and opening the preference pane, my custom shortcut gets changed to Ctrl+Shift+Insert

    • I've noticed an inconsistency with killing and yanking lines, i.e. doing C-k followed by C-y. In Emacs, consecutive applications of C-k will keep adding to the kill ring (provided no other command interrupts the killing), so that C-y will yank them all back together. For example, I might use C-k to kill 3 consecutive lines, and then yank them all back with a single C-y. However, in RStudio, only the last killed line gets saved to the kill ring.

  • Avatar
    Kevin Ushey

    Hi Clark,

    Thanks for the feedback! I'll see what's going on re: the remapping from _ to Insert.

    The kill + yank behaviour is also currently suboptimal; the 'kill ring' is not yet fully implemented. We mostly borrow this functionality from the Emacs mode that's bundled with the Ace editor (http://ace.c9.io/#nav=about), but I'll see if we can either patch RStudio to implement this or else get changes adopted upstream in Ace as well.

    Thanks,
    Kevin

  • Avatar
    Wenjian Yang
    This is very helpful. Thanks!! However, on Windows OS, the Alt key does not work as the Meta key. For example, "M-f" (Alt-f) will open the "File" menu instead of moving forward by word. Is there anyway to use Alt as Meta in Windows? Using 0.99.837. Thanks.
  • Avatar
    William Murrah

    I love the emacs keybindings, but C-f opens the find pane inside the editor instead of moving the point forward. I there a way to change that?

  • Avatar
    Kevin Ushey

    Hi William,

    Thanks for pointing this out -- I've logged an issue internally and we'll try to get this fixed soon.

    Best,
    Kevin

  • Avatar
    Jonathan Steinhart

    THANK YOU THANK YOU THANK YOU! :D

    RStudio was already such a big leap up in productivity for me from Emacs/ESS that I have been using it almost exclusively for some time; but this was the one gripe I still had (old habits dying hard and all). Thanks so much for actually implementing this!

  • Avatar
    Jared Studyvin

    C-c C-n does NOT evaluate the current line/section, in fact C-c does not seem to be working at all. I'm using the current version of rStudio. C-r does evaluate the line but that should be search in reverse for emacs keybindings.

  • Avatar
    Kevin Ushey

    Hi Jared,

    Thanks for reporting the issue -- we'll investigate what's going on.

    Best,
    Kevin

  • Avatar
    ethan xu

    THANK YOU! I remembered asking for emacs key bindings in 2014 JSM at RStudio booth. It's finally here:)

    Will full custom key settings happen in the future?

  • Avatar
    Jonathan Gilligan

    On Windows, what key acts as "meta"? Neither "alt" nor "esc" seem to work as meta.

  • Avatar
    Elpo Ghash

    Ubuntu 16.10 "M-b, M-f to move left and right by words" don't work, as they open the Build and File menus. Would it be possible to fix this?