When I call this function inside vim statusline it returns this nvim^@.weird character, What is the meaning of ^@ in returned value and how to get rid of it?

function! GetEditor()
  let s:name = system("echo $EDITOR")
  return s:name
endfunction

set statusline+=%0*\ %{GetEditor()}\ 
up vote 3 down vote accepted

The ^@ is a NUL character, 0x00, which Vim uses in certain cases to store a newline, 0x0a.

Unix commands usually terminate their output with a newline, and the system() function captures that newline in the output of your echo command.

To get rid of that terminating newline, use substitute():

let s:name = substitute(system("echo $EDITOR"), '\n', '', 'g')

That substitute() call replaces newlines ('\n') in the output of system() with nothing (''), and does so globally ('g'), i.e., to all newlines in the output of system().

You got an answer already. However, for your specific case, you don't need the system() call, since you want an environment variable to be expanded. In that case you could simply use echo $EDITOR or in case of a function call do:

function! GetEditor()
    return $EDITOR
endfunction

Now, suppose you still need a system() function call, because you need to return the output of a system specific command. If you know, the output will only have a simple line (e.g. only one single linebreak), you could simply return the output with the last byte (the linebreak) removed:

return system("yes |head -1")[:-2]

which uses the expr[] syntax to strip the last byte from the output.


Alternatively, you could make use of the systemlist() function which automatically converts the output of the system command into a vim list, so you do not have to take care of those trailing linebreaks:

return systemlist("echo $EDITOR")

See also the similar answer on superuser

  • Thanks i'll try that, i'm still just fiddling around with my vimrc.. +1 for informative explanation.. – John Fred Fadrigalan Oct 24 at 1:57

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.