Question

I want to have a function in my .vimrc that will run a bash script, check the output, and do different things depending on the bash script output.

Currently, my bash script returns "yes". My vim function is then:

function! Callscript()

  if 'r ! . test.sh' == 'yes'
    echom "returned yes"
  else
    echom "returned something else"
  endif
endfunction

This will always print "returned something else". When I run the command ':r ! . test.sh', it returns:

test.sh: line 5: return: yes: numeric argument required

I didn't see anything in :help r! that mentioned doing anything extra to get the return values, but I tried:

  if 'r ! . test.sh'.return == 'yes'
  if 'r ! . test.sh':return == 'yes'

which gave me errors. I have tried using system, but could not get that to work. The most obvious approach I tried with system was:

  if system('r ! . test.sh') == 'yes'

When I try system('. test.sh') in the editor, it tells me system is not an editor command.

How can I access the return value of the bash script so that I can use it in my vim function?

Was it helpful?

Solution

  if 'r ! . test.sh' == 'yes'

That syntax is nonsense. First, the :r !{cmd} is reading the output of {cmd} into the current buffer, which is probably not what you want. To get this into a variable, use :let output = system('test.sh'), or directly compare via:

if system('test.sh') == 'yes'

Note that comparing the output (especially if it's a true / false or success / failure boolean value as suggested by your question) is unusual. Rather, the script's exit status is checked (e.g. with commands like grep). You can get that in Vim via the special v:shell_error variable (which is filled by both system() and :r !).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top