Thursday, July 15, 2010

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

If anybody else misses the standard hotkeys (ctrl-c, ctrl-v, ctrl-z, etc.) while working with Emacs, there is a solution! If your Emacs version is greater 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-c" translates to "ctrl-c", "C-o" translates to "ctrl-o", etc.:
(global-set-key (kbd "C-c") 'copy-region-as-kill)
(global-set-key (kbd "C-w") 'kill-region)
(global-set-key (kbd "C-v") 'yank)
(global-set-key (kbd "C-z") 'undo)

(global-set-key (kbd "C-a") 'mark-whole-buffer)
(global-set-key (kbd "C-s") 'save-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)

Unfortunately, I have not yet found a way to properly bind ctrl-x to "cut", so please use ctrl-w instead. The reason for this is likely because ctrl-x is, by default, a prefix command in emacs. For the same reason, ctrl-c causes some problems. However, if your Emacs version is above 22.1, these keys will work correctly if you follow this guide. Also note that undo may not work as you intend, 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