5

I have some code in my ~/.vimrc that (simplified) looks like this:

function! SomeAppendFunction()
    let failed = append(0, ["Some header text"])
endfunction
command SomeAppendFunction :call SomeAppendFunction()

The intent is to provide a command that prepends some fixed text to any file I wish. I don't really care too much about handling errors from the append() function.

When I run Syntastic over this, which uses vimlint to check it, it quite rightly warns that I'm not using the return variable failed:

/Users/xyz/.vimrc|255 col 9 warning| [EVL102] unused variable `l:failed`

However, if I remove the let failed = from the line inside the function, so it looks like this:

function! SomeAppendFunction()
    append(0, ["Some header text"])
endfunction
command SomeAppendFunction :call SomeAppendFunction()

... I instead now see: ...

line  301:
E126: Missing :endfunction

... when I start vim.

How can I avoid the warning about the unused variable, but also have a syntactically correct function?

4
  • 1
    There are a few ways to do it: (1) call append(...); (2) put " @vimlint(EVL102, 1, l:failed) and " @vimlint(EVL102, 0, l:failed) around your function; (3) let g:syntastic_vimlint_options = { 'EVL102': 1 }; (4) use g:syntastic_quiet_messages; (5) use g:syntastic_vim_vimlint_quiet_messages.
    – lcd047
    Commented Nov 19, 2015 at 7:34
  • @lcd047, ok, thanks. All but (1) are suppressing the warning in vimlint, not fixing the root cause. So I'm going to accept Christian's answer. But good tips if there's a slightly different vimlint issue in future. Commented Nov 19, 2015 at 7:50
  • The root cause is you not checking for errors. None of the above fixes that, but all of them answer your question as you asked it: "avoid the warning about the unused variable, but also have a syntactically correct function". shrug
    – lcd047
    Commented Nov 19, 2015 at 8:41
  • @lcd047 fair point :) Anyway, thanks for the answer... Commented Nov 19, 2015 at 9:01

1 Answer 1

6

append() is a function and no command. As such, it must be called using the :call command. So use :call append(). Second, if you want to ignore errors, you can wrap the whole command into a try...catch...endtry statement like this:

try
   call append()   " could fail for whatever reason
catch
   " no-op
endtry
1
  • Perfect, using call works. I'm not interested in swallowing any errors either (it's not something worth writing error-handling for, but I'm happy for the error to bubble up - if you see what it's doing, it's very unlikely to fail anyway :). Thanks. Commented Nov 19, 2015 at 7:14

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.