1

This command works without errors:

:echo v:oldfiles->copy()->filter('v:val =~# "txt$"')

(see :h method, :h v:oldfiles, :h v:val and :h =~#)

I want to create a custom command / function combination that wraps the :echo and still accepts the v:oldfiles->copy()->filter('v:val =~# "txt$"') argument like in the :echo did.

I have this:

command! -bar -complete=expression -nargs=1 Foo call FooFunc(<args>)

function! FooFunc(x) 
    echo a:x
endfunction

However, invoking :Foo v:oldfiles->copy()->filter('v:val =~# "txt$"') results in the error:

E115: Missing quote: 'v:val =~#

How can I make it work like the :echo?

2
  1. For this particular task it's much much easier to do simply

    :filter /txt$/ oldfiles
    
  2. The error is due to -bar argument: the quote is parsed like a comment sign, and so the rest of the command is lost (see :h :command-bar, :h :quote).

Either always escape all quotes (and bars) with backslashes, or remove -bar argument from the command definition.

5
  • Wouldnt it also need to be execute 'echo' a:x? – D. Ben Knoble Sep 19 '20 at 13:01
  • 2
    @D.BenKnoble Here we have FooFunc(<args>), i.e. the argument is non-quoted. Hence an expression gets evaluated upon function call and no execute is needed. Of course, that could result in some other errors, such as :Foo 1,2 --> Too many parameters, etc.etc. – Matt Sep 19 '20 at 13:09
  • 1
    Huh! That's pretty interesting! But -bar is about recognizing | as a command separator, what does " as a comment character has to do with it? Why are these two connected? (I don't doubt that this is correct and indeed how it works, just wondering why it's that way...) – filbranden Sep 19 '20 at 13:23
  • For instance, :echo "a" | echo "b" works just fine... So it's akin to a user-defined command that has -bar, but it doesn't take " as comments... Yes, user-defined commands are somewhat different from built-ins, but still wondering why this is the way it is. – filbranden Sep 19 '20 at 13:26
  • 2
    @filbranden That's how it works: in a user command one can only switch on/off both simultaneously using "-bar". Not a case for internal commands such as :echo. So, yes, this is really frustrating :-(( – Matt Sep 19 '20 at 13:45

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.