EmacsNiftyTricks

“I’ve used emacs for many years now, but have never reached its maximum potential.” – Anon
“I think this is the motto of every emacs user.” – LathI

Some programmers are enormously productive using Emacs. Their hands seem to quickly run over the keyboard accomplishing things in minutes that it takes others hours to accomplish. They must know many Emacs tricks which allow them to work rapidly. If asked, they are hesitant to show their tricks. Understandably so, it would probably take many hours for them to show me all their tricks.

This web page describes all their little tricks for using Emacs effectively.

Some were mentioned on the EmacsChannel.

See also EmacsCrashTips from the EmacsCrashCourse.

Nifty tricks for Dired are on their own page: DiredPower.

Editing

Searching and Replacing

Copy, Paste, and Select

Buffers and Files

          (set-register ?e '(file . "~/.emacs"))
          (set-register ?i '(file . "~/org/ideas.org"))

Auto modes

Outlines and Overviews

            (global-set-key "\C-x$" 'set-selective-display-dlw)
            (defun set-selective-display-dlw (&optional level)
              "Fold text indented more than the cursor.
            If level is set, set the indent level to level.
            0 displays the entire buffer."
              (interactive "P")
              (set-selective-display (or level (current-column))))

External Tools

      (defadvice save-buffers-kill-emacs (around no-query-kill-emacs activate)
        "Prevent annoying \"Active processes exist\" query when you quit Emacs."
        (flet ((process-list ())) ad-do-it))

Audiovisual settings: bells and whistles

    ;; quiet, please! No dinging!
    (setq visible-bell nil)
    (setq ring-bell-function `(lambda ()
                            (set-face-background 'default "DodgerBlue")
                            (set-face-background 'default "black")))
    (eval-after-load "startup" '(fset 'display-startup-echo-area-message (lambda ())))

to get rid of it everywhere.

    Emacs*Xlw''''''Menu*fontSet: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*
    Emacs*Xlw''''''Menu*foreground: black
    Emacs*Xlw''''''Menu*shadowThickness: 1

User Interface

        (setq hcz-set-cursor-color-color "")
        (setq hcz-set-cursor-color-buffer "")
        (defun hcz-set-cursor-color-according-to-mode ()
          "change cursor color according to some minor modes."
          ;; set-cursor-color is somewhat costly, so we only call it when needed:
          (let ((color
                 (if buffer-read-only "black"
                   (if overwrite-mode "red"
                     "blue"))))
            (unless (and
                     (string= color hcz-set-cursor-color-color)
                     (string= (buffer-name) hcz-set-cursor-color-buffer))
              (set-cursor-color (setq hcz-set-cursor-color-color color))
              (setq hcz-set-cursor-color-buffer (buffer-name)))))
        (add-hook 'post-command-hook 'hcz-set-cursor-color-according-to-mode)

Mail

        (setq mail-user-agent 'message-user-agent)
        (setq mail-user-agent 'gnus-user-agent) 

See also GnusNiftyTricks

Programming

      (eval-after-load "dabbrev" '(defalias 'dabbrev-expand 'hippie-expand))
       (global-set-key [(shift end)]
          '(lambda () (interactive) (other-window -1)))
‘untabify’ acts only on the region, so that is typically not a problem. The better guideline is to not use it on a region that contains TABs other than as insignificant whitespace. IOW, it is not a proble of badly written code but of knowing what the code is/does before you act on it. – DrewAdams
  • Make the region lowercase: ‘downcase-region’ – JonAquino?
  • Align lines in region on a common string with ‘M-x align-regexp’
  • Delete all space around point with M-\
  • RectangleCommands: kill, yank, insert, delete rectangles and more!

Miscellaneous

(lexical-let ((beg -1)
	      (end -1)
	      (prev-mid -1))

(defun backward-binary ()
   (interactive)
   (if (/= prev-mid (point)) 
       (setq beg -1 end -1)
     (setq end prev-mid))
   (if (< beg 0) (setq beg (line-beginning-position)
		       end (point)))
   (setq prev-mid (/ (+ beg end) 2))
   (goto-char prev-mid))

(defun forward-binary ()
   (interactive)
   (if (/= prev-mid (point))
       (setq beg -1 end -1)
     (setq beg prev-mid))
   (if (< end 0) (setq beg (point)
		       end (line-end-position)))
   (setq prev-mid (/ (+ beg end ) 2))
   (goto-char prev-mid))
)

(global-set-key [(meta j)] 'backward-binary)
(global-set-key [(meta k)] 'forward-binary)

Navigating like this feels a bit odd at first, but by now I badly miss the feature in my terminals.

Further ideas for nifty tricks

See Also

See also Wiki:GreatEmacsFeatures, DayInTheLife


CategoryDotEmacs CategoryDocumentation. CategoryEditing CategoryHelp