Newbie Problems Solved
A close friend of mine has been a vim, and Dr. Racket user for as long as I can remember, so it was simply shocking to me to receive an email saying, “Hey, so I’m going whole hog into Emacs.” The shocking part wasn’t that he said, “whole hog,” but rather “Emacs.” (He’s a vegetarian)
Since then, an epic thread has formed in which he presents a problem and I reply with a few ways to fix it, or work arounds, or in the case of his problems with replace-string, elisp.
The interactive command replace-string is sort of funny to a newcomer. Most editors, when invoking the search and replace functionality operate on the entire file. replace-string, however, does not. It insists on going forward (or backwards with a prefix argument), but unless you explicitly go to the beginning of the buffer (and thereby losing your point position [yes, you are free to fix that by running this sequence: C-<spc> C-<spc> M-< M-x replace-string C-u C-<spc>]), you’re out of luck if you want the common functionality. That is unless you know elisp. A simple function later got him exactly where he wanted to be, though I’m sure he’ll change his mind after he uses the real replace-string for a while.
(defun replace-string-whole-buffer () "Whole buffer version of replace-string" (interactive) (save-excursion (beginning-of-buffer) (call-interactively 'replace-string)))
Install emacs color theme by buffer (or major-mode)
Have you ever wanted some of your emacs buffers to use a different color theme than the rest?, perhaps you want to set a color theme per major mode. Well, now that recent Emacs includes Face Remapping you can tell Emacs that a particular face should look different on a particular buffer, that is, the face becomes buffer-local.
Taking advantage of face-remapping, the color-theme-buffer-local project allows you to install a theme on per-buffer basis. Not only themes made for color-theme.el but also the all new emacs24 deftheme based ones.
color-theme-buffer-local is available for installation from Marmalade repo as two packages, one for color-theme based themes:
M-x package-install color-theme-buffer-local and one for emacs24 themes:
M-x package-install load-theme-buffer-local
For installing a color-theme.el theme say robin-hood on current buffer use:
(color-theme-buffer-local 'color-theme-robin-hood (current-buffer))
For installing emacs24 based theme, use:
(load-theme-buffer-local 'misterioso (current-buffer))
This way you can create a hook for your favorite major-mode and have it show using a color theme of your like:
(add-hook 'java-mode (lambda nil (color-theme-buffer-local 'color-theme-robin-hood (current-buffer))))
emacs goodie: auto-wrap a comment block
I always forget this emacs trick: M-q auto-wraps the current text block to the fill column (default is usually column 70). This is wondrous for long comments:
””“Sometimes I like to write a nice long block of text explaining what a function does. You can wrap it manually, but then it gets all screwy when you need to add something to the middle!”“”
M-x set-fill-column 20
””“Sometimes I like
to write a nice long
block of text
explaining what a
function does. You
can wrap it
manually, but then
it gets all screwy
when you need to add
something to the