blob: 18b0232dfac9826a62acdd223011ecf6059073eb [file] [log] [blame]
Bram Moolenaar077cc7a2020-09-04 16:35:35 +02001*channel.txt* For Vim version 8.2. Last change: 2020 Sep 03
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7 Inter-process communication *channel*
8
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009Vim uses channels to communicate with other processes.
Bram Moolenaar269f5952016-07-15 22:54:41 +020010A channel uses a socket or pipes. *socket-interface*
Bram Moolenaar38a55632016-02-15 22:07:32 +010011Jobs can be used to start processes and communicate with them.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010012The Netbeans interface also uses a channel. |netbeans|
13
Bram Moolenaar38a55632016-02-15 22:07:32 +0100141. Overview |job-channel-overview|
152. Channel demo |channel-demo|
163. Opening a channel |channel-open|
174. Using a JSON or JS channel |channel-use|
185. Channel commands |channel-commands|
196. Using a RAW or NL channel |channel-raw|
207. More channel functions |channel-more|
Bram Moolenaar54775062019-07-31 21:07:14 +0200218. Channel functions details |channel-functions-details|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200229. Starting a job with a channel |job-start|
2310. Starting a job without a channel |job-start-nochannel|
2411. Job functions |job-functions-details|
2512. Job options |job-options|
2613. Controlling a job |job-control|
2714. Using a prompt buffer |prompt-buffer|
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010028
Bram Moolenaar38a55632016-02-15 22:07:32 +010029{only when compiled with the |+channel| feature for channel stuff}
Bram Moolenaarf37506f2016-08-31 22:22:10 +020030 You can check this with: `has('channel')`
Bram Moolenaar38a55632016-02-15 22:07:32 +010031{only when compiled with the |+job| feature for job stuff}
Bram Moolenaarf37506f2016-08-31 22:22:10 +020032 You can check this with: `has('job')`
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010033
34==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +0100351. Overview *job-channel-overview*
36
37There are four main types of jobs:
Bram Moolenaar50ba5262016-09-22 22:33:02 +0200381. A daemon, serving several Vim instances.
Bram Moolenaar38a55632016-02-15 22:07:32 +010039 Vim connects to it with a socket.
402. One job working with one Vim instance, asynchronously.
41 Uses a socket or pipes.
423. A job performing some work for a short time, asynchronously.
43 Uses a socket or pipes.
444. Running a filter, synchronously.
45 Uses pipes.
46
Bram Moolenaar77cdfd12016-03-12 12:57:59 +010047For when using sockets See |job-start|, |job-start-nochannel| and
48|channel-open|. For 2 and 3, one or more jobs using pipes, see |job-start|.
Bram Moolenaar38a55632016-02-15 22:07:32 +010049For 4 use the ":{range}!cmd" command, see |filter|.
50
51Over the socket and pipes these protocols are available:
52RAW nothing known, Vim cannot tell where a message ends
53NL every message ends in a NL (newline) character
54JSON JSON encoding |json_encode()|
55JS JavaScript style JSON-like encoding |js_encode()|
56
57Common combination are:
58- Using a job connected through pipes in NL mode. E.g., to run a style
59 checker and receive errors and warnings.
Bram Moolenaar7dda86f2018-04-20 22:36:41 +020060- Using a daemon, connecting over a socket in JSON mode. E.g. to lookup
Bram Moolenaar09521312016-08-12 22:54:35 +020061 cross-references in a database.
Bram Moolenaar38a55632016-02-15 22:07:32 +010062
63==============================================================================
Bram Moolenaar26852122016-05-24 20:02:38 +0200642. Channel demo *channel-demo* *demoserver.py*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010065
66This requires Python. The demo program can be found in
67$VIMRUNTIME/tools/demoserver.py
68Run it in one terminal. We will call this T1.
69
70Run Vim in another terminal. Connect to the demo server with: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010071 let channel = ch_open('localhost:8765')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010072
73In T1 you should see:
74 === socket opened === ~
75
76You can now send a message to the server: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010077 echo ch_evalexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010078
79The message is received in T1 and a response is sent back to Vim.
80You can see the raw messages in T1. What Vim sends is:
81 [1,"hello!"] ~
82And the response is:
83 [1,"got it"] ~
84The number will increase every time you send a message.
85
86The server can send a command to Vim. Type this on T1 (literally, including
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010087the quotes):
88 ["ex","echo 'hi there'"] ~
89And you should see the message in Vim. You can move the cursor a word forward:
90 ["normal","w"] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010091
92To handle asynchronous communication a callback needs to be used: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010093 func MyHandler(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010094 echo "from the handler: " . a:msg
95 endfunc
Bram Moolenaar02e83b42016-02-21 20:10:26 +010096 call ch_sendexpr(channel, 'hello!', {'callback': "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +010097Vim will not wait for a response. Now the server can send the response later
98and MyHandler will be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010099
100Instead of giving a callback with every send call, it can also be specified
101when opening the channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100102 call ch_close(channel)
103 let channel = ch_open('localhost:8765', {'callback': "MyHandler"})
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100104 call ch_sendexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100105
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100106When trying out channels it's useful to see what is going on. You can tell
107Vim to write lines in log file: >
108 call ch_logfile('channellog', 'w')
109See |ch_logfile()|.
110
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100111==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01001123. Opening a channel *channel-open*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100113
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100114To open a channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100115 let channel = ch_open({address} [, {options}])
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100116 if ch_status(channel) == "open"
117 " use the channel
Bram Moolenaar38a55632016-02-15 22:07:32 +0100118
119Use |ch_status()| to see if the channel could be opened.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100120
121{address} has the form "hostname:port". E.g., "localhost:8765".
122
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200123When using an IPv6 address, enclose it within square brackets. E.g.,
124"[2001:db8::1]:8765".
125
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100126{options} is a dictionary with optional entries: *channel-open-options*
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100127
128"mode" can be: *channel-mode*
129 "json" - Use JSON, see below; most convenient way. Default.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100130 "js" - Use JS (JavaScript) encoding, more efficient than JSON.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100131 "nl" - Use messages that end in a NL character
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100132 "raw" - Use raw messages
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100133 *channel-callback* *E921*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100134"callback" A function that is called when a message is received that is
135 not handled otherwise. It gets two arguments: the channel
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100136 and the received message. Example: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100137 func Handle(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100138 echo 'Received: ' . a:msg
139 endfunc
Bram Moolenaar38a55632016-02-15 22:07:32 +0100140 let channel = ch_open("localhost:8765", {"callback": "Handle"})
141<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100142 When "mode" is "json" or "js" the "msg" argument is the body
143 of the received message, converted to Vim types.
144 When "mode" is "nl" the "msg" argument is one message,
145 excluding the NL.
146 When "mode" is "raw" the "msg" argument is the whole message
147 as a string.
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100148
149 For all callbacks: Use |function()| to bind it to arguments
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100150 and/or a Dictionary. Or use the form "dict.function" to bind
151 the Dictionary.
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200152
153 Callbacks are only called at a "safe" moment, usually when Vim
154 is waiting for the user to type a character. Vim does not use
155 multi-threading.
156
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100157 *close_cb*
158"close_cb" A function that is called when the channel gets closed, other
Bram Moolenaar38a55632016-02-15 22:07:32 +0100159 than by calling ch_close(). It should be defined like this: >
160 func MyCloseHandler(channel)
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200161< Vim will invoke callbacks that handle data before invoking
162 close_cb, thus when this function is called no more data will
Bram Moolenaar68e65602019-05-26 21:33:31 +0200163 be passed to the callbacks. However, if a callback causes Vim
164 to check for messages, the close_cb may be invoked while still
165 in the callback. The plugin must handle this somehow, it can
166 be useful to know that no more data is coming.
Bram Moolenaar958dc692016-12-01 15:34:12 +0100167 *channel-drop*
168"drop" Specifies when to drop messages:
169 "auto" When there is no callback to handle a message.
170 The "close_cb" is also considered for this.
171 "never" All messages will be kept.
172
Bram Moolenaar0b146882018-09-06 16:27:24 +0200173 *channel-noblock*
174"noblock" Same effect as |job-noblock|. Only matters for writing.
175
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200176 *waittime*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100177"waittime" The time to wait for the connection to be made in
Bram Moolenaarf3913272016-02-25 00:00:01 +0100178 milliseconds. A negative number waits forever.
179
180 The default is zero, don't wait, which is useful if a local
181 server is supposed to be running already. On Unix Vim
182 actually uses a 1 msec timeout, that is required on many
183 systems. Use a larger value for a remote server, e.g. 10
184 msec at least.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100185 *channel-timeout*
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100186"timeout" The time to wait for a request when blocking, E.g. when using
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100187 ch_evalexpr(). In milliseconds. The default is 2000 (2
Bram Moolenaar38a55632016-02-15 22:07:32 +0100188 seconds).
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100189
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100190When "mode" is "json" or "js" the "callback" is optional. When omitted it is
191only possible to receive a message after sending one.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100192
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100193To change the channel options after opening it use |ch_setoptions()|. The
194arguments are similar to what is passed to |ch_open()|, but "waittime" cannot
195be given, since that only applies to opening the channel.
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100196
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100197For example, the handler can be added or changed: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100198 call ch_setoptions(channel, {'callback': callback})
199When "callback" is empty (zero or an empty string) the handler is removed.
200
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100201After a callback has been invoked Vim will update the screen and put the
202cursor back where it belongs. Thus the callback should not need to do
203`:redraw`.
204
Bram Moolenaar38a55632016-02-15 22:07:32 +0100205The timeout can be changed: >
206 call ch_setoptions(channel, {'timeout': msec})
207<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100208 *channel-close* *E906*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100209Once done with the channel, disconnect it like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100210 call ch_close(channel)
211When a socket is used this will close the socket for both directions. When
212pipes are used (stdin/stdout/stderr) they are all closed. This might not be
213what you want! Stopping the job with job_stop() might be better.
Bram Moolenaar187db502016-02-27 14:44:26 +0100214All readahead is discarded, callbacks will no longer be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100215
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100216Note that a channel is closed in three stages:
217 - The I/O ends, log message: "Closing channel". There can still be queued
218 messages to read or callbacks to invoke.
219 - The readahead is cleared, log message: "Clearing channel". Some variables
220 may still reference the channel.
221 - The channel is freed, log message: "Freeing channel".
222
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100223When the channel can't be opened you will get an error message. There is a
224difference between MS-Windows and Unix: On Unix when the port doesn't exist
225ch_open() fails quickly. On MS-Windows "waittime" applies.
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200226*E898* *E901* *E902*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100227
228If there is an error reading or writing a channel it will be closed.
Bram Moolenaar664f3cf2019-12-07 16:03:51 +0100229*E630* *E631*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100230
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100231==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002324. Using a JSON or JS channel *channel-use*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100233
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100234If mode is JSON then a message can be sent synchronously like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100235 let response = ch_evalexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100236This awaits a response from the other side.
237
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100238When mode is JS this works the same, except that the messages use
Bram Moolenaar38a55632016-02-15 22:07:32 +0100239JavaScript encoding. See |js_encode()| for the difference.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100240
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100241To send a message, without handling a response or letting the channel callback
242handle the response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100243 call ch_sendexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100244
245To send a message and letting the response handled by a specific function,
246asynchronously: >
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100247 call ch_sendexpr(channel, {expr}, {'callback': Handler})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100248
249Vim will match the response with the request using the message ID. Once the
250response is received the callback will be invoked. Further responses with the
251same ID will be ignored. If your server sends back multiple responses you
252need to send them with ID zero, they will be passed to the channel callback.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100253
254The {expr} is converted to JSON and wrapped in an array. An example of the
255message that the receiver will get when {expr} is the string "hello":
256 [12,"hello"] ~
257
258The format of the JSON sent is:
259 [{number},{expr}]
260
261In which {number} is different every time. It must be used in the response
262(if any):
263
264 [{number},{response}]
265
266This way Vim knows which sent message matches with which received message and
267can call the right handler. Also when the messages arrive out of order.
268
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200269A newline character is terminating the JSON text. This can be used to
270separate the read text. For example, in Python:
271 splitidx = read_text.find('\n')
272 message = read_text[:splitidx]
273 rest = read_text[splitidx + 1:]
274
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100275The sender must always send valid JSON to Vim. Vim can check for the end of
276the message by parsing the JSON. It will only accept the message if the end
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200277was received. A newline after the message is optional.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100278
279When the process wants to send a message to Vim without first receiving a
280message, it must use the number zero:
281 [0,{response}]
282
283Then channel handler will then get {response} converted to Vim types. If the
284channel does not have a handler the message is dropped.
285
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100286It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS
287channel. The caller is then completely responsible for correct encoding and
288decoding.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100289
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100290==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002915. Channel commands *channel-commands*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100292
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100293With a JSON channel the process can send commands to Vim that will be
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100294handled by Vim internally, it does not require a handler for the channel.
295
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100296Possible commands are: *E903* *E904* *E905*
Bram Moolenaar220adb12016-09-12 12:17:26 +0200297 ["redraw", {forced}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100298 ["ex", {Ex command}]
299 ["normal", {Normal mode command}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100300 ["expr", {expression}, {number}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100301 ["expr", {expression}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100302 ["call", {func name}, {argument list}, {number}]
303 ["call", {func name}, {argument list}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100304
305With all of these: Be careful what these commands do! You can easily
306interfere with what the user is doing. To avoid trouble use |mode()| to check
307that the editor is in the expected state. E.g., to send keys that must be
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100308inserted as text, not executed as a command:
309 ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~
310
311Errors in these commands are normally not reported to avoid them messing up
312the display. If you do want to see them, set the 'verbose' option to 3 or
313higher.
314
315
316Command "redraw" ~
317
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100318The other commands do not explicitly update the screen, so that you can send a
319sequence of commands without the cursor moving around. A redraw can happen as
320a side effect of some commands. You must end with the "redraw" command to
321show any changed text and show the cursor where it belongs.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100322
323The argument is normally an empty string:
324 ["redraw", ""] ~
325To first clear the screen pass "force":
326 ["redraw", "force"] ~
327
328
329Command "ex" ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100330
331The "ex" command is executed as any Ex command. There is no response for
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100332completion or error. You could use functions in an |autoload| script:
333 ["ex","call myscript#MyFunc(arg)"]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100334
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100335You can also use "call |feedkeys()|" to insert any key sequence.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100336
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100337When there is an error a message is written to the channel log, if it exists,
338and v:errmsg is set to the error.
339
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100340
341Command "normal" ~
342
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100343The "normal" command is executed like with ":normal!", commands are not
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100344mapped. Example to open the folds under the cursor:
345 ["normal" "zO"]
346
347
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100348Command "expr" with response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100349
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100350The "expr" command can be used to get the result of an expression. For
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100351example, to get the number of lines in the current buffer:
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100352 ["expr","line('$')", -2] ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100353
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100354It will send back the result of the expression:
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100355 [-2, "last line"] ~
356The format is:
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100357 [{number}, {result}]
Bram Moolenaar187db502016-02-27 14:44:26 +0100358
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100359Here {number} is the same as what was in the request. Use a negative number
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100360to avoid confusion with message that Vim sends. Use a different number on
361every request to be able to match the request with the response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100362
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100363{result} is the result of the evaluation and is JSON encoded. If the
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100364evaluation fails or the result can't be encoded in JSON it is the string
365"ERROR".
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100366
367
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100368Command "expr" without a response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100369
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100370This command is similar to "expr" above, but does not send back any response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100371Example:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100372 ["expr","setline('$', ['one', 'two', 'three'])"] ~
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100373There is no third argument in the request.
374
375
376Command "call" ~
377
378This is similar to "expr", but instead of passing the whole expression as a
379string this passes the name of a function and a list of arguments. This
380avoids the conversion of the arguments to a string and escaping and
381concatenating them. Example:
382 ["call", "line", ["$"], -2] ~
383
384Leave out the fourth argument if no response is to be sent:
385 ["call", "setline", ["$", ["one", "two", "three"]]] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100386
387==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003886. Using a RAW or NL channel *channel-raw*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100389
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100390If mode is RAW or NL then a message can be sent like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100391 let response = ch_evalraw(channel, {string})
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100392
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100393The {string} is sent as-is. The response will be what can be read from the
394channel right away. Since Vim doesn't know how to recognize the end of the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100395message you need to take care of it yourself. The timeout applies for reading
396the first byte, after that it will not wait for anything more.
397
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100398If mode is "nl" you can send a message in a similar way. You are expected
Bram Moolenaar38a55632016-02-15 22:07:32 +0100399to put in the NL after each message. Thus you can also send several messages
400ending in a NL at once. The response will be the text up to and including the
401first NL. This can also be just the NL for an empty response.
402If no NL was read before the channel timeout an empty string is returned.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100403
404To send a message, without expecting a response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100405 call ch_sendraw(channel, {string})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100406The process can send back a response, the channel handler will be called with
407it.
408
409To send a message and letting the response handled by a specific function,
410asynchronously: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100411 call ch_sendraw(channel, {string}, {'callback': 'MyHandler'})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100412
Bram Moolenaar38a55632016-02-15 22:07:32 +0100413This {string} can also be JSON, use |json_encode()| to create it and
414|json_decode()| to handle a received JSON message.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100415
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100416It is not possible to use |ch_evalexpr()| or |ch_sendexpr()| on a raw channel.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100417
Bram Moolenaar818078d2016-08-27 21:58:42 +0200418A String in Vim cannot contain NUL bytes. To send or receive NUL bytes read
419or write from a buffer. See |in_io-buffer| and |out_io-buffer|.
420
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100421==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01004227. More channel functions *channel-more*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100423
Bram Moolenaar38a55632016-02-15 22:07:32 +0100424To obtain the status of a channel: ch_status(channel). The possible results
425are:
426 "fail" Failed to open the channel.
427 "open" The channel can be used.
Bram Moolenaar06481422016-04-30 15:13:38 +0200428 "buffered" The channel was closed but there is data to read.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100429 "closed" The channel was closed.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100430
Bram Moolenaar187db502016-02-27 14:44:26 +0100431To obtain the job associated with a channel: ch_getjob(channel)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100432
Bram Moolenaar38a55632016-02-15 22:07:32 +0100433To read one message from a channel: >
434 let output = ch_read(channel)
435This uses the channel timeout. To read without a timeout, just get any
436message that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100437 let output = ch_read(channel, {'timeout': 0})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100438When no message was available then the result is v:none for a JSON or JS mode
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100439channels, an empty string for a RAW or NL channel. You can use |ch_canread()|
440to check if there is something to read.
441
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200442Note that when there is no callback, messages are dropped. To avoid that add
443a close callback to the channel.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100444
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100445To read all output from a RAW channel that is available: >
446 let output = ch_readraw(channel)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100447To read the error output: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100448 let output = ch_readraw(channel, {"part": "err"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100449
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100450ch_read() and ch_readraw() use the channel timeout. When there is nothing to
451read within that time an empty string is returned. To specify a different
452timeout in msec use the "timeout" option:
453 {"timeout": 123} ~
454To read from the error output use the "part" option:
455 {"part": "err"} ~
456To read a message with a specific ID, on a JS or JSON channel:
457 {"id": 99} ~
458When no ID is specified or the ID is -1, the first message is returned. This
459overrules any callback waiting for this message.
460
461For a RAW channel this returns whatever is available, since Vim does not know
462where a message ends.
463For a NL channel this returns one message.
464For a JS or JSON channel this returns one decoded message.
465This includes any sequence number.
466
Bram Moolenaar38a55632016-02-15 22:07:32 +0100467==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02004688. Channel functions details *channel-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200469
470ch_canread({handle}) *ch_canread()*
471 Return non-zero when there is something to read from {handle}.
472 {handle} can be a Channel or a Job that has a Channel.
473
474 This is useful to read from a channel at a convenient time,
475 e.g. from a timer.
476
477 Note that messages are dropped when the channel does not have
478 a callback. Add a close callback to avoid that.
479
Bram Moolenaar570497a2019-08-22 22:55:13 +0200480 Can also be used as a |method|: >
481 GetChannel()->ch_canread()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200482
483ch_close({handle}) *ch_close()*
484 Close {handle}. See |channel-close|.
485 {handle} can be a Channel or a Job that has a Channel.
486 A close callback is not invoked.
487
Bram Moolenaar570497a2019-08-22 22:55:13 +0200488 Can also be used as a |method|: >
489 GetChannel()->ch_close()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200490
491ch_close_in({handle}) *ch_close_in()*
492 Close the "in" part of {handle}. See |channel-close-in|.
493 {handle} can be a Channel or a Job that has a Channel.
494 A close callback is not invoked.
495
Bram Moolenaar570497a2019-08-22 22:55:13 +0200496 Can also be used as a |method|: >
497 GetChannel()->ch_close_in()
498
Bram Moolenaared997ad2019-07-21 16:42:00 +0200499
500ch_evalexpr({handle}, {expr} [, {options}]) *ch_evalexpr()*
501 Send {expr} over {handle}. The {expr} is encoded
502 according to the type of channel. The function cannot be used
503 with a raw channel. See |channel-use|.
504 {handle} can be a Channel or a Job that has a Channel.
505 *E917*
506 {options} must be a Dictionary. It must not have a "callback"
507 entry. It can have a "timeout" entry to specify the timeout
508 for this specific request.
509
510 ch_evalexpr() waits for a response and returns the decoded
511 expression. When there is an error or timeout it returns an
512 empty string.
513
Bram Moolenaar8fe10002019-09-11 22:56:44 +0200514 Note that while waiting for the response, Vim handles other
515 messages. You need to make sure this doesn't cause trouble.
516
Bram Moolenaar570497a2019-08-22 22:55:13 +0200517 Can also be used as a |method|: >
518 GetChannel()->ch_evalexpr(expr)
519
Bram Moolenaared997ad2019-07-21 16:42:00 +0200520
521ch_evalraw({handle}, {string} [, {options}]) *ch_evalraw()*
522 Send {string} over {handle}.
523 {handle} can be a Channel or a Job that has a Channel.
524
525 Works like |ch_evalexpr()|, but does not encode the request or
526 decode the response. The caller is responsible for the
527 correct contents. Also does not add a newline for a channel
528 in NL mode, the caller must do that. The NL in the response
529 is removed.
530 Note that Vim does not know when the text received on a raw
531 channel is complete, it may only return the first part and you
532 need to use |ch_readraw()| to fetch the rest.
533 See |channel-use|.
534
Bram Moolenaar570497a2019-08-22 22:55:13 +0200535 Can also be used as a |method|: >
536 GetChannel()->ch_evalraw(rawstring)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200537
538ch_getbufnr({handle}, {what}) *ch_getbufnr()*
539 Get the buffer number that {handle} is using for {what}.
540 {handle} can be a Channel or a Job that has a Channel.
541 {what} can be "err" for stderr, "out" for stdout or empty for
542 socket output.
543 Returns -1 when there is no buffer.
544
Bram Moolenaar570497a2019-08-22 22:55:13 +0200545 Can also be used as a |method|: >
546 GetChannel()->ch_getbufnr(what)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200547
548ch_getjob({channel}) *ch_getjob()*
549 Get the Job associated with {channel}.
550 If there is no job calling |job_status()| on the returned Job
551 will result in "fail".
552
Bram Moolenaar570497a2019-08-22 22:55:13 +0200553 Can also be used as a |method|: >
554 GetChannel()->ch_getjob()
555
Bram Moolenaared997ad2019-07-21 16:42:00 +0200556
557ch_info({handle}) *ch_info()*
558 Returns a Dictionary with information about {handle}. The
559 items are:
560 "id" number of the channel
561 "status" "open", "buffered" or "closed", like
562 ch_status()
563 When opened with ch_open():
564 "hostname" the hostname of the address
565 "port" the port of the address
566 "sock_status" "open" or "closed"
567 "sock_mode" "NL", "RAW", "JSON" or "JS"
568 "sock_io" "socket"
569 "sock_timeout" timeout in msec
570 When opened with job_start():
571 "out_status" "open", "buffered" or "closed"
572 "out_mode" "NL", "RAW", "JSON" or "JS"
573 "out_io" "null", "pipe", "file" or "buffer"
574 "out_timeout" timeout in msec
575 "err_status" "open", "buffered" or "closed"
576 "err_mode" "NL", "RAW", "JSON" or "JS"
577 "err_io" "out", "null", "pipe", "file" or "buffer"
578 "err_timeout" timeout in msec
579 "in_status" "open" or "closed"
580 "in_mode" "NL", "RAW", "JSON" or "JS"
581 "in_io" "null", "pipe", "file" or "buffer"
582 "in_timeout" timeout in msec
583
Bram Moolenaar570497a2019-08-22 22:55:13 +0200584 Can also be used as a |method|: >
585 GetChannel()->ch_info()
586
Bram Moolenaared997ad2019-07-21 16:42:00 +0200587
588ch_log({msg} [, {handle}]) *ch_log()*
589 Write {msg} in the channel log file, if it was opened with
590 |ch_logfile()|.
591 When {handle} is passed the channel number is used for the
592 message.
593 {handle} can be a Channel or a Job that has a Channel. The
594 Channel must be open for the channel number to be used.
595
Bram Moolenaar570497a2019-08-22 22:55:13 +0200596 Can also be used as a |method|: >
597 'did something'->ch_log()
598
Bram Moolenaared997ad2019-07-21 16:42:00 +0200599
600ch_logfile({fname} [, {mode}]) *ch_logfile()*
601 Start logging channel activity to {fname}.
602 When {fname} is an empty string: stop logging.
603
604 When {mode} is omitted or "a" append to the file.
605 When {mode} is "w" start with an empty file.
606
607 Use |ch_log()| to write log messages. The file is flushed
608 after every message, on Unix you can use "tail -f" to see what
609 is going on in real time.
610
Bram Moolenaar077cc7a2020-09-04 16:35:35 +0200611 To enable the log very early, to see what is received from a
612 terminal during startup, use |--cmd|: >
613 vim --cmd "call ch_logfile('logfile', 'w')"
614<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200615 This function is not available in the |sandbox|.
616 NOTE: the channel communication is stored in the file, be
617 aware that this may contain confidential and privacy sensitive
618 information, e.g. a password you type in a terminal window.
619
Bram Moolenaar570497a2019-08-22 22:55:13 +0200620 Can also be used as a |method|: >
621 'logfile'->ch_logfile('w')
622
Bram Moolenaared997ad2019-07-21 16:42:00 +0200623
624ch_open({address} [, {options}]) *ch_open()*
625 Open a channel to {address}. See |channel|.
626 Returns a Channel. Use |ch_status()| to check for failure.
627
628 {address} has the form "hostname:port", e.g.,
629 "localhost:8765".
630
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200631 When using an IPv6 address, enclose it within square brackets.
632 E.g., "[2001:db8::1]:8765".
633
Bram Moolenaared997ad2019-07-21 16:42:00 +0200634 If {options} is given it must be a |Dictionary|.
635 See |channel-open-options|.
636
Bram Moolenaar570497a2019-08-22 22:55:13 +0200637 Can also be used as a |method|: >
638 GetAddress()->ch_open()
639
Bram Moolenaared997ad2019-07-21 16:42:00 +0200640
641ch_read({handle} [, {options}]) *ch_read()*
642 Read from {handle} and return the received message.
643 {handle} can be a Channel or a Job that has a Channel.
644 For a NL channel this waits for a NL to arrive, except when
645 there is nothing more to read (channel was closed).
646 See |channel-more|.
647
Bram Moolenaar570497a2019-08-22 22:55:13 +0200648 Can also be used as a |method|: >
649 GetChannel()->ch_read()
650
Bram Moolenaared997ad2019-07-21 16:42:00 +0200651
652ch_readblob({handle} [, {options}]) *ch_readblob()*
653 Like ch_read() but reads binary data and returns a |Blob|.
654 See |channel-more|.
655
Bram Moolenaar570497a2019-08-22 22:55:13 +0200656 Can also be used as a |method|: >
657 GetChannel()->ch_readblob()
658
Bram Moolenaared997ad2019-07-21 16:42:00 +0200659
660ch_readraw({handle} [, {options}]) *ch_readraw()*
661 Like ch_read() but for a JS and JSON channel does not decode
662 the message. For a NL channel it does not block waiting for
663 the NL to arrive, but otherwise works like ch_read().
664 See |channel-more|.
665
Bram Moolenaar570497a2019-08-22 22:55:13 +0200666 Can also be used as a |method|: >
667 GetChannel()->ch_readraw()
668
Bram Moolenaared997ad2019-07-21 16:42:00 +0200669
670ch_sendexpr({handle}, {expr} [, {options}]) *ch_sendexpr()*
671 Send {expr} over {handle}. The {expr} is encoded
672 according to the type of channel. The function cannot be used
673 with a raw channel.
674 See |channel-use|. *E912*
675 {handle} can be a Channel or a Job that has a Channel.
676
Bram Moolenaar570497a2019-08-22 22:55:13 +0200677 Can also be used as a |method|: >
678 GetChannel()->ch_sendexpr(expr)
679
Bram Moolenaared997ad2019-07-21 16:42:00 +0200680
681ch_sendraw({handle}, {expr} [, {options}]) *ch_sendraw()*
682 Send |String| or |Blob| {expr} over {handle}.
683 Works like |ch_sendexpr()|, but does not encode the request or
684 decode the response. The caller is responsible for the
685 correct contents. Also does not add a newline for a channel
686 in NL mode, the caller must do that. The NL in the response
687 is removed.
688 See |channel-use|.
689
Bram Moolenaar570497a2019-08-22 22:55:13 +0200690 Can also be used as a |method|: >
691 GetChannel()->ch_sendraw(rawexpr)
692
Bram Moolenaared997ad2019-07-21 16:42:00 +0200693
694ch_setoptions({handle}, {options}) *ch_setoptions()*
695 Set options on {handle}:
696 "callback" the channel callback
697 "timeout" default read timeout in msec
698 "mode" mode for the whole channel
699 See |ch_open()| for more explanation.
700 {handle} can be a Channel or a Job that has a Channel.
701
702 Note that changing the mode may cause queued messages to be
703 lost.
704
705 These options cannot be changed:
706 "waittime" only applies to |ch_open()|
707
Bram Moolenaar570497a2019-08-22 22:55:13 +0200708 Can also be used as a |method|: >
709 GetChannel()->ch_setoptions(options)
710
Bram Moolenaared997ad2019-07-21 16:42:00 +0200711
712ch_status({handle} [, {options}]) *ch_status()*
713 Return the status of {handle}:
714 "fail" failed to open the channel
715 "open" channel can be used
716 "buffered" channel can be read, not written to
717 "closed" channel can not be used
718 {handle} can be a Channel or a Job that has a Channel.
719 "buffered" is used when the channel was closed but there is
720 still data that can be obtained with |ch_read()|.
721
722 If {options} is given it can contain a "part" entry to specify
723 the part of the channel to return the status for: "out" or
724 "err". For example, to get the error status: >
725 ch_status(job, {"part": "err"})
726<
Bram Moolenaar570497a2019-08-22 22:55:13 +0200727 Can also be used as a |method|: >
728 GetChannel()->ch_status()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200729
730==============================================================================
7319. Starting a job with a channel *job-start* *job*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100732
733To start a job and open a channel for stdin/stdout/stderr: >
734 let job = job_start(command, {options})
735
736You can get the channel with: >
737 let channel = job_getchannel(job)
738
739The channel will use NL mode. If you want another mode it's best to specify
740this in {options}. When changing the mode later some text may have already
741been received and not parsed correctly.
742
743If the command produces a line of output that you want to deal with, specify
744a handler for stdout: >
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100745 let job = job_start(command, {"out_cb": "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100746The function will be called with the channel and a message. You would define
747it like this: >
748 func MyHandler(channel, msg)
749
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100750Without the handler you need to read the output with |ch_read()| or
Bram Moolenaar06481422016-04-30 15:13:38 +0200751|ch_readraw()|. You can do this in the close callback, see |read-in-close-cb|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100752
Bram Moolenaar1ccd8ff2017-08-11 19:50:37 +0200753Note that if the job exits before you read the output, the output may be lost.
754This depends on the system (on Unix this happens because closing the write end
755of a pipe causes the read end to get EOF). To avoid this make the job sleep
756for a short while before it exits.
757
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100758The handler defined for "out_cb" will not receive stderr. If you want to
759handle that separately, add an "err_cb" handler: >
760 let job = job_start(command, {"out_cb": "MyHandler",
761 \ "err_cb": "ErrHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100762
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100763If you want to handle both stderr and stdout with one handler use the
764"callback" option: >
Bram Moolenaar664f3cf2019-12-07 16:03:51 +0100765 let job = job_start(command, {"callback": "MyHandler"})
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100766
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200767Depending on the system, starting a job can put Vim in the background, the
768started job gets the focus. To avoid that, use the `foreground()` function.
769This might not always work when called early, put in the callback handler or
770use a timer to call it after the job has started.
771
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100772You can send a message to the command with ch_evalraw(). If the channel is in
773JSON or JS mode you can use ch_evalexpr().
Bram Moolenaar38a55632016-02-15 22:07:32 +0100774
775There are several options you can use, see |job-options|.
Bram Moolenaar187db502016-02-27 14:44:26 +0100776For example, to start a job and write its output in buffer "dummy": >
777 let logjob = job_start("tail -f /tmp/log",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100778 \ {'out_io': 'buffer', 'out_name': 'dummy'})
Bram Moolenaar187db502016-02-27 14:44:26 +0100779 sbuf dummy
Bram Moolenaar38a55632016-02-15 22:07:32 +0100780
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100781
782Job input from a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +0200783 *in_io-buffer*
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100784To run a job that reads from a buffer: >
785 let job = job_start({command},
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100786 \ {'in_io': 'buffer', 'in_name': 'mybuffer'})
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100787<
788 *E915* *E918*
789The buffer is found by name, similar to |bufnr()|. The buffer must exist and
790be loaded when job_start() is called.
791
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100792By default this reads the whole buffer. This can be changed with the "in_top"
793and "in_bot" options.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100794
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100795A special mode is when "in_top" is set to zero and "in_bot" is not set: Every
Bram Moolenaar74675a62017-07-15 13:53:23 +0200796time a line is added to the buffer, the last-but-one line will be sent to the
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100797job stdin. This allows for editing the last line and sending it when pressing
798Enter.
Bram Moolenaar0874a832016-09-01 15:11:51 +0200799 *channel-close-in*
800When not using the special mode the pipe or socket will be closed after the
801last line has been written. This signals the reading end that the input
802finished. You can also use |ch_close_in()| to close it sooner.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100803
Bram Moolenaar063b9d12016-07-09 20:21:48 +0200804NUL bytes in the text will be passed to the job (internally Vim stores these
805as NL bytes).
806
Bram Moolenaar06481422016-04-30 15:13:38 +0200807
808Reading job output in the close callback ~
809 *read-in-close-cb*
810If the job can take some time and you don't need intermediate results, you can
811add a close callback and read the output there: >
812
813 func! CloseHandler(channel)
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200814 while ch_status(a:channel, {'part': 'out'}) == 'buffered'
Bram Moolenaar06481422016-04-30 15:13:38 +0200815 echomsg ch_read(a:channel)
816 endwhile
817 endfunc
818 let job = job_start(command, {'close_cb': 'CloseHandler'})
819
820You will want to do something more useful than "echomsg".
821
Bram Moolenaar38a55632016-02-15 22:07:32 +0100822==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +020082310. Starting a job without a channel *job-start-nochannel*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100824
825To start another process without creating a channel: >
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100826 let job = job_start(command,
Bram Moolenaar51628222016-12-01 23:03:28 +0100827 \ {"in_io": "null", "out_io": "null", "err_io": "null"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100828
829This starts {command} in the background, Vim does not wait for it to finish.
830
Bram Moolenaar38a55632016-02-15 22:07:32 +0100831When Vim sees that neither stdin, stdout or stderr are connected, no channel
832will be created. Often you will want to include redirection in the command to
833avoid it getting stuck.
834
835There are several options you can use, see |job-options|.
836
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100837 *job-start-if-needed*
838To start a job only when connecting to an address does not work, do something
839like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100840 let channel = ch_open(address, {"waittime": 0})
841 if ch_status(channel) == "fail"
842 let job = job_start(command)
843 let channel = ch_open(address, {"waittime": 1000})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100844 endif
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100845
846Note that the waittime for ch_open() gives the job one second to make the port
847available.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100848
849==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +020085011. Job functions *job-functions-details*
851
852job_getchannel({job}) *job_getchannel()*
853 Get the channel handle that {job} is using.
854 To check if the job has no channel: >
855 if string(job_getchannel()) == 'channel fail'
856<
Bram Moolenaar570497a2019-08-22 22:55:13 +0200857 Can also be used as a |method|: >
858 GetJob()->job_getchannel()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200859
860job_info([{job}]) *job_info()*
861 Returns a Dictionary with information about {job}:
862 "status" what |job_status()| returns
863 "channel" what |job_getchannel()| returns
864 "cmd" List of command arguments used to start the job
865 "process" process ID
866 "tty_in" terminal input name, empty when none
867 "tty_out" terminal output name, empty when none
868 "exitval" only valid when "status" is "dead"
869 "exit_cb" function to be called on exit
870 "stoponexit" |job-stoponexit|
871
872 Only in Unix:
873 "termsig" the signal which terminated the process
874 (See |job_stop()| for the values)
875 only valid when "status" is "dead"
876
877 Only in MS-Windows:
878 "tty_type" Type of virtual console in use.
879 Values are "winpty" or "conpty".
880 See 'termwintype'.
881
882 Without any arguments, returns a List with all Job objects.
883
Bram Moolenaar570497a2019-08-22 22:55:13 +0200884 Can also be used as a |method|: >
885 GetJob()->job_info()
886
Bram Moolenaared997ad2019-07-21 16:42:00 +0200887
888job_setoptions({job}, {options}) *job_setoptions()*
889 Change options for {job}. Supported are:
890 "stoponexit" |job-stoponexit|
891 "exit_cb" |job-exit_cb|
892
Bram Moolenaar570497a2019-08-22 22:55:13 +0200893 Can also be used as a |method|: >
894 GetJob()->job_setoptions(options)
895
Bram Moolenaared997ad2019-07-21 16:42:00 +0200896
897job_start({command} [, {options}]) *job_start()*
898 Start a job and return a Job object. Unlike |system()| and
899 |:!cmd| this does not wait for the job to finish.
900 To start a job in a terminal window see |term_start()|.
901
902 If the job fails to start then |job_status()| on the returned
903 Job object results in "fail" and none of the callbacks will be
904 invoked.
905
906 {command} can be a String. This works best on MS-Windows. On
907 Unix it is split up in white-separated parts to be passed to
908 execvp(). Arguments in double quotes can contain white space.
909
910 {command} can be a List, where the first item is the executable
911 and further items are the arguments. All items are converted
912 to String. This works best on Unix.
913
914 On MS-Windows, job_start() makes a GUI application hidden. If
915 want to show it, Use |:!start| instead.
916
917 The command is executed directly, not through a shell, the
918 'shell' option is not used. To use the shell: >
919 let job = job_start(["/bin/sh", "-c", "echo hello"])
920< Or: >
921 let job = job_start('/bin/sh -c "echo hello"')
922< Note that this will start two processes, the shell and the
923 command it executes. If you don't want this use the "exec"
924 shell command.
925
926 On Unix $PATH is used to search for the executable only when
927 the command does not contain a slash.
928
929 The job will use the same terminal as Vim. If it reads from
930 stdin the job and Vim will be fighting over input, that
931 doesn't work. Redirect stdin and stdout to avoid problems: >
932 let job = job_start(['sh', '-c', "myserver </dev/null >/dev/null"])
933<
934 The returned Job object can be used to get the status with
935 |job_status()| and stop the job with |job_stop()|.
936
937 Note that the job object will be deleted if there are no
938 references to it. This closes the stdin and stderr, which may
939 cause the job to fail with an error. To avoid this keep a
940 reference to the job. Thus instead of: >
941 call job_start('my-command')
942< use: >
943 let myjob = job_start('my-command')
944< and unlet "myjob" once the job is not needed or is past the
945 point where it would fail (e.g. when it prints a message on
946 startup). Keep in mind that variables local to a function
947 will cease to exist if the function returns. Use a
948 script-local variable if needed: >
949 let s:myjob = job_start('my-command')
950<
951 {options} must be a Dictionary. It can contain many optional
952 items, see |job-options|.
953
Bram Moolenaar570497a2019-08-22 22:55:13 +0200954 Can also be used as a |method|: >
955 BuildCommand()->job_start()
956
Bram Moolenaared997ad2019-07-21 16:42:00 +0200957
958job_status({job}) *job_status()* *E916*
959 Returns a String with the status of {job}:
960 "run" job is running
961 "fail" job failed to start
962 "dead" job died or was stopped after running
963
964 On Unix a non-existing command results in "dead" instead of
965 "fail", because a fork happens before the failure can be
966 detected.
967
968 If an exit callback was set with the "exit_cb" option and the
969 job is now detected to be "dead" the callback will be invoked.
970
971 For more information see |job_info()|.
972
Bram Moolenaar570497a2019-08-22 22:55:13 +0200973 Can also be used as a |method|: >
974 GetJob()->job_status()
975
Bram Moolenaared997ad2019-07-21 16:42:00 +0200976
977job_stop({job} [, {how}]) *job_stop()*
978 Stop the {job}. This can also be used to signal the job.
979
980 When {how} is omitted or is "term" the job will be terminated.
981 For Unix SIGTERM is sent. On MS-Windows the job will be
982 terminated forcedly (there is no "gentle" way).
983 This goes to the process group, thus children may also be
984 affected.
985
986 Effect for Unix:
987 "term" SIGTERM (default)
988 "hup" SIGHUP
989 "quit" SIGQUIT
990 "int" SIGINT
991 "kill" SIGKILL (strongest way to stop)
992 number signal with that number
993
994 Effect for MS-Windows:
995 "term" terminate process forcedly (default)
996 "hup" CTRL_BREAK
997 "quit" CTRL_BREAK
998 "int" CTRL_C
999 "kill" terminate process forcedly
1000 Others CTRL_BREAK
1001
1002 On Unix the signal is sent to the process group. This means
1003 that when the job is "sh -c command" it affects both the shell
1004 and the command.
1005
1006 The result is a Number: 1 if the operation could be executed,
1007 0 if "how" is not supported on the system.
1008 Note that even when the operation was executed, whether the
1009 job was actually stopped needs to be checked with
1010 |job_status()|.
1011
1012 If the status of the job is "dead", the signal will not be
1013 sent. This is to avoid to stop the wrong job (esp. on Unix,
1014 where process numbers are recycled).
1015
1016 When using "kill" Vim will assume the job will die and close
1017 the channel.
1018
Bram Moolenaar570497a2019-08-22 22:55:13 +02001019 Can also be used as a |method|: >
1020 GetJob()->job_stop()
1021
Bram Moolenaared997ad2019-07-21 16:42:00 +02001022
1023==============================================================================
102412. Job options *job-options*
Bram Moolenaar38a55632016-02-15 22:07:32 +01001025
1026The {options} argument in job_start() is a dictionary. All entries are
Bram Moolenaar02e83b42016-02-21 20:10:26 +01001027optional. Some options can be used after the job has started, using
1028job_setoptions(job, {options}). Many options can be used with the channel
1029related to the job, using ch_setoptions(channel, {options}).
1030See |job_setoptions()| and |ch_setoptions()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +01001031
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001032 *in_mode* *out_mode* *err_mode*
1033"in_mode" mode specifically for stdin, only when using pipes
1034"out_mode" mode specifically for stdout, only when using pipes
1035"err_mode" mode specifically for stderr, only when using pipes
1036 See |channel-mode| for the values.
1037
1038 Note: when setting "mode" the part specific mode is
1039 overwritten. Therefore set "mode" first and the part
1040 specific mode later.
1041
1042 Note: when writing to a file or buffer and when
1043 reading from a buffer NL mode is used by default.
1044
Bram Moolenaar0b146882018-09-06 16:27:24 +02001045 *job-noblock*
1046"noblock": 1 When writing use a non-blocking write call. This
1047 avoids getting stuck if Vim should handle other
1048 messages in between, e.g. when a job sends back data
1049 to Vim. It implies that when `ch_sendraw()` returns
1050 not all data may have been written yet.
1051 This option was added in patch 8.1.0350, test with: >
1052 if has("patch-8.1.350")
1053 let options['noblock'] = 1
1054 endif
1055<
Bram Moolenaardecb14d2016-02-20 23:32:02 +01001056 *job-callback*
1057"callback": handler Callback for something to read on any part of the
1058 channel.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001059 *job-out_cb* *out_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001060"out_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001061 stdout. Only for when the channel uses pipes. When
1062 "out_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +02001063 The two arguments are the channel and the message.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001064
1065 *job-err_cb* *err_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001066"err_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001067 stderr. Only for when the channel uses pipes. When
1068 "err_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +02001069 The two arguments are the channel and the message.
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001070 *job-close_cb*
1071"close_cb": handler Callback for when the channel is closed. Same as
Bram Moolenaar82af8712016-06-04 20:20:29 +02001072 "close_cb" on |ch_open()|, see |close_cb|.
Bram Moolenaarbc2eada2017-01-02 21:27:47 +01001073 *job-drop*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001074"drop": when Specifies when to drop messages. Same as "drop" on
Bram Moolenaar51628222016-12-01 23:03:28 +01001075 |ch_open()|, see |channel-drop|. For "auto" the
1076 exit_cb is not considered.
Bram Moolenaarbc2eada2017-01-02 21:27:47 +01001077 *job-exit_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001078"exit_cb": handler Callback for when the job ends. The arguments are the
Bram Moolenaar38a55632016-02-15 22:07:32 +01001079 job and the exit status.
Bram Moolenaarb4ada792016-10-30 21:55:26 +01001080 Vim checks up to 10 times per second for jobs that
1081 ended. The check can also be triggered by calling
1082 |job_status()|, which may then invoke the exit_cb
1083 handler.
Bram Moolenaar06d2d382016-05-20 17:24:11 +02001084 Note that data can be buffered, callbacks may still be
1085 called after the process ends.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001086 *job-timeout*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001087"timeout": time The time to wait for a request when blocking, E.g.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001088 when using ch_evalexpr(). In milliseconds. The
1089 default is 2000 (2 seconds).
1090 *out_timeout* *err_timeout*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001091"out_timeout": time Timeout for stdout. Only when using pipes.
1092"err_timeout": time Timeout for stderr. Only when using pipes.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001093 Note: when setting "timeout" the part specific mode is
1094 overwritten. Therefore set "timeout" first and the
1095 part specific mode later.
1096
Bram Moolenaar02e83b42016-02-21 20:10:26 +01001097 *job-stoponexit*
1098"stoponexit": {signal} Send {signal} to the job when Vim exits. See
1099 |job_stop()| for possible values.
1100"stoponexit": "" Do not stop the job when Vim exits.
1101 The default is "term".
1102
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001103 *job-term*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001104"term": "open" Start a terminal in a new window and connect the job
1105 stdin/stdout/stderr to it. Similar to using
1106 `:terminal`.
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001107 NOTE: Not implemented yet!
Bram Moolenaar38a55632016-02-15 22:07:32 +01001108
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001109"channel": {channel} Use an existing channel instead of creating a new one.
1110 The parts of the channel that get used for the new job
1111 will be disconnected from what they were used before.
Bram Moolenaar51628222016-12-01 23:03:28 +01001112 If the channel was still used by another job this may
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001113 cause I/O errors.
1114 Existing callbacks and other settings remain.
1115
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001116"pty": 1 Use a pty (pseudo-tty) instead of a pipe when
1117 possible. This is most useful in combination with a
1118 terminal window, see |terminal|.
1119 {only on Unix and Unix-like systems}
1120
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001121 *job-in_io* *in_top* *in_bot* *in_name* *in_buf*
1122"in_io": "null" disconnect stdin (read from /dev/null)
1123"in_io": "pipe" stdin is connected to the channel (default)
1124"in_io": "file" stdin reads from a file
1125"in_io": "buffer" stdin reads from a buffer
1126"in_top": number when using "buffer": first line to send (default: 1)
1127"in_bot": number when using "buffer": last line to send (default: last)
1128"in_name": "/path/file" the name of the file or buffer to read from
1129"in_buf": number the number of the buffer to read from
Bram Moolenaar38a55632016-02-15 22:07:32 +01001130
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001131 *job-out_io* *out_name* *out_buf*
1132"out_io": "null" disconnect stdout (goes to /dev/null)
1133"out_io": "pipe" stdout is connected to the channel (default)
1134"out_io": "file" stdout writes to a file
Bram Moolenaar51628222016-12-01 23:03:28 +01001135"out_io": "buffer" stdout appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001136"out_name": "/path/file" the name of the file or buffer to write to
1137"out_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001138"out_modifiable": 0 when writing to a buffer, 'modifiable' will be off
1139 (see below)
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001140"out_msg": 0 when writing to a new buffer, the first line will be
1141 set to "Reading from channel output..."
Bram Moolenaar38a55632016-02-15 22:07:32 +01001142
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001143 *job-err_io* *err_name* *err_buf*
1144"err_io": "out" stderr messages to go to stdout
1145"err_io": "null" disconnect stderr (goes to /dev/null)
1146"err_io": "pipe" stderr is connected to the channel (default)
1147"err_io": "file" stderr writes to a file
Bram Moolenaar51628222016-12-01 23:03:28 +01001148"err_io": "buffer" stderr appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001149"err_name": "/path/file" the name of the file or buffer to write to
1150"err_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001151"err_modifiable": 0 when writing to a buffer, 'modifiable' will be off
1152 (see below)
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001153"err_msg": 0 when writing to a new buffer, the first line will be
1154 set to "Reading from channel error..."
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001155
Bram Moolenaar7db8f6f2016-03-29 23:12:46 +02001156"block_write": number only for testing: pretend every other write to stdin
1157 will block
1158
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001159"env": dict environment variables for the new process
1160"cwd": "/path/to/dir" current working directory for the new process;
1161 if the directory does not exist an error is given
1162
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001163
1164Writing to a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +02001165 *out_io-buffer*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001166When the out_io or err_io mode is "buffer" and there is a callback, the text
Bram Moolenaar5f148ec2016-03-07 22:59:26 +01001167is appended to the buffer before invoking the callback.
1168
1169When a buffer is used both for input and output, the output lines are put
1170above the last line, since the last line is what is written to the channel
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001171input. Otherwise lines are appended below the last line.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001172
Bram Moolenaar328da0d2016-03-04 22:22:32 +01001173When using JS or JSON mode with "buffer", only messages with zero or negative
1174ID will be added to the buffer, after decoding + encoding. Messages with a
1175positive number will be handled by a callback, commands are handled as usual.
1176
Bram Moolenaar82af8712016-06-04 20:20:29 +02001177The name of the buffer from "out_name" or "err_name" is compared the full name
1178of existing buffers, also after expanding the name for the current directory.
1179E.g., when a buffer was created with ":edit somename" and the buffer name is
1180"somename" it will use that buffer.
1181
1182If there is no matching buffer a new buffer is created. Use an empty name to
1183always create a new buffer. |ch_getbufnr()| can then be used to get the
1184buffer number.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001185
1186For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If
1187you prefer other settings, create the buffer first and pass the buffer number.
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001188 *out_modifiable* *err_modifiable*
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001189The "out_modifiable" and "err_modifiable" options can be used to set the
1190'modifiable' option off, or write to a buffer that has 'modifiable' off. That
1191means that lines will be appended to the buffer, but the user can't easily
1192change the buffer.
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001193 *out_msg* *err_msg*
1194The "out_msg" option can be used to specify whether a new buffer will have the
1195first line set to "Reading from channel output...". The default is to add the
1196message. "err_msg" does the same for channel error.
1197
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001198When an existing buffer is to be written where 'modifiable' is off and the
1199"out_modifiable" or "err_modifiable" options is not zero, an error is given
1200and the buffer will not be written to.
1201
Bram Moolenaar187db502016-02-27 14:44:26 +01001202When the buffer written to is displayed in a window and the cursor is in the
1203first column of the last line, the cursor will be moved to the newly added
1204line and the window is scrolled up to show the cursor if needed.
1205
Bram Moolenaar063b9d12016-07-09 20:21:48 +02001206Undo is synced for every added line. NUL bytes are accepted (internally Vim
1207stores these as NL bytes).
Bram Moolenaar38a55632016-02-15 22:07:32 +01001208
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001209
1210Writing to a file ~
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001211 *E920*
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001212The file is created with permissions 600 (read-write for the user, not
1213accessible for others). Use |setfperm()| to change this.
1214
1215If the file already exists it is truncated.
1216
Bram Moolenaar38a55632016-02-15 22:07:32 +01001217==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +0200121813. Controlling a job *job-control*
Bram Moolenaar38a55632016-02-15 22:07:32 +01001219
1220To get the status of a job: >
1221 echo job_status(job)
1222
1223To make a job stop running: >
1224 job_stop(job)
1225
1226This is the normal way to end a job. On Unix it sends a SIGTERM to the job.
1227It is possible to use other ways to stop the job, or even send arbitrary
1228signals. E.g. to force a job to stop, "kill it": >
1229 job_stop(job, "kill")
1230
1231For more options see |job_stop()|.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001232
Bram Moolenaarf2732452018-06-03 14:47:35 +02001233==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +0200123414. Using a prompt buffer *prompt-buffer*
Bram Moolenaarf2732452018-06-03 14:47:35 +02001235
1236If you want to type input for the job in a Vim window you have a few options:
1237- Use a normal buffer and handle all possible commands yourself.
1238 This will be complicated, since there are so many possible commands.
1239- Use a terminal window. This works well if what you type goes directly to
1240 the job and the job output is directly displayed in the window.
1241 See |terminal-window|.
Bram Moolenaaracc22402020-06-07 21:07:18 +02001242- Use a window with a prompt buffer. This works well when entering a line for
1243 the job in Vim while displaying (possibly filtered) output from the job.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001244
1245A prompt buffer is created by setting 'buftype' to "prompt". You would
1246normally only do that in a newly created buffer.
1247
1248The user can edit and enter one line of text at the very last line of the
1249buffer. When pressing Enter in the prompt line the callback set with
1250|prompt_setcallback()| is invoked. It would normally send the line to a job.
1251Another callback would receive the output from the job and display it in the
1252buffer, below the prompt (and above the next prompt).
1253
1254Only the text in the last line, after the prompt, is editable. The rest of the
1255buffer is not modifiable with Normal mode commands. It can be modified by
1256calling functions, such as |append()|. Using other commands may mess up the
1257buffer.
1258
1259After setting 'buftype' to "prompt" Vim does not automatically start Insert
1260mode, use `:startinsert` if you want to enter Insert mode, so that the user
1261can start typing a line.
1262
Bram Moolenaar077cc7a2020-09-04 16:35:35 +02001263The text of the prompt can be set with the |prompt_setprompt()| function. If
1264no prompt is set with |prompt_setprompt()|, "% " is used. You can get the
1265effective prompt text for a buffer, with |prompt_getprompt()|.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001266
1267The user can go to Normal mode and navigate through the buffer. This can be
Bram Moolenaar7ff78462020-07-10 22:00:53 +02001268useful to see older output or copy text.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001269
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +02001270The CTRL-W key can be used to start a window command, such as CTRL-W w to
1271switch to the next window. This also works in Insert mode (use Shift-CTRL-W
1272to delete a word). When leaving the window Insert mode will be stopped. When
1273coming back to the prompt window Insert mode will be restored.
1274
Bram Moolenaarf2732452018-06-03 14:47:35 +02001275Any command that starts Insert mode, such as "a", "i", "A" and "I", will move
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +02001276the cursor to the last line. "A" will move to the end of the line, "I" to the
1277start of the line.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001278
Bram Moolenaaracc22402020-06-07 21:07:18 +02001279Here is an example for Unix. It starts a shell in the background and prompts
1280for the next shell command. Output from the shell is displayed above the
1281prompt. >
1282
1283 " Create a channel log so we can see what happens.
1284 call ch_logfile('logfile', 'w')
1285
1286 " Function handling a line of text has been typed.
1287 func TextEntered(text)
1288 " Send the text to a shell with Enter appended.
1289 call ch_sendraw(g:shell_job, a:text .. "\n")
1290 endfunc
1291
1292 " Function handling output from the shell: Added above the prompt.
1293 func GotOutput(channel, msg)
1294 call append(line("$") - 1, "- " . a:msg)
1295 endfunc
1296
1297 " Function handling the shell exist: close the window.
1298 func JobExit(job, status)
1299 quit!
1300 endfunc
1301
1302 " Start a shell in the background.
1303 let shell_job = job_start(["/bin/sh"], #{
1304 \ out_cb: function('GotOutput'),
1305 \ err_cb: function('GotOutput'),
1306 \ exit_cb: function('JobExit'),
1307 \ })
1308 let shell_ch = job_getchannel(shell_job)
1309
1310 new
1311 set buftype=prompt
1312 let buf = bufnr('')
1313 call prompt_setcallback(buf, function("TextEntered"))
1314 eval prompt_setprompt(buf, "shell command: ")
1315
1316 " start accepting shell commands
1317 startinsert
1318<
1319
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001320
Bram Moolenaar91f84f62018-07-29 15:07:52 +02001321 vim:tw=78:ts=8:noet:ft=help:norl: