Vim: job_start: Bug or need for clarification: how to start a job with a blank in its arguments

Created on 7 Mar 2017  路  4Comments  路  Source: vim/vim

According to :h job_start "Arguments in double quotes can contain white space.". The docs don't tell how exactly arguments in double quotes are passed to the comnand.

Example:
R --slave --no-readline --silent --no-save -e "print(1 + 2)"prints 3 when run from the bash command line.

The following code:

function! Callback(ch, msg) abort "{{{3
    echom string(a:msg)
endf
let cmd = 'R --slave -e "print(1 + 2)"'
let job = job_start(cmd, {'in_mode': 'nl', 'out_mode': 'nl', 'callback': function('Callback')})
let ch = job_getchannel(job)
sleep 500ms

prints "print(1 + 2)" though, i.e. the argument gets escaped in a way that makes R think it's a string.

Either vim handles the argument wrongly or the docs need some clarification on how to pass an argument with whitespace to a command that is run as a job.

Most helpful comment

use this for a command string

let args = ['/bin/bash', '-c', cmd]

All 4 comments

Tom Link wrote:

According to :h job_start "Arguments in double quotes can contain
white space.". The docs don't tell how exactly arguments in double
quotes are passed to the comnand.

Example:
R --slave --no-readline --silent --no-save -e "print(1 + 2)"prints 3 when run from the bash command line.

The following code:

function! Callback(ch, msg) abort "{{{3
    echom string(a:msg)
endf
let cmd = 'R --slave -e "print(1 + 2)"'
let job = job_start(cmd, {'in_mode': 'nl', 'out_mode': 'nl', 'callback': function('Callback')})
let ch = job_getchannel(job)
sleep 500ms

prints "print(1 + 2)" though, i.e. the argument gets escaped in a way that makes R think it's a string.

Either vim handles the argument wrongly or the docs need some clarification on how to pass an argument with whitespace to a command that is run as a job.

What system is this on? For Unix it's recommended to use a list instead
of a string.

--
From "know your smileys":
:-& Eating spaghetti

/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \
\ an exciting new programming language -- http://www.Zimbu.org ///
\ help me help AIDS victims -- http://ICCF-Holland.org ///

What system is this on? For Unix it's recommended to use a list instead
of a string.

This is on ubuntu 16.04.

Ok, using let cmd = ['R', '--slave', '-e', 'print(1 + 2)'] actually
works. If I understand you right, it won't work on windows though? So,
for windows, I'd have to join the list and wrap an argument containing
special characters in quotes?

Anyway, I now pass the expression as input to the inferior process,
which works on linux & windows.

Thanks for your reply.

Tom Link wrote:

What system is this on? For Unix it's recommended to use a list instead
of a string.

This is on ubuntu 16.04.

Ok, using let cmd = ['R', '--slave', '-e', 'print(1 + 2)'] actually
works. If I understand you right, it won't work on windows though? So,
for windows, I'd have to join the list and wrap an argument containing
special characters in quotes?

Anyway, I now pass the expression as input to the inferior process,
which works on linux & windows.

The problem is that Unix and MS-Windows have very different ways of
starting an executable. Libraries hide that a bit, but when it comes to
escaping special characters it often fails. Also because on Windows the
backslash is a path separator.

Using the list is probably the best, then Vim can try hard to do proper
escaping. But how an executable unescapes is not always the same...

Anyway, I think we should use the list on Unix as the reference, and try
to make that also work on MS-Windows.

--
The primary purpose of the DATA statement is to give names to constants;
instead of referring to pi as 3.141592653589793 at every appearance, the
variable PI can be given that value with a DATA statement and used instead
of the longer form of the constant. This also simplifies modifying the
program, should the value of pi change.
-- FORTRAN manual for Xerox Computers

/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \
\ an exciting new programming language -- http://www.Zimbu.org ///
\ help me help AIDS victims -- http://ICCF-Holland.org ///

use this for a command string

let args = ['/bin/bash', '-c', cmd]
Was this page helpful?
0 / 5 - 0 ratings