Friday, July 30, 2010

Adding Standard Hotkeys to Emacs v22.1 or Above (copy/paste/undo/save)

If anybody else misses the standard hotkeys (ctrl-c, ctrl-x, ctrl-v, ctrl-z, etc.) while working with Emacs, there is a solution! If your Emacs version is less than 22.1, see this post instead.
Disclaimer: I am not an expert on Emacs, but what I describe below has worked well for me. These modifications will disable a few of Emacs native hotkeys, but this shouldn't cause any problems for you.

First, find your .emacs file. This file controls the settings for Emacs and is probably located in your "home" directory. Once you have found this file, open it up in your favorite text editor (Emacs).

Next, add these lines of text to your .emacs file (preferably at the end of the file or in the area where other "global-set-key" calls are made, which can be found by searching the file). Note that "C-s" translates to "ctrl-s", "C-o" translates to "ctrl-o", etc.:
(cua-mode t) ;;Ctrl-z/x/c/v;;
(global-set-key (kbd "C-s") 'save-buffer)

(global-set-key (kbd "C-a") 'mark-whole-buffer)
(global-set-key (kbd "C-o") 'find-file)
(global-set-key (kbd "C-f") 'isearch-forward)
(global-set-key (kbd "C-g") 'goto-line)
Note that undo may not work as you expect it to, due to the design of undo in emacs.

After this, search your .emacs files for any other global-set-key calls to any of the above keys and comment them out if necessary. Note that \^z is another way of representing (kbd "C-z"). Basically, these lines might replace some of the hotkey bindings we made above. Also, it is also a good idea to comment out any global-unset-key calls to ensure that none of our bindings are disabled. For example, the below lines would disable ctrl-o and ctrl-z:
(global-unset-key (kbd "C-o")) ;disables "ctrl-o"
(global-unset-key "\^z")       ;disables "ctrl-z"

Finally, save your .emacs file and restart Emacs. Enjoy your new hotkeys!

No comments:

Post a Comment