1

I'm on a server, and the server-administrator are really conservative about what I put on that server. It took bottles of wine, just to be able to put a .vimrc-file on there - so in order to install a plugin, then I assume I will have to give him my first-born child.

I'm editting these files that has CRAAAZY indentation. Tabs, spaces - you name it. I was looking for at way in my .vimrc-file to set up the following:

  • Make all tabs one color.
  • If there is 3 spaces in a row, then make them another color
  • If there is 6 spaces in a row, then make them a third color
  • if there is 9 spaces in a row, then make them a a fourth color ... etc. etc. etc.

This way, then I can make some pseudo-indentation-guides, that I can have on the server, and keep all my babies to myself.

This answer has something that comes close, but no cigar: How to add indentation guides/lines

1
  • 1
    "Plugins" are just "bits of vimrc in another directory". Just copy/paste the plugin code you want in your vimrc. Jun 28, 2017 at 11:42

1 Answer 1

5

So you want a custom plugin, that you can copy paste into your .vimrc and be done?

Something like this should do it:

function! MyPoorIndentGuide(clear)
    if !exists("w:ids")
        let w:ids=[]
    endif
    if a:clear
        call map(w:ids, 'matchdelete(v:val)')
        unlet! w:ids
        return
    endif
    hi HiTabs ctermfg=red guibg=red ctermbg=red
    hi HiSpaces3 ctermfg=blue guibg=blue ctermbg=blue
    hi HiSpaces6 ctermfg=yellow guibg=yellow ctermbg=yellow
    hi HiSpaces9 ctermfg=green guibg=green ctermbg=green
    call add(w:ids, matchadd('HiTabs', '\t'))
    for i in range(3, 9, 3)
        exe printf("call add(w:ids, matchadd('HiSpaces%d', '\\s\\{%d\\}', %d))", i, i, i+10)
    endfor
endfunction

function! ToggleIndentGuides()
    if !exists('#MyIndentGuideGroup')
        augroup MyIndentGuideGroup
            au!
            au WinEnter * call MyPoorIndentGuide(0)
        augroup end
        call MyPoorIndentGuide(0)
    else
        augroup MyIndentGuideGroup
            au!
        augroup end
        augroup! MyIndentGuideGroup
        call MyPoorIndentGuide(1)
    endif
endfunction

com! MyIndentGuideToggle :call ToggleIndentGuides()

Note: it uses the background color to highlight, since we are highlighting invisible things, we cannot use the foreground (unless one uses :set list listchars=...), however that means, highlighting might be a little bit, well funky. You might want to customize the colors to your liking.

2
  • That's awesome!! Thank you. For other bone-heads like me, who don't get it straight away, then you have to write :call ToggleIndentGuides() in order to activate this magic.
    – Zeth
    Jun 28, 2017 at 20:39
  • 3
    @Zeth you can actually use :MyIndentGuideToggle to call the function. Jun 28, 2017 at 20:52

Your Answer

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

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