16

I have a ~/.vimrc that contains just this:

function! NewFile()
        let filename = input("Filename:")
endfunction
command NewFile :call NewFile()<cr>

(of course my real .vimrc is more complex, but I've recreated this small test case with no plugins etc.)

My intent is to write a function that supports creating a new file according to a template. Some input items will be asked from the vi user, such as the name of the file.

The function isn't that sophisticated yet (understatement!) - all it does is ask for the filename. When I use the command NewFile from the vi command line, it starts, but then once I enter the filename and hit Enter, I get the error:

E488: Trailing characters

Why is that? What am I doing wrong?

21

Remove the trailing <cr> That is only needed for mappings, but not for commands.

  • Ah, perfect, thank you! I had copied this incantation from a mapping - a bit too closely, it seems. Thanks. – Andrew Ferrier Sep 9 '15 at 15:29
  • 1
    You solved me a problem with same error message with a different context. Thank you ^_^ – sensorario Jan 13 '17 at 13:55

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.