7

I would like to either find or write a plugin that does the following:

Assume I have this python code:

def foo(a,b,c):
    # comment
    [100 lines that do stuff]
    for i in range(N):
        [100 lines that do stuff]
        # another comment
-->     more code

Let's assume this does not vertically fit in to my window because the height is only 100 lines and I can't see the entire function as a whole. Not even the beginning of the for-loop when my cursor is where the arrow --> is.

I would like to have plugin, where the buffer would look as follows:

def foo(a,b,c):
    for i in range(N):
-----------------------------------------------------
        [96 other lines that I can see]
        # another comment
-->     more code

In other words, I would like to see the lines that are not visible for me which are the first lines to be one indentation level higher. This way when my cursor is at -->, I would not need to scroll up to figure out which function or for-loop I am in.

Does such a plugin exist? If not, how would one add these lines at the top of the editor without modifying the buffer?

10

There's plug-in wellle/context.vim which seems to implement what you requested. (Perhaps it's the one D. Ben Knoble was trying to recall?)

It will try to show context (if, for or while block, function/class definitions, cases within a switch command, etc.) for the line under the cursor.

It doesn't seem to use indentation (only) as a hint for where to find context, but it claims to support most languages out of the box, and the plug-in seems to have ways to customize and override settings if you'd like to tweak the behavior. It also supports commands and mappings to temporarily enable/disable or quickly show/hide context.

2
  • 2
    Wow, that seems to be exactly what I need. Thanks
    – cmosig
    Dec 7 '21 at 18:00
  • 2
    I just tried it and It also works for def and class. Amazing
    – cmosig
    Dec 7 '21 at 18:03
2

I think I've heard of a plugin that does this, but I cannot recall it's name. When I need this I do one of two things:

  1. Create a split. I lean towards vertical splits, since my monitor is wider than tall, but either would work. In the split you are working in, be near that more code line. In the other split, move upwards to the indent or other code you want to see.

  2. Create (temporary?) folds to hide those unwanted lines. Then you see what you choose to.

A third option is to refactor for shorter bodies in the indentation levels :)

1
  • Uh that's good news. I wonder if someone remembers the name of that plugin. Thanks for the options. In my mind it seems like too much work figure out where you are in your code though...
    – cmosig
    Dec 7 '21 at 15:30
2

I would recommend writing a plugin that adds some text to your statusline.

You can create a function that returns the text of the previous line that has one less unit of indentation (use indent())

Then you can call the function in a statusline definition like this:

%{MyFunc()}

If you use vim-airline, then can define sections of the airline status line:

let g:airline_section_y='%{MyFunc()}'

By putting your function result in the statusline, then it will update whenever you move your cursor. Because it's called frequently, try to reduce the runtime of the function if necessary.

1
  • 1
    Thank you, that is very helpful. Not sure if I would want to put it into the status line though. It is one possible solution though!
    – cmosig
    Dec 7 '21 at 16:09
0

One option is to use folding

set foldmethod=manual " this is likely the default

And use visual mode to select the lines to hide and then :fold them to focus on relevant part.

1
  • Yes, you are right, that is possible. I feel like it makes the editing process more tedious though.
    – cmosig
    Dec 8 '21 at 16:18

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.