Wayne Werner
Wayne Werner

Reputation: 51807

Is it possible to make vim display leading spaces with a different amount of indentation?

It appears, surprisingly, that more self-selected SO devs prefer to indent via tabs rather than spaces. Some people brought up the argument that you can use tabs to indent, and spaces to align. In theory this sounds cool, but in practice I suspect it would be more of a pain than anything, seeing as you can't see which character you have (unless you like turning on that sort of thing).

So I had an idea - why not the editors? Why shouldn't editors let you configure the number of spaces you're going to use to indent, but also the appearance of those spaces. That is:

Normal:

class MyClass:
____def myfun():
________somevariable = 42
________volts        = 40000000 # If you're into that sort of thing. 
________________________________# Not well-formatted Python, though.

Leading indent set to appear as 2 spaces:

class MyClass:
__def myfun():
____somevariable = 42
____volts        = 400000000

Is it possible to do something like this with vim? I know it's totally possible to write a post-open/pre-save command to replace the contents, which might work the same... but I'm more curious if it's possible, in vim, to make it appear as though the leading spaces are less (or more) than they actually are?

Upvotes: 1

Views: 55

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172540

Yes, you can, using the conceal feature. Demonstration (using the markup from your example text and a different replacement character instead of spaces for effect):

:syntax match Indent "\%(^\%(__\)*\)\@<=__" conceal cchar=#
:set conceallevel=2 concealcursor=nvic

The pattern matches every pair of __ at the beginning of the line, and replaces (conceals) each with a single #, effectively reducing the visible indent.

As a purely visual feature, I don't find it very useful though, and would prefer the post-open / pre-save solution you seem to be aware of.

Upvotes: 1

Related Questions