ok, I'll admit it - this writeup is only half for the public good. (the other half is so that I have a repository of the emacs lisp functions that I have either written or stolen1, outside of my .emacs file.) All of the functions should be considered to be in the public domain...
This, right here, is just about my favorite function of all time - I have no idea how I lived without it!
(defun create-scratch-buffer nil
"create a new scratch buffer to work in. (could be *scratch* - *scratchX*)"
(interactive)
(let ((n 0)
bufname)
(while (progn
(setq bufname (concat "*scratch"
(if (= n 0) "" (int-to-string n))
"*"))
(setq n (1+ n))
(get-buffer bufname)))
(switch-to-buffer (get-buffer-create bufname))
(if (= n 1) (lisp-interaction-mode)) ; 1, because n was incremented
))
A horribly basic function; I have no idea why it isn't part of emacs to begin with. If you're a vi user, it's like 'dd' - I actually wrote the function because a friend of mine asked me "why doesn't emacs have 'dd'?" Well, now it does:
(defun kill-whole-line nil
"kills the entire line on which the cursor is located, and places the
cursor as close to its previous position as possible."
(interactive)
(progn
(let ((y (current-column))
(a (progn (beginning-of-line) (point)))
(b (progn (forward-line 1) (point))))
(kill-region a b)
(move-to-column y))))
Is it anal-retentive of me to be offended by whitespace at the end of lines? Well, if it is, too bad. It's a good candidate for insertion into write-file-hooks, or local-write-file-hooks from a particular mode-hook, for the more-sane. (If placed in the former, ALL files will be saved without trailing whitespace; if placed in the latter, just those for a particular mode will be altered. Try putting it in your c-mode-common-hook, and see how much smaller your files are...)
(defun strip-trailing-whitespace ()
"strips whitespace from the end of all the lines in the buffer;
equivalent to (`replace-regexp' \" \\t+$\" \"\")"
(interactive)
(save-excursion
(goto-char (point-min))
(while (re-search-forward " \t+$" nil t)
(replace-match "" nil t))
nil)
)
Nowadays (Emacs 21 and later), delete-trailing-whitespace does the same thing, more robustly.
This is like Unix's 'uniq' program, but without the hassle of leaving the comfort of emacs. (Plus, you can do it on a region, instead of an entire file.) You might want to call sort-lines before calling this; otherwise, not much is uniqued away.
(defun uniquify-region ()
"remove duplicate adjacent lines in the given region"
(interactive)
(narrow-to-region (region-beginning) (region-end))
(beginning-of-buffer)
(while (re-search-forward "\\(.*\n\\)\\1+" nil t)
(replace-match "\\1" nil nil))
(widen)
nil
)
If you have the "uniq" command installed on your system, you might want to use this version instead:
(defun uniquify-region ()
"remove duplicate adjacent lines in the given region"
(interactive)
(shell-command-on-region (region-beginning) (region-end) "uniq" t)
)
It has been pointed out to me that you can do the same thing with C-u M-| sort RET but that requires you to have 'sort' installed, which you should.
Alright, that's it, at least for now.. GOOD NIGHT EVERYBODY! (check back later, to see if any new wonders have been added...)
1 It actually turns out that I wrote all of these myself... originally I was going to include some by other people, but when I looked through my .emacs, there weren't very many of them. oh well..