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..
I'd like to add a function of my own that I find useful. Often there'll be a buffer where I want to take notes and I want to keep coming back to it from wherever I am. To this end I present:

(defun switch-to-buffer-and-back (buf)
  "Toggle between given buffer and the current buffer."
  (interactive)
  (if (equal (buffer-name) buf)
      (bury-buffer)
    (switch-to-buffer buf 'NORECORD)))

(global-set-key [(control c) ?l]
             (lambda () (interactive) (switch-to-buffer-and-back "BUFFER-NAME-HERE")))


Of course, you can bind the function to any key combination you want (in the example above it's bound to "Control-C l").
Extra tip: if you're running Emacs from within Windows, and you have one of those keyboards with the windows keys next to the Alt keys, you can rebind these keys so they act as a new modifier in Emacs (like Control or Alt) using the following code:

(setq w32-rwindow-modifier 'hyper)
(setq w32-pass-rwindow-to-system nil)


Once you do this, you can use the right-windows key together with any other key to unleash a whole new world of customised key bindings, for example:

(global-set-key [(hyper s)] 
            (lambda () (interactive) (switch-to-buffer-and-back "BUFFER-NAME-HERE")))


I'll include some more Elisp functions here sometime soon.

Dear God I Love Emacs.

Log in or registerto write something here or to contact authors.