18

Here's what Vim's documentation has to say about it:

Virtual editing means that the cursor can be positioned where there is no actual character. This can be halfway into a tab or beyond the end of the line. Useful for selecting a rectangle in Visual mode and editing a table.

I'd preferably like to see this implemented as a minor mode or a built in setting.

Malabarba
  • 22,878
  • 6
  • 78
  • 163
nanny
  • 5,704
  • 18
  • 38
  • Do you want this for selecting a rectangle? If that's the case it's the default in current development version. – Malabarba Nov 17 '14 at 17:58
  • No, I'm thinking more like yanking/replacing rectangles. I do use the rectangle mark mode and while it is a nice feature it's not what I'm looking for. – nanny Nov 17 '14 at 18:01
  • Indeed, for yanking/replacing rectangles, it'd be a natural companion to the existing functionality in rectangle-mark-mode. Not sure what the UI should be, tho. – Stefan Nov 17 '14 at 18:16
  • Thanks for the reply, Stefan. This feature would be most useful when dealing with rectangles, so perhaps while in rectangle mark mode a command could toggle a virtual edit type of mode. Right now in rectangle mark mode, the point looks like it's in a virtual edit mode: it can move past the end of a line with `C-f`. But when you yank a rectangle after doing this, the rectangle appears at the mark instead of the point. If it appeared at the point, that would solve this issue. – nanny Nov 17 '14 at 19:52

2 Answers2

19

Another answer: use M-x picture-mode. You can kill or copy rectangles through the middle of tab chars etc., send them to registers, etc.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 3
    One advantage of using this method is that you can control exactly how wide you want your rectangle selection to be. If I have line 1 with 30 chars and line 2 with 5 chars, I can choose the rectangle to be 15 chars wide and 2 lines tall; can't do that with `extend-rectangle-to-end` as the rectangle selection with that will always be 30x2. – Kaushal Modi Nov 17 '14 at 20:19
  • 1
    `picture-mode` is old and little-known. Among other little-known features it has: exiting (`C-c C-c`) removes trailing whitespace. – Drew Nov 18 '14 at 20:33
  • 1
    That's brilliant, Drew! Question: are there any reason *not* to advice `rectangle-minor-mode` to be "wrapped" in `picture-mode`? (advice since I don't see any hooks in the source of `rectangle-minor-mode`). – rasmus Nov 21 '14 at 11:32
  • 1
    Here's my advice, btw. It doesn't strip whitespace since that's kind of uncalled for here: `(defun rectangle-mark-mode-wrapped-in-picture-mode (orig-fun &rest args) (if rectangle-mark-mode (picture-mode) (and (eq major-mode 'picture-mode) (picture-mode-exit t))))` `(advice-add 'rectangle-mark-mode :around #'rectangle-mark-mode-wrapped-in-picture-mode)` – rasmus Nov 21 '14 at 11:46
11

The extend-rectangle-to-end function in the rectangle-utils package is what you want. It's on melpa.

It inserts spaces to make the current rectangle selection extend to cover the longest line in the region.

To get this behaviour, I've got this in my init.el. C-x r e is not bound to anything else in vanilla emacs

(require 'rectangle-utils)
(global-set-key (kbd "C-x r e") 'extend-rectangle-to-end)
Squidly
  • 1,499
  • 14
  • 17
  • 1
    To clarify, with this package, you select the region, then do `M-x extend-rectangle-to-end` and then `C-x r M-w` or `C-x r k`. – Kaushal Modi Nov 17 '14 at 18:47