(or emacs irrelevant)

Rectangle-mode Hydra

Today, I'll show a very useful hydra that I found yesterday on the hydra wiki. The idea is by @zhaojiangbin, I've made some minor changes to get the arrows to work the way that I like, and made all keys into plain letters.

The code

(defun ora-ex-point-mark ()
  (interactive)
  (if rectangle-mark-mode
      (exchange-point-and-mark)
    (let ((mk (mark)))
      (rectangle-mark-mode 1)
      (goto-char mk))))

(defhydra hydra-rectangle (:body-pre (rectangle-mark-mode 1)
                           :color pink
                           :post (deactivate-mark))
  "
  ^_k_^     _d_elete    _s_tring     |\\     ‗,,,--,,‗
_h_   _l_   _o_k        _y_ank       /,`.-'`'   .‗  \-;;,‗
  ^_j_^     _n_ew-copy  _r_eset     |,4-  ) )‗   .;.(  `'-'
^^^^        _e_xchange  _u_ndo     '---''(‗/.‗)-'(‗\‗)
^^^^        ^ ^         _p_aste
"
  ("h" backward-char nil)
  ("l" forward-char nil)
  ("k" previous-line nil)
  ("j" next-line nil)
  ("e" ora-ex-point-mark nil)
  ("n" copy-rectangle-as-kill nil)
  ("d" delete-rectangle nil)
  ("r" (if (region-active-p)
           (deactivate-mark)
         (rectangle-mark-mode 1)) nil)
  ("y" yank-rectangle nil)
  ("u" undo nil)
  ("s" string-rectangle nil)
  ("p" kill-rectangle nil)
  ("o" nil nil))

(global-set-key (kbd "C-x SPC") 'hydra-rectangle/body)

There was a lot of screen estate left over, so I added some ASCII-art. I wanted something related to syrup, or at least pancakes, but instead I found a cat. Apparently, it's very easy to find pictures of cats on the internet. Who knew.

Here's how it looks like in-action:

hydra-rectangle

The pink variation is pretty useful here, since it doesn't get in the way of e.g. DEL or C-n or C-e or inserting spaces.

I've been using it today for editing some table data in org-mode, and it feels pretty efficient.

Some explanations

What does what:

  • d deletes rectangle; it's similar to C-d.
  • n copies rectangle; it's similar to M-w.
  • o exits; it's very easy to press.
  • e exchanges the point and mark; it's also quite useful to re-activate the region if you disabled it with n or r.
  • s fills the selected rectangle with a string.
  • y yanks the rectangle that you saved before with n.
  • r deactivates or activates the rectangle at point.
  • u calls undo.
  • p kills the rectangle; it's similar to C-w.