How select a rectangle in emacs

25.7k views Asked by At

In Notepad++, you can press Alt-LeftMouseClick-Drag to select a rectangular region.

How do I do this in emacs (windows) ?

I tried the methods in http://www.emacswiki.org/emacs/RectangleCommands but my selection happens as a regular region instead of rectangular shape:

https://i.stack.imgur.com/tBMBN.png

I also tried enabling/disabling cua mode.

9

There are 9 answers

1
legoscia On BEST ANSWER

In Emacs, whether a command affects a continuous piece of text or a rectangle depends on the command, not on what the selection looks like.

I'm not aware of any way to make the selection appear as a rectangle on the screen (but see this answer), but if you use any of the rectangle commands listed in the page you linked to, such as C-x r k or C-x r t, you'll see that they operate on the rectangle defined by the beginning and the end of the selection, despite the fact that the selection looks like a regular region.

1
lily On

You use rectangle commands by placing the mark at one corner of the rectangle and point at the opposite corner. Even though the region will be displayed as normal, the rectangle commands will act on the rectangle delimited by point and mark. CUA mode doesn't affect this.

screenshot of emacs with mark, point, and rectangle highlighted

You just have to use your imagination :)

2
dgtized On

In Emacs 24.4 and greater, C-x SPC is bound to rectangle-mark-mode which selects a rectangle visually.

http://emacsredux.com/blog/2014/01/01/a-peek-at-emacs-24-dot-4-rectangular-selection/ describes this in greater detail.

3
Drew On

In addition to what others have mentioned, including about highlighting shown as a rectangle by rectangle-mark-mode and cua-rectangle-mark-mode, library Mode-Line Position (modeline-posn.el) provides the following feature for use with rectangle commands: It shows the length and width of the rectangle in the mode line, as part of size-indication-mode.

Normally, size-indication-mode shows only the buffer size and the current relative position in the buffer. With library modeline-posn.el what you see is different when the region is active:

  • For a rectangle command that reads input, you see the number of rectangle rows and columns (e.g. 21 rows, 16 cols)

  • Otherwise, the region size.

You can customize how this information appears (option modelinepos-style). By default, the active region size is shown as the number of characters and the number of lines in the region (e.g. 473 ch, 3 l).

The face used for the mode-line indication of an active region (including rectangle) is face region, so it looks the same as the region.

An additional feature draws your attention further to the mode-line region indication when a command that acts on the active region or changes its behavior when the region is active reads input. This just uses a different face, which by default is face region but with a red overline and underline.

Finally, Boolean option modelinepos-empty-region-flag determines whether an empty active region is indicated in the mode line, to attract your attention (you might not otherwise notice that you are acting on an empty region). E.g., you see (highlighted using face region): 0 ch, 0 l.

1
Dan On

One more fun one to add to the list.

If you're using evil-mode (ie, the Vim emulation layer), you can use evil-visual-block (default binding of C-v) to select a rectangle visually.

0
jpkotta On

If you want to see the rectangular selection, use CUA rectangles instead. Enable it with

(cua-selection-mode t)

Then use M-RET to start the selection, move the cursor to change it (you can also hit RET to move the cursor to different corners), C-? to get a list of commands to operate on the rectangle. M-RET with the selection active cancels the selection, as does C-g.

CUA selection mode does not enable the rest of CUA, so C-x, C-c, C-v, etc. will not change.

0
Storm Blast On

You can do the same thing with the mouse if you so wish. While not as specific as doing it via keyboard where you can select characters that don't exist following the end of the line for example it will fulfill most use cases.

(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 "M-<down-mouse-1>") #'mouse-start-rectangle)

Found this here: https://emacs.stackexchange.com/a/7261

0
Micah Elliott On

Most rectangle commands are accessed via the C-x r prefix (shared with the "register" commands). See the Rectangles manual section here for a description of all the special kill, yank, delete, number, replace, etc. commands, which you'll need to do anything useful with your rectangles.

The only non-prefixed rectangle command is the initiator C-x SPC (rectangle-mark-mode), but that's easy enough to remember, being a lot like the very common C-SPC (set-mark-command).

It looks like in recent Emacs versions, the selected region is working well to show exactly what's selected.


(I recommend helm-descbinds or guide-key to complete your C-x r command prefixes, as well as any others.)

0
AudioBubble On
  1. Enter rectangle mark mode with C-x SPC

  2. Shrink or grow region by (This is going to select a rectangle):

    2.1. Do C-n or C-p to expand the cursor to the next or previous lines respectively.

    2.2. You can now play with text selection by doing C-f, C-b, M-f, M-b, etc.

  3. This selected rectangle region can be used to perform some actions like

    • kill : C-x r k
    • delete: C-x r d
    • yank: C-x r y (paste last killed rectangle at cursor position)