skamsie
skamsie

Reputation: 2726

Get rid of ^@ in vim output from system(...)

I am trying to add the result of a system() call as a header for one of the plugins I use (vim-startify). The command is:

system('vim --version | head -1')

However when it gets printed it has an extra character in the end: ^@

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Feb 12 2017 23:20:57)^@

enter image description here

There is a similar question here, but it addresses a file, not a buffer.

Upvotes: 1

Views: 233

Answers (3)

Luc Hermitte
Luc Hermitte

Reputation: 32976

Just chomp it: system('whatever')[:-2], or, if you prefer something more robust—something that won't misbehave should the newline be missing— you can apply: substitute(system_result, "\n\\+$", "", "").

Note that, unlike tr, this solution works on all platforms. That's what we used to do with the chomp() function in Perl.

Upvotes: 2

Sato Katsura
Sato Katsura

Reputation: 3086

With Vim 8 (or late 7.4 versions):

get(systemlist('vim --version'), 0)

Upvotes: 2

romainl
romainl

Reputation: 196906

This should give you what you want:

system('vim --version | head -1 | tr -d "\n"')

See $ man tr.

Upvotes: 1

Related Questions