[OS X Emacs] Select all: how to *not* jump to the beginning of the file + how to not change the selection?

Jack Repenning jrepenning at collab.net
Tue Sep 20 16:37:31 EDT 2011

On Sep 20, 2011, at 2:30 AM, Hofert Jan Marius wrote:

> If I either hit Command-A or C-x h to select all text in a buffer, the cursor jumps to the beginning of a file. This is quite annoying when you want to, e.g., indent the whole file (or apply some other function) and then want to continue to work on where you were before. How can I select the whole buffer content without jumping the the first line of the buffer?

You can't do that: Emacs' idea of "the selection" is "the stuff between the cursor (called point) and another magic location (called mark)." So by (Emacs') definition, the cursor is *always* at one end or the other of any selection.

What you might be able to do is return to where you came from. At the Elisp programming level, Emacs provides an assortment of features for this, such as "save-excursion" and "set-window-configuration." I don't think there are any direct bindings to these functions (can anyone prove me wrong?), but I've used the below code, in my Preferences.el or equivalent for ages. This makes C-u C-M-z save the current window state, and C-M-z restore the previous state. So you can for instance do some sequence like this:

  C-u M-C-z (save state)
  M-h (mark paragraph)
  M-q (indent region)
  C-M-z (restore cursor to where you were)

I admit that C-u C-M-z is a pretty complicated keystroke. A problem with Emacs is that all the good keystrokes are already taken! But you can change the "global-set-key" below to something you like better.

Jack Repenning
CollabNet Cloud Services
CollabNet, Inc.
8000 Marina Boulevard, Suite 600
Brisbane, California 94005
office: +1 650.228.2562
twitter: http://twitter.com/jrep

(global-set-key [(C M z)]  'pp-window-configuration)

(defvar pp-window-configuration ()
  "Holds the stack of former window configurations.  Form: (list (list window-configuration point-in-current)            ...)")

(defun pp-window-configuration (arg &optional thunk)
  "Save current window config (with ARG, restore
; with ARG>=16,flush)
; Optional THUNK on save executed after eventual restore."
  (interactive "p")
   ((and (numberp arg) (>= arg 16))
    (setq pp-window-configuration nil)
    (message "Window configuration stack cleared."))
   ((or (not (numberp arg)) (and (numberp arg) (> arg 1)))
    ;; Push current window configuration
    (setq pp-window-configuration
	  (append (list
		   (list (current-window-configuration)
    (message "Window configuration %d saved."
	     (length pp-window-configuration)))
    ;; Pop a prior window configuration (if any)
    (if (= 0 (length pp-window-configuration))
	(message "Window configuration stack is empty.")
      (let* ((ppwc (car pp-window-configuration))
	     (wc (nth 0 ppwc))
	     (p (nth 1 ppwc))
	     (thunk (nth 2 ppwc)))
	(setq pp-window-configuration (cdr pp-window-configuration))
	(set-window-configuration wc)
	(goto-char p)
	(eval thunk))
      (message "Window configuration %d restored"
	       (length pp-window-configuration))))))

More information about the MacOSX-Emacs mailing list