I have this snippet:

let g:git_file_prefix = ['M','A','R','D','U','?']

function! Colorize(str)
  let index = 0
  for file in a:str
   call add(a:str[index], '$(tput 37)')
   let index = index + 1

call Colorize(g:git_file_prefix)

As you can see, the global variable git_file_prefix has already been defined as a list. Yet vim returns E714 List required on line calling the add() function.

What might be wrong in this snippet?


As the doc :h add() says:

add({list}, {expr})                 *add()*
        Append the item {expr} to {list}.

The first argument of the function add() should be a list but you give it a:str[index] which is an element of the list (so a string) not the list itself.

If you want to concatenate '$(tput 37)' to the element of the list you probably want to use let a:str[index] .= '$(tput 37)'

| improve this answer | |
  • 1
    You're right! I can't believe I missed that!. thanks mate +1.. still have to wait few more minutes to accept. – John Fred Fadrigalan Nov 14 '18 at 13:52
  • just one more thing, how can i prepend a value to a string? – John Fred Fadrigalan Nov 14 '18 at 14:02
  • 1
    @JohnFredFadrigalan You're welcome :) Also if I can give you a little advice, naming your list str doesn't sound like a good idea: 1) str is not really a name which conveys what the variable contains and 2) the worst part it is misguiding about the type of the variable. And about your for loop which uses an index and do the same thing with all the items, you probably want to use :h map() instead :) – statox Nov 14 '18 at 14:03
  • 1
    And to prepend to a string you could use let str = 'new value' . str for example – statox Nov 14 '18 at 14:04
  • 1
    Thanks! you're really a big help! since i'm a novice in this stuff.. I'll try looking it up! – John Fred Fadrigalan Nov 14 '18 at 14:10

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.