Is there a simple way to search within the current column or within a rectangle?

I am navigating a CSV file and would like to find the value "10" in a particular column. Searching the entire file is unproductive. However, it is easy to select the column with a rectangle.

I don't see any commands to search within a rectangle or to narrow to a rectangle. The best I've been able to come up with is to copy the column and paste it into a scratch buffer and then search.

Any ideas?


You can do this with library Isearch+. As far as I know, this is the only library that offers this possibility.

  1. Set or bind option isearchp-restrict-to-region-flag to non-nil. This means that when the region is active and you start isearching the search will be limited to the region.

  2. Select the text between the upper-left and lower-right corners of the column you want to search as the active region.

    You might want to use C-x C-x (to exchange-point-and-mark), depending on which end the cursor is at and which direction you want to search.

  3. Make the region rectangular using C-x SPC (command rectangle-mark-mode.

  4. Start isearching (regexp or literal-string search).

If option isearchp-deactivate-region-flag is non-nil then the region is automatically deactivated when you start searching, so you can better see the search space.

(You can use C-x n (command isearchp-toggle-region-restriction) and C-SPC C-SPC (command isearchp-toggle-region-deactivation) during search to toggle isearchp-restrict-to-region-flag and isearchp-deactivate-region-flag, respectively, but in each case the new value takes effect only when the current search is exited.)

If you use also library Mode-Line Position then not only can you restrict Isearch to the active region but you can keep that restriction when you invoke a query-replacement command from Isearch (e.g. M-% from Isearch, to use query-replace).

The main job of that library is to show information about the active region in the mode line. And whenever you invoke a replacement command or Isearch for the active region, that region information is highlighted specially.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.