TeMPOraL_PL · 2022-05-17 11:28
I'm aware of Emacs [rectangle commands](https://www.gnu.org/software/emacs/manual/html_node/emacs/Rectangles.html). Unfortunately, they all work on a rectangle in bulk. I'm wondering if there are commands - built-in or from an external package - that would allow me to narrow the buffer to a *rectangle*, or otherwise edit text within the rectangle?
My immediate use case is text that's effectively a table, like [Hydra](https://github.com/abo-abo/hydra)'s "awesome docstring". Taking the first example from the repo README:
"
^Mark^ ^Unmark^ ^Actions^ ^Search
^^^^^^^^-----------------------------------------------------------------
_m_: mark _u_: unmark _x_: execute _R_: re-isearch
_s_: save _U_: unmark up _b_: bury _I_: isearch
_d_: delete ^ ^ _g_: refresh _O_: multi-occur
_D_: delete up ^ ^ _T_: files only: % -28`Buffer-menu-files-only
_~_: modified
"
Say I want to move the `save` command to be before `delete`, and I also want to remove `mark` command. Currently, I have to deal with manually un-reordering and realigning all the other entries to the right of the text I want to manipulate. A better workflow I imagine would be to narrow editing to a rectangle covering the first column of the Hydra docstring, and do my manipulation in that scope, without worrying about text around it. Is there anything available that would support such workflow? Or, what are the recommended alternatives?
flexibeast · 2022-05-17 11:46
Create a rectangle region, then call `narrow-to-region`?
TeMPOraL_PL · 2022-05-17 12:06
Doesn't work, it narrows to regular region between point and mark.
flexibeast · 2022-05-17 12:09
Dang.
ndamee · 2022-05-17 12:22
Turn on `overwrite-mode`, and then you can just type over things, no need to realign other columns.
ndamee · 2022-05-17 12:33
You can use `picture-mode` together with rectangle mode to copy/move/overwrite rectangles.
See this question and answer: https://emacs.stackexchange.com/questions/86/how-to-overwrite-a-copied-rectangle-instead-of-inserting-it-which-is-the-defaul
00-11 · 2022-05-17 17:14
Good question.
Other answers tell you how to edit text in the rectangular region, but not in a way that limits editing to that area.
Here are two things that can help:
1. Replacement commands, such as `query-replace` limit their action to matches in the rectangular region, when active.
2. Library [Isearch+](https://www.emacswiki.org/emacs/IsearchPlus) ([`isearch+.el`](https://www.emacswiki.org/emacs/download/isearch%2b.el)). It can limit search hits to matches in the active region, which can be rectangular.
This is controlled by option `isearchp-restrict-to-region-flag`. You can toggle this anytime while searching with `C-x n` (command `isearchp-toggle-region-restriction`).
Be aware that for a rectangular region search matching is carried out by (1) first matching within the region normally, disregarding the rectangular limits (start and end for each line), and then (2) filtering out matches that are not completely within the rectangle.
This is different from what a proper constraining of search to the rectangle would provide. For example, if a regexp search matches across a rectangle boundary, but there is also a (shorter) match for the same regexp that is within the rectangle and ends at that boundary, that match will not be counted -- it's filtered out because search chooses the longest match for the regexp, and that match is filtered out because it's not entirely within the rectangle.
The same limitation does not apply, however, to the overall region limits. With non-nil `isearchp-restrict-to-region-flag` Isearch+ searches only within the region, just as if the buffer were narrowed to it.
kaushalmodi · 2022-05-17 19:16
My thought process ..
That SO question looks so familiar ..
_clicks the link_
Of course!