Channel commands: special characters, quotes and escaping

45 views
Skip to first unread message

Jan Bundesmann

unread,
May 24, 2016, 2:14:46 AM5/24/16
to vim_use
Hi list!

I'm currently playing around with the new channel support and came across something I do not fully understand. Playing around with the demoserver.py I tried to send a command to my vim-instance via feedkeys. The original command was:

:call feedkeys("ggOHello, world!\e")

From within vim this works. I tried to enter it in the server stdin as:

["ex", "..."]

and had to fight with the nested quotes. The special character "\e" is only accepted within double-quotes. So I tried to include the whole call-command in single quotes which is not accepted by the default handler leaving me with

["ex", 'call feedkeys("ggOHello, world!\e")']
> Decoding failed - discarding input

in the ch_log. A working version is

["ex", "call feedkeys(\"ggOHello, world!\\e\")"]

with a lot of escapes. I do not understand why the first solution does not work.

Tanks
Jan

Bram Moolenaar

unread,
May 24, 2016, 5:31:56 AM5/24/16
to 'Jan Bundesmann' via vim_use
Keep in mind this is in Python. And in Python a backslash inside a
single quoted string has a special meaning, unlike Vim.

I think this should also work:

["ex", 'call feedkeys("ggOHello, world!\\e")']

The text inside single quotes is a Python string, where the actual
resulting text is:
call feedkeys("ggOHello, world!\e")

And then Vim will interpret the \e in double quotes as an escape.


--
Bad fashion can discourage normal people from interacting with the engineer
and talking about the cute things their children do.
(Scott Adams - The Dilbert principle)

/// Bram Moolenaar -- Br...@Moolenaar.net -- 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 ///

Jan Bundesmann

unread,
May 25, 2016, 7:19:35 AM5/25/16
to vim_use
> I think this should also work:
>
> ["ex", 'call feedkeys("ggOHello, world!\\e")']
Nope, it does not. Channel log says:
82.925983 RECV on 0: '["ex", 'call feedkeys("ggOHello, world!\\e")']'
82.926009 ERR on 0: Decoding failed - discarding input

> Keep in mind this is in Python. And in Python a backslash inside a
> single quoted string has a special meaning, unlike Vim.

I still think, it's somehow related to the handler. Or is this, too, written in Python?

Out of curiousity, I tried the same but with a simple socket, established via "netcat".
["ex", "call feedkeys(\"ggOHello, world!\\e\")"] works
["ex", 'call feedkeys("ggOHello, world!\\e")'] does not

Nikolay Aleksandrovich Pavlov

unread,
May 25, 2016, 9:02:38 AM5/25/16
to vim...@googlegroups.com
2016-05-25 14:19 GMT+03:00 'Jan Bundesmann' via vim_use
<vim...@googlegroups.com>:
>> I think this should also work:
>>
>> ["ex", 'call feedkeys("ggOHello, world!\\e")']
> Nope, it does not. Channel log says:
> 82.925983 RECV on 0: '["ex", 'call feedkeys("ggOHello, world!\\e")']'
> 82.926009 ERR on 0: Decoding failed - discarding input
>
>> Keep in mind this is in Python. And in Python a backslash inside a
>> single quoted string has a special meaning, unlike Vim.
> I still think, it's somehow related to the handler. Or is this, too, written in Python?
>
> Out of curiousity, I tried the same but with a simple socket, established via "netcat".

I have no idea why Bram said about Python. Unless you stated otherwise
messages in channel are in JSON and JSON has no single quoted literal
strings, so your example *must* fail because it is not a valid JSON.
There are also other modes, but if I understand `:h channel-commands`
correctly you will have to write handlers for ex, etc yourself if you
use another mode.

> ["ex", "call feedkeys(\"ggOHello, world!\\e\")"] works
> ["ex", 'call feedkeys("ggOHello, world!\\e")'] does not
>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jan Bundesmann

unread,
May 25, 2016, 9:35:34 AM5/25/16
to vim_use
> JSON has no single quoted literal
> strings, so your example *must* fail because it is not a valid JSON.

I think, this is the answer.

Maybe learning first how to write valid JSON is a good idea when playing around with channels. :-p

Thanks
Jan

Bram Moolenaar

unread,
May 25, 2016, 4:49:52 PM5/25/16
to 'Jan Bundesmann' via vim_use

Jan Bundesmann wrote:

> > I think this should also work:
> >
> > ["ex", 'call feedkeys("ggOHello, world!\\e")']
> Nope, it does not. Channel log says:
> 82.925983 RECV on 0: '["ex", 'call feedkeys("ggOHello, world!\\e")']'
> 82.926009 ERR on 0: Decoding failed - discarding input

Oh wait, this is JSON and JSON only accepts double quoted strings.

> > Keep in mind this is in Python. And in Python a backslash inside a
> > single quoted string has a special meaning, unlike Vim.
> I still think, it's somehow related to the handler. Or is this, too,
> written in Python?
>
> Out of curiousity, I tried the same but with a simple socket, established via "netcat".
> ["ex", "call feedkeys(\"ggOHello, world!\\e\")"] works
> ["ex", 'call feedkeys("ggOHello, world!\\e")'] does not

Yes, you must use double quoted strings. And then take care of what
Python does with the backslashes.

--
hundred-and-one symptoms of being an internet addict:
10. And even your night dreams are in HTML.

Nikolay Aleksandrovich Pavlov

unread,
May 25, 2016, 6:43:21 PM5/25/16
to vim...@googlegroups.com
2016-05-25 16:35 GMT+03:00 'Jan Bundesmann' via vim_use
<vim...@googlegroups.com>:
Most of time JSON is not written “by hand”, it is created by some
library functions. E.g. Python has `json` standard (almost always
installed with the interpreter itself) module, VimL has built-in
`json_encode`, etc: many modern languages have this either “built-in”
or “in standard library”, exceptions are usually positioning
themselves as “system” (like Rust or C) or “light-weight” (like lua)
languages. These functions would not create invalid JSON, they accept
native data types and they may be optimized, so 99% of time it is
better to use them even if you are absolutely sure you can write valid
JSON by hand.

>
> Thanks
> Jan

Jan Bundesmann

unread,
May 26, 2016, 3:52:43 AM5/26/16
to vim_use
On Thursday, May 26, 2016 at 12:43:21 AM UTC+2, ZyX wrote:
> Most of time JSON is not written “by hand”, it is created by some
> library functions. E.g. Python has `json` standard (almost always
> installed with the interpreter itself) module, VimL has built-in
> `json_encode`, etc: many modern languages have this either “built-in”
> or “in standard library”, exceptions are usually positioning
> themselves as “system” (like Rust or C) or “light-weight” (like lua)
> languages. These functions would not create invalid JSON, they accept
> native data types and they may be optimized, so 99% of time it is
> better to use them even if you are absolutely sure you can write valid
> JSON by hand.
Thanks for that. For writing services/plugins/whatever I would, of course, rely on those libraries. I simply didn't have the need to do so in the past. That's why my knowledge about JSON is rather limited.

Right now, I have a particular use case - I'm playing with the new functions for a review of the upcoming version. So, I was looking for a simple example to be put in the article. Basically, I'm satisfied with telling the readers the default handler expects JSON.
Reply all
Reply to author
Forward
0 new messages