Skip to content

Instantly share code, notes, and snippets.

@jdtsmith
Last active March 8, 2023 07:19
Show Gist options
  • Save jdtsmith/bfa2d692c4fbbffe06b558e4bcf9abec to your computer and use it in GitHub Desktop.
Save jdtsmith/bfa2d692c4fbbffe06b558e4bcf9abec to your computer and use it in GitHub Desktop.
Supercharge emacs rectangle-mark-mode with modal keys
(use-package rect
:init
(defun my/rect-transient-map-info ()
(interactive)
(with-help-window "Rectangle Mark Command Help"
(dolist
(l '("Rectangle Mark Mode Modal Commands\n"
"==================================\n\n"
"Insertion:\n\n"
" [o] open fill rectangle with spaces, moving adjacent text right\n"
" [t] string replace rectangle with prompt string\n\n"
"Killing:\n\n"
" [k] kill kill and save rectangle for yanking\n"
" [d] delete kill rectangle without saving\n"
" [SPC] del-ws delete all whitespace, starting from left column\n"
" [c] clear clear rectangle area by overwriting with spaces\n\n"
"Rectangles:\n\n"
" [n] new start a new rectangle from this location\n"
" [w] copy copy rectangle for future yanking\n"
" [y] yank yank rectangle, inserting at point\n\n"
"Etc:\n\n"
" [q] quit exit rectangle-mark-mode\n"
" [?] help view this Help buffer"))
(princ l))))
:config
(cl-loop for (key def) in
'(("k" kill-rectangle) ("t" string-rectangle)
("o" open-rectangle) ("w" copy-rectangle-as-kill)
("y" yank-rectangle) ("c" clear-rectangle)
("d" delete-rectangle) ("N" rectangle-number-lines)
(" " delete-whitespace-rectangle)
("=" calc-grab-sum-across) ("+" calc-grab-sum-down)
("#" calc-grab-rectangle) ("n" set-mark-command)
("q" (lambda () (interactive) (deactivate-mark)))
("?" my/rect-transient-map-info))
do (define-key rectangle-mark-mode-map key def)))
@jdtsmith
Copy link
Author

jdtsmith commented Mar 6, 2023

Updated to use rectangle-mark-mode-map (with thanks to @oantolin).

@jdtsmith
Copy link
Author

jdtsmith commented Mar 7, 2023

Updated to use ? for a help screen instead of the repeating message. Also added q to quit, and n to re-start drawing a new rectangle from point.

@raveensrk
Copy link

Why do you need use-package for this? Is it related to rect package?

@oantolin
Copy link

oantolin commented Mar 7, 2023

I'm obviously biased, but I'd bind ? to (lambda () (interactive) (embark-bindings-in-keymap rectangle-mark-mode-map)) instead. 😛

@jdtsmith
Copy link
Author

jdtsmith commented Mar 7, 2023

@raveensrk Just a convenient way to package it. See SpeedRect for a package version of this I put together. @oantolin nice idea. I went the "longer docs" route in the package.

@raveensrk
Copy link

@jdtsmith Thank you for this. Installed and using it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment