One of the joys of writing this blog is the things I learn from the comments. The other day, I wrote about fancy-narrow and that provoked an interesting discussion between Wilfred and Phil. Wilfred compared fancy-narrow
to narrow-to-region-indirect
. I assumed that narrow-to-region-indirect
was a standard command or function that I wasn’t familiar with and put it in my todo queue to investigate. Phil beat me to it and discovered that it was actually a very useful piece of custom code.
The idea is that you clone the current buffer one or more times in such a way that you can narrow to different regions in each of the clones. If the utility of doing this is obscure to you, Zane Ashby has a nice post that gives one use case as well as showing code for a function to do it. As Phil remarks, before he saw that code he used to do the same thing manually.
You might think that you could open a file in a buffer and then split the buffer the appropriate number of times to do this but that doesn’t work because narrowing in one of the buffers narrows in all of them. The trick is to create indirect buffers, which can be narrowed separately. Changes made to the text of any of the indirect buffers is reflected in all the clones and the base buffer so that when you’re finished you can just delete the clones and save the base buffer.
You can find the code to automate this in Ashby’s post or a slightly different version by grayswx in this paste. If you have the need to work in different parts of a buffer at the same time—especially if you want to use different modes in each—this technique (and the function that implements it) could be just what you need.