Hacker News new | comments | show | ask | jobs | submit login
Emacs-fu: some handy key bindings (emacs-fu.blogspot.com)
80 points by vamsee 2697 days ago | hide | past | web | favorite | 12 comments

Really useful commands:

  ^X-2: Split window horizontally
  ^X-3: Split window vertically
  ^X-0: Combine window with sibling
  ^X-1: Undo all splits
From my .emacs file:

  ;;; Key bindings
  (global-set-key "\C-xg" 'goto-line)
  (global-set-key "\C-xl" 'what-line)
  (global-set-key [f1] 'goto-line)
  (global-set-key [f2] 'switch-to-buffer)
  (global-set-key [f3] 'shell)
  (global-set-key [f4] 'query-replace)
  (global-set-key [f5] 'compare-windows)
  (global-set-key [f6] 'set-left-margin)
  (global-set-key [f7] 'find-file)
  (global-set-key [S-f7] 'revert-buffer)
  (global-set-key [f8] 'save-buffer)
  (global-set-key [f9] 'auto-fill-mode)
  (global-set-key [f10] 'call-last-kbd-macro)
  (global-set-key [S-f10] 'apply-macro-to-region-lines)
  (global-set-key [f11] 'sort-lines)
  (global-set-key [f12] 'undo)
  (global-set-key [home] 'beginning-of-line)
  (global-set-key [end] 'end-of-line)
  ;;; no insert key on mac keyboard 
  (global-set-key [insert] 'other-window) 
  (global-set-ke y [help] 'other-window)
  (global-set-key [S-help] 'yank)
  (global-set-key [C-home] 'beginning-of-buffer)
  (global-set-key [C-end] 'end-of-buffer)
  (global-set-key [S-next] 'scroll-other-window)
  (global-set-key [S-prior] 'scroll-other-window-down)
Not key bindings, but very nice to have in .emacs:

  ;;; this highlights the marked region
  (transient-mark-mode t)

  ;; don't automatically add new lines when scrolling down at 
  ;; the bottom
  ;;of a buffer.
  (setq next-line-add-newlines nil)

  ;;; Don't truncate lines when windows are split vertically
  (setq-default truncate-partial-width-windows nil)

By default goto-line is mapped to M-g M-g which I find quite convenient already. I mention this because I used to have it bound to C-x g before discovering it was already bound elsewhere, default was good so C-x g got the boot to be available for something else someday.

Here's some of mine (full config @ http://github.com/cycojesus/emacs )

    (global-set-key (kbd "C-z") 'undo)
    (global-set-key [delete] 'delete-char)
    (global-set-key [kp-delete] 'delete-char)
    (global-set-key (kbd "C-c b") 'browse-url)
    (global-set-key (kbd "C-c c") 'calendar)
    (global-set-key (kbd "C-c h") 'calc) ;; 'h' for 'h'P-48
    (global-set-key (kbd "C-c l") 'linum-mode)
    (global-set-key (kbd "C-c w") 'whitespace-mode)
    (global-set-key [(control mouse-4)] (lambda () (interactive)
                                      (text-scale-increase 1)))
    (global-set-key [(control mouse-5)] (lambda () (interactive)
                                      (text-scale-decrease 1)))
    (define-key global-map (kbd "RET") 'newline-and-indent)
    (global-set-key (kbd "C-;") 'shrink-window)
    (global-set-key (kbd "C-.") 'enlarge-window)
    (global-set-key (kbd "M-/") 'hippie-expand)

    ;; scrollwheel on the modeline to navigate buffers
    (global-set-key [mode-line mouse-4] 'next-buffer)
    (global-set-key [mode-line mouse-5] 'previous-buffer)

In my Emacs 23 default configuration, the functionality for editing code and comments side-by-side in separate buffers is on the F2 key, whereas replace-regexp doesn't have a key of its own. Guess which one I use more.

Sometimes I wish for a keybinding revolution...

Yes, I know that keys can be remapped, and have done so on my machines, but sensible defaults are still a good thing to have, if only to welcome new users.

You might be interested in Xah Lee's ErgoEmacs project. Here's a page specific to its keybindings: http://xahlee.org/emacs/ergonomic_emacs_keybinding.html

Does anyone know better ways to evaluate and execute Clojure expressions in Emacs?

I find key combinations like these clunky:

C-x C-e


Those kinds of shortcuts of are very typical of Emacs defaults, but you can rebind them. Here's how.

First, find the name of the function you want to rebind. Press C-h k (describe-key), and then the normal shortcut for the command you want to rebind. It should open a help buffer beginning with e.g. "C-x C-e runs the command something-hyphenated". That's your command name. (You can get a list of all commands in clojure's mode by pressing C-h m, which might be quicker if you're redefining several.)

Then, set up a mode-specific hook (something that runs when you edit a buffer in that mode). Add this to your Emacs config:

    (add-hook 'clojure-mode-hook 'my-clojure-mode-hook)
(or whatever the clojure mode is called, I haven't used clojure). Finally, define your mode hook and add custom local keybindings with local-set-key. It should look like this:

    (defun my-clojure-mode-hook ()
      (local-set-key (kbd "better-shortcut") 'name-of-command)
      (local-set-key (kbd "better-shortcut-2") 'name-of-second-command))
Where (kbd "better-shortcut") is in the usual Emacs keyboard shortcut notation, like (kbd "C-M-e").* To figure out what this is for a key, you can press C-h k again, followed by your shortcut. It will say "X is unbound", "X is assemble-delicious-sandwich", or whatever. The X part is what Emacs calls that key combination.

* You don't strictly need the (kbd "string") form, but it's probably the simplest form to use - you can press C-x C-e after the (kbd _) form to verify that it recognizes the key description.

Anything else you want to run when you start editing clojure code can go in the mode hook, too. Let me know if you have any questions. While I haven't done much with clojure, I know customizing Emacs pretty well, and adding keybindings like this works for any mode. You just need to know what the mode is called.

Checkout hs-minor-mode as well. Great for sifting through code (or xml, html etc)

M-1 M-q: justifies text, adding spaces between words.

M-x artist-mode

Forgot the most important one:

C-X C-c; vim

I believe you mean C-x C-c vim [RET]

Though personally I've never found it that useful.

Vim also runs in M-x term. No need to quit Emacs for that.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact