Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

Faster pop-to-mark command

Today’s tip is one I learned from Magnar. A lot of Emacsers don’t know this, but most commands that move point large distances (like isearch or end-of-buffer) push the old position to the mark-ring. The advantage is that you can easily jump back through this history of positions by hitting C-u C-SPC.

This is a hugely convenient take-me-back-to-that-last-place command. The only problem is that sometimes the ring gets filled with repeated entries, so you find yourself hitting C-u C-SPC 2 to 4 times in the same place. Of course, this is Emacs, so all it takes to solve our problem is one simple advice.

;; When popping the mark, continue popping until the cursor
;; actually moves
(defadvice pop-to-mark-command
    (around ensure-new-position activate)
  (let ((p (point)))
    (dotimes (i 10)
      (when (= p (point))
        ad-do-it))))

Finally, a simple setq ensures we can quickly pop the mark several times by typing C-u C-SPC C-SPC, instead of having to type C-u C-SPC C-u C-SPC.

(setq set-mark-command-repeat-pop t)

Update 20 Jan 2016

Kaushal Modi provides us with how this advice would look using the new advice-add interface.

(defun modi/multi-pop-to-mark (orig-fun &rest args)
  "Call ORIG-FUN until the cursor moves.
Try the repeated popping up to 10 times."
  (let ((p (point)))
    (dotimes (i 10)
      (when (= p (point))
        (apply orig-fun args)))))
(advice-add 'pop-to-mark-command :around
            #'modi/multi-pop-to-mark)
comments powered by Disqus