Zamansky 28: Rectangles

Mike Zamansky has posted number 28 in his Using Emacs video series. This one is about rectangle editing. As Zamansky says, you don’t often need this but when you do it’s really handy.

My problem with it is that because I use it so infrequently, I can never remember the key strokes. There are three solutions to that, one of which the video demonstrates: simply call the command by it’s name. If you’re using something like counsel, ido, or helm, that’s pretty easy because they will help you fill in the entire name correctly even if you don’t remember the exact name. Another possibility is to make a hydra for the rectangle commands. I’m always one day away from doing that. Finally, there’s which-key. Once you have that enabled, you can’t really use the “I can’t remember the proper keys” excuse anymore. If you can remember that Ctrl+x r is the prefix for dealing with rectangles, which-key will tell you what to do next.

The video is only 5 and a half minutes so it’s easy to find time to watch it. If you’re not familiar with editing rectangles, you should definitely spend the time so that when you need to use it, you’ll have an idea of what’s available.

This entry was posted in General and tagged . Bookmark the permalink.
  • glucas

    Another option is to use the package smart-region, which lets you use one command to mark a region in several different ways, including a rectangle. I've currently bound this to but you could just replace the default binding as well.

    If you have the supporting packages installed you can use smart-region's single command as a front-end to expand-region, and mc/edit-lines (from multiple-cursors) in addition to rectangle-mark-mode.

    See: https://github.com/uk-ar/smart-region

    • jcs

      Hmmm. That sounds really nice. I'm definitely going to check it out. Thanks for the pointer.

    • Mattias Bengtsson

      Yeah +1 on smart-region as well. One of my favourite packages for emacs.

  • Has anyone made a hydra for rectangles?

  • Phil

    I used to only use the rectangle kill/copy/yank commands, but these days I find C-x r t -- string-rectangle -- the most frequently used command of the set, usually as a form of search and replace, or sometimes just to insert. You might prefer multiple-cursors for all such use-cases, but in a great many situations string-rectangle is the easiest and quickest way to do what I want. Note that if your rectangle has no width, then you are replacing nothing with something on each line (i.e. inserting). They've made it a bit more interactive as of Emacs 25, but I think I actually preferred how it was before.

  • Alan Third

    While not quite as interactive as which-key, you can also do C-x r ? to list the key bindings and doesn't involve installing anything else.

    I use the rectangle commands all the time to manipulate data files, and like Phil says, C-x r t is top of the bunch.

  • Thomas L.

    Niiiice ! Thx !

  • I have the same problem of you, I can remember the keybinds, so I use one Hydra configuration made it by Abo Abo: https://github.com/abo-abo/hydra/wiki/Rectangle-Operations
    It's awesome!