5

I use four spaces for indent. I would like to replace the indentation with some UTF-8 chars.

What I have:

1.

def foo(bar):
    for jaffa in bar:
        print jaffa

2.

var foo = function(bar){
    bar.forEach(function(jaffa){
        console.log(jaffa);
    });
}

What I want:

1.

def foo(bar):
►for jaffa in bar:
►►print jaffa

2.

var foo = function(bar){
►bar.forEach(function(jaffa){
►►console.log(jaffa);
►});
}

I tried using conceal myself, but I couldn't come up with a pattern to match the indentation spaces.

How would I accomplish changing the indentation to UTF-8 chars using vim conceal as described in the examples?

@Muru gave the answer for this question. But what if I want to conceal the last 'block' of the indentation spaces differently than the ones before it? Example:

var foo = function(bar){
►bar.forEach(function(jaffa){
◼►console.log(jaffa);
►});
}

2 Answers 2

6

With the following:

set conceallevel=2
syn match IndentWS '\v(^( {4}|\t)*)@<=( {4}|\t)' conceal cchar=▶

I get:

enter image description here

The regex \v(^( {4}|\t)*)@<=( {4}|\t):

  • uses very magic \v
  • matches a sets of 4 spaces ( {4}) or a tab \t, at the beginning of the line, with zero width \@<=
  • and then a set of 4 spaces or a tab.

Of course, if you use only tabs or spaces, you can simplify the regex accordingly.

For the update, with:

set conceallevel=2
syn match IndentWS '\v^( {4}|\t)+\S@=' contains=IndentEndWS,IndentStartWS
syn match IndentStartWS '\v( {4}|\t)' conceal cchar== 
syn match IndentEndWS '\v( {4}|\t)\S@=' conceal cchar=▶

I get:

enter image description here

Making ends highlight differently would need a region, or contained matches. In this case I used two contained matches, one for general indentation whitespace, and one for indentation just before a non-whitespace character (\S).

2
  • Exactly what I wanted. How would I change the last conceal character to a different character to signify that it is the last block of indent. Explanation is added in the question. Jul 3, 2016 at 12:25
  • @SriHariRajuPenmatsa see update. If you have further requests, post a new question.
    – muru
    Jul 3, 2016 at 13:02
5

This could be used call matchadd('Conceal', '\s\{'.&ts.'\}', 10, -1, {'conceal': '►'})

This uses the matchadd() function to conceal parts of your text, which needs a 7.4.500 release or something. This has the advantage, that the match functions can override syntax rules, the other way around might not be always possible.

2
  • Interesting. I know nothing about this feature. I'll read it up when I get some time. Thank you. Jul 3, 2016 at 12:29
  • If you'd like to avoid concealing spaces inside strings, you can use something like call matchadd('Conceal', '\(\_^\s*\)\@<=\s\{'.&ts.'\}', 10, -1, {'conceal':'►'})
    – sheerun
    Oct 5, 2020 at 15:32

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.