user4189799
user4189799

Reputation: 43

Vim: Hide all code around selected code

I want to be able to hide all code around the specific section of code that I am working with. Now I am wondering if this is possible in Vim somehow. I have experimented with it a bit already and have been successful at hiding lines above and below my selection by using highlight group Igore. This enables me to only see the lines that I want to focus on but the problem is when I begin to edit the code and add or remove lines. When I add a line or remove a line the already set highlight group Ignore is still maintaining the set line numbers so I either get to see some of the hidden code or some of the code that I want to see gets long and extends into the hidden line numbers. So I am wondering if there is some way to fix this or any other way to accomplish what I want in Vim?

Appreciate any suggestions!

Upvotes: 0

Views: 1187

Answers (2)

Asenar
Asenar

Reputation: 7030

To hide a range of lines (let's say from 1 to 10 and 20 to end, you can type :1,10fo|20,$fo

From there, you can create a function based on the current cursor position -10/+10

Note you have first to :set foldmethod=manual to make this works.

EDIT: a simple solution : :1,.-10fo|.+10,$fo

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172788

Hiding or shading parts of the buffer is not the Vim way. Folding is the built-in feature that comes closest. With :set foldmethod=manual, you can then use zf or :fold to hide the parts above and below.

For a plugin solution, have a look at NrrwRgn - A Narrow Region Plugin. It allows you to edit parts of a buffer in a separate scratch buffer, with automatic syncing back.

Upvotes: 3

Related Questions