What a coincidence. I encountered this exact same thing a few hours before you posted this. I just figured it was a line-ending issue and solved it by wrapping the system()
call in a call to trim()
.
Since you mentioned it I since verified that the cause is the presence of a newline in the output of system()
.
So you can solve this with a trim()
call that directly addresses the problem like so...
trim(system(...), "\n")
If you are using an older version of Vim which doesn't have trim()
you can use something like this instead...
let foo = system(...)
let foo = strcharpart(foo, 0, strchars(foo)-1))
Or this...
let foo = substitute(system(...), "\n$", "", "")
Update: I should point out that this isn't a bug or anything. When running a command from the shell you'll get a newline at the end and that's expected. system()
does the right thing and passes everything back to the caller. It just so happens that there's a fairly common use case, assigning the result to a variable, where we don't want that newline.
(Note: One thing that might not be expected is that Vim normalizes line-endings on non-unix systems so, for example, Windows CRLF becomes LF. It makes sense but one needs to be aware of it.)