4
:set cc=1,5,9,13,17

The command above here can create a series red lines on the edited file in vim whose version is lager than 7.3.

Why can't write it as :set cc=range(1,17,4) ?

5

There's two reasons you can't use this syntax:

  • Firstly you can't mix functions calls and normal commands without distinction. Your syntax should be exec ":set cc=" . range(1, 17, 4). to concatenate the result of the function range() with the normal command :set cc=.

  • Secondly even with this syntax your command will not work: When you use :echo range(1, 17, 4) you get a list: [1, 5, 9, 13, 17] so the previous command would expand to :set cc=[1, 5, 9, 13, 17] which isn't a correct syntax.

A possible solution would be to cast the result of range as a string, remove the [ and ] characters and then concatenate the result with :set cc= with the exec command.

Edit @Saginaw suggested to use the function join() to implement the transformation of the list to a string without brackets characters. Using this idea the final solution for your question would be:

Use this:

exec "set cc=" . join(range(1, 17, 4), ",")

Thanks to @Saginaw for his idea.


You might want to read:

| improve this answer | |
  • 1
    You could use the join() function with the second argument ',' which would give: execute 'set colorcolumn=' . join(range(1, 17, 4), ','). You could also use the following syntax: let &l:colorcolumn = join(range(1, 17, 4), ',') The prefix &l: is used to change the window-local value of the option (instead of the global one). – saginaw Mar 23 '16 at 10:01
  • 1
    @saginaw: Your use of join() is indeed the good way to go, do you mind if I add it to my answer? About the let &l:colorcolumn I guess it depends on what OP wants to do :-) – statox Mar 23 '16 at 10:05
  • 1
    Ah yes you're right, the window-local scope was maybe not a good idea. Yes, if you think join() is a good idea, you should add it to the answer. If the window-local scope is not what the OP wants, in the 2nd syntax you could simply remove l:. – saginaw Mar 23 '16 at 10:07
  • 1
    Using let &cc = join('1, 17, 4), ',') as @saginaw suggested is shorter and accomplishes the same result :) – VanLaser Mar 23 '16 at 12:32
  • 1
    Damn indeed that is shorter, I focused on the &l part and didn't even realize that the important point of @saginaw's solution was that he used a more clever solution. As the approach is pretty different (and better!) than mine I guess that would be worth a second answer :-) – statox Mar 23 '16 at 12:37

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.