7

How to use mouse to select column in Emacs? How to enable it?

The emacs wiki RectangleMark says,

there is mouse support for rectangle highlighting by dragging the mouse while holding down the shift key. The idea is that this behaves exactly like normal mouse dragging except that the region is treated as a rectangle.

However, when I tried it, it doesn't work. No rectangle column is highlighted.

I don't know if I need to use the next Lisp:rect-mark.el file or not, because when I look at it, it says,

;; If you use both transient-mark-mode and picture-mode, you will
;; probably realize how convenient it would be to be able to highlight
;; the region between point and mark as a rectangle.  Have you ever
;; wished you could see where exactly those other two corners fell
;; before you operated on a rectangle?  If so, then this program is
;; for you.

To "see where exactly those other two corners fell"? Isn't that feature already provided out of box (ref http://www.gnu.org/software/emacs/manual/html_node/emacs/Rectangles.html)? I'm confused. UTSL tells me that it is duplicating what is already provided out of box by Emacs. Do I need the whole rect-mark.el file, or merely the mouse key-binding part only?

UPDATE, for the record, for all my questions, I gather all the answers to here.

5
  • 1
    Is the mouse a strict requirement? I understand why, because the interactive feedback is nice, but Emacs 24.4 has a nice interactive rectangle selector (rectangle-mark-mode) bound to C-x SPC although it is keyboard based.
    – PythonNut
    Commented Jan 10, 2015 at 19:46
  • Yep, keyboard based, that's why I asked for mouse based selection.
    – xpt
    Commented Jan 10, 2015 at 21:33
  • You can certainly do it with a few lines of elisp. Let me cook it up, and I'll answer.
    – PythonNut
    Commented Jan 11, 2015 at 2:06
  • oh, thank you, thank you. I really hope there is a simpler answer, instead of having to install that whole file into my system. Nowadays, when things are not available with package install, we really should put a question mark after it. The Emacs Wiki always feels staled to me, hope that emacs.stackexchange.com become the de-factor live-and-breath doc source for Emacs. Cheers!
    – xpt
    Commented Jan 11, 2015 at 2:22
  • 1
    Indeed, EmacsWiki has a long and glorious history, but you never can tell how many decades old any particular piece of code is without serious investigation. I, personally, hope that all packages move to git (github is nice!). emacs.stackexchange can become the troubleshooting battleground. The Emacs Mailing List can become a forum for extremely technical discussion, and EmacsWiki can continue to catalog various tips and tricks (but not packages).
    – PythonNut
    Commented Jan 11, 2015 at 3:22

1 Answer 1

10

Here is a solution:

(defun mouse-start-rectangle (start-event)
  (interactive "e")
  (deactivate-mark)
  (mouse-set-point start-event)
  (rectangle-mark-mode +1)
  (let ((drag-event))
    (track-mouse
      (while (progn
               (setq drag-event (read-event))
               (mouse-movement-p drag-event))
        (mouse-set-point drag-event)))))

(global-set-key (kbd "S-<down-mouse-1>") #'mouse-start-rectangle)

This works in Emacs 24.4 and later, when rectangle-mark-mode was introduced.

4
  • 1
    Yep, that works like a charm. This solution is really simple and elegant. Thanks!
    – xpt
    Commented Jan 11, 2015 at 3:36
  • 1
    Copy-n-pasted into my .emacs with Aquamacs 3.4 (GNU Emacs 25.3.50.1) - brilliant!
    – kbro
    Commented Sep 12, 2018 at 11:18
  • Finally my Dream has come True !
    – nicolas
    Commented Dec 6, 2018 at 15:42
  • How to make it work without needing to drag. Just to click with alt+shift in another place?
    – rofrol
    Commented Aug 10, 2021 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.