6

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()}\ 

2 Answers 2

9

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().

0
8

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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