3

Some of the files I am editing have a huge amount of whitespace leading before each line, for example:

module Something
  module Inside
    class FinallyClass
      def each_method_adds_indentation_as_well
        # actual code
        # ..
        # ..
      end
    end
  end
end

And sometimes even more indentation happens before it actually gets into the code part which I work on.

Is there a way to condense one, or two levels of indentation somehow through a plugin or a setting? (while maintaining the file contents intact)

So that above would become

module Something
module Inside # (hidden indent level 1)
class FinallyClass # (hidden indent level 2)
  def each_method_adds_indentation_as_well
    # actual code
    # ..
    # ..
  end
end # (hidden indent level 2)
end # (hidden indent level 1)
end 
1
  • class Something::Inside::FinallyClass also saves you two levels of indentation. I don't understand why so many Rubyists add so much unneeded indentation like that :-/ Commented Dec 6, 2017 at 10:05

2 Answers 2

6

You can achieve this with Vim's conceal feature:

" Hide one or two two-space indents
syntax match HideTwoIndents /\v^  (  )?/ conceal

" (Optional) Hide regardless of cursor position
set concealcursor=nvic

" Hide concealed matches without replacement chars completely
set conceallevel=2

This tells Vim to hide the first (and second, if that exists) level of indentation using a regular expression.

If this is something you want to use a lot (and not just a quick fix for a particular set of files) then you'll want to improve it to build the regular expression using your current 'shiftwidth' value.

Because of some odd behaviour with the way Vim displays concealed tabs, you'll need something slightly more complicated for tab indents:

syntax match HideOneIndent /\v^\t\ze[^\t]/ conceal
syntax match HideTwoIndents /\v^\t*\zs\t\t\ze[^\t]/ conceal

Further Reading

  • :help conceal
  • :help 'conceallevel'
  • :help 'concealcursor'
4
  • Tried adding this to vimrc to test it out, but with no effect. I am using neovim, but I would hope that this would work as well?
    – hakunin
    Commented Dec 5, 2017 at 15:23
  • @hakunin I haven't tried it in neovim, but it should work. Does it work if you enter the commands into an already running copy of neovim?
    – Rich
    Commented Dec 6, 2017 at 9:37
  • I think you may have to use autocmd or after directory to ensure it doesn't get overwritten by Ruby's syntax (didn't test). Commented Dec 6, 2017 at 10:06
  • @Carpetsmoker That was my thinking too: I was just trying to diagnose the issue first.
    – Rich
    Commented Dec 6, 2017 at 10:15
2

I made a plugin that does this

https://github.com/fent/vim-relative-indent

It doesn't use the conceal feature. It instead scrolls the window to the right accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.