1

Suppose we have a file:

1. un
2. deux
3. trois
4. quatre
5. cinq
6. six
7. sept
8. huit
9. neuf
10. dix

I want to create a window that shows only lines 5--10 and nothing else. Modifying them should modify the original lines (hence it refers to the same buffer). Ideally, adding and removing lines would add and remove lines into the original buffer. Is that possible, out of the box?

(The reason is to focus on that part of the text... and procrastination.)

  • Welcome to Vi and Vim! Nice first question. Hopefully you've found that this is not so impossible after all :) – D. Ben Knoble Oct 28 '19 at 22:25
2

The NrrwRgn plugin (the plugin page has links to alternative plugins) comes closest to your use case: As it mirrors part of the buffer into a separate scratch buffer (and can sync it back to the original), you can treat it like any regular Vim buffer, and edit away without worries.

There are other workarounds, like

  • folding other lines (what you've discovered)
  • using syntax highlighting (to the Normal group, or maybe NonText) or the conceal feature to make the contents invisible (whereas folding only works on whole lines, this works best on columns)

However, the hidden fragments are still there, so searches and other commands like :substitute will still find them, so the illusion is far from perfect and you have to be vigilant during editing.

The best out of the box solution that I can imagine is a set of :autocmds that move the uninteresting lines into buffer-local List variable(s) on BufReadPost,BufWritePost and undo that on BufWritePre. That way, the lines would only be there during saving.

  • NrrwRgn plugin produces errors when I move the new scratch buffer into a separate tab: smth weird happens. Yet, my original question was about 'new window' not 'new tab', so accept. Agreed about :substitute affecting the folded lines in the case of my workaround. thanks! – Ayrat Oct 29 '19 at 15:06
  • 1
    I also get an error when :quitting a scratch buffer that was moved to a different tab page (but no information is lost): E121: Undefined variable: b:nrrw_instn. I guess that use case hasn't been considered by its author yet. – Ingo Karkat Oct 29 '19 at 16:50
1

Seems impossible, but there is a workaround. Create a new window and fold (using zf) unnecessary text. Note that by default the search command will search in the folded text too. To avoid this, run :set fdo-=search, then vim only indicates that the item is found in the folded block, but won't unfold it. (This command changes the behaviour in other windows as well.)

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.