blob: 54c7e1f044bcf4cec5727f186a0d460d65eed42d [file] [log] [blame]
Bram Moolenaarab943432018-03-29 18:27:07 +02001*terminal.txt* For Vim version 8.0. Last change: 2018 Mar 26
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7Terminal window support *terminal*
8
9
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +020010The terminal feature is optional, use this to check if your Vim has it: >
11 echo has('terminal')
12If the result is "1" you have it.
13
Bram Moolenaare4f25e42017-07-07 11:54:15 +020014
Bram Moolenaarda650582018-02-20 15:51:40 +0100151. Basic use |terminal-use|
16 Typing |terminal-typing|
17 Size and color |terminal-size-color|
18 Syntax |:terminal|
19 Resizing |terminal-resizing|
20 Terminal Modes |Terminal-mode|
21 Cursor style |terminal-cursor-style|
22 Special keys |terminal-special-keys|
Bram Moolenaarb5b75622018-03-09 22:22:21 +010023 Session |terminal-session|
Bram Moolenaarda650582018-02-20 15:51:40 +010024 Unix |terminal-unix|
25 MS-Windows |terminal-ms-windows|
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200262. Terminal communication |terminal-communication|
27 Vim to job: term_sendkeys() |terminal-to-job|
28 Job to Vim: JSON API |terminal-api|
29 Using the client-server feature |terminal-client-server|
303. Remote testing |terminal-testing|
314. Diffing screen dumps |terminal-diff|
Bram Moolenaarda650582018-02-20 15:51:40 +010032 Writing a screen dump test for Vim |terminal-dumptest|
33 Creating a screen dump |terminal-screendump|
34 Comparing screen dumps |terminal-diffscreendump|
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200355. Debugging |terminal-debug|
Bram Moolenaarda650582018-02-20 15:51:40 +010036 Starting |termdebug-starting|
37 Example session |termdebug-example|
38 Stepping through code |termdebug-stepping|
39 Inspecting variables |termdebug-variables|
40 Other commands |termdebug-commands|
41 Communication |termdebug-communication|
42 Customizing |termdebug-customizing|
Bram Moolenaare4f25e42017-07-07 11:54:15 +020043
44{Vi does not have any of these commands}
Bram Moolenaarc572da52017-08-27 16:52:01 +020045{only available when compiled with the |+terminal| feature}
Bram Moolenaarc572da52017-08-27 16:52:01 +020046The terminal feature requires the |+multi_byte|, |+job| and |+channel| features.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020047
48==============================================================================
491. Basic use *terminal-use*
50
51This feature is for running a terminal emulator in a Vim window. A job can be
52started connected to the terminal emulator. For example, to run a shell: >
53 :term bash
54
Bram Moolenaare09ba7b2017-09-09 22:19:47 +020055Or to run build command: >
56 :term make myprogram
Bram Moolenaare4f25e42017-07-07 11:54:15 +020057
58The job runs asynchronously from Vim, the window will be updated to show
Bram Moolenaare09ba7b2017-09-09 22:19:47 +020059output from the job, also while editing in another window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020060
Bram Moolenaar423802d2017-07-30 16:52:24 +020061
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +020062Typing ~
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +020063 *terminal-typing*
Bram Moolenaardd693ce2017-08-10 23:15:19 +020064When the keyboard focus is in the terminal window, typed keys will be sent to
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +020065the job. This uses a pty when possible. You can click outside of the
66terminal window to move keyboard focus elsewhere.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020067
Bram Moolenaar423802d2017-07-30 16:52:24 +020068CTRL-W can be used to navigate between windows and other CTRL-W commands, e.g.:
Bram Moolenaar60e73f22017-11-12 18:02:06 +010069 CTRL-W CTRL-W move focus to the next window
Bram Moolenaar423802d2017-07-30 16:52:24 +020070 CTRL-W : enter an Ex command
71See |CTRL-W| for more commands.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020072
Bram Moolenaar423802d2017-07-30 16:52:24 +020073Special in the terminal window: *CTRL-W_.* *CTRL-W_N*
74 CTRL-W . send a CTRL-W to the job in the terminal
Bram Moolenaardd693ce2017-08-10 23:15:19 +020075 CTRL-W N go to Terminal-Normal mode, see |Terminal-mode|
76 CTRL-\ CTRL-N go to Terminal-Normal mode, see |Terminal-mode|
Bram Moolenaarf55e4c82017-08-01 20:44:53 +020077 CTRL-W " {reg} paste register {reg} *CTRL-W_quote*
78 Also works with the = register to insert the result of
79 evaluating an expression.
Bram Moolenaar8e539c52017-08-18 22:57:06 +020080 CTRL-W CTRL-C ends the job, see below |t_CTRL-W_CTRL-C|
Bram Moolenaar423802d2017-07-30 16:52:24 +020081
82See option 'termkey' for specifying another key instead of CTRL-W that
83will work like CTRL-W. However, typing 'termkey' twice sends 'termkey' to
84the job. For example:
85 'termkey' CTRL-W move focus to the next window
86 'termkey' : enter an Ex command
87 'termkey' 'termkey' send 'termkey' to the job in the terminal
88 'termkey' . send a CTRL-W to the job in the terminal
Bram Moolenaar60e73f22017-11-12 18:02:06 +010089 'termkey' N go to terminal Normal mode, see below
Bram Moolenaar423802d2017-07-30 16:52:24 +020090 'termkey' CTRL-N same as CTRL-W N
Bram Moolenaar8e539c52017-08-18 22:57:06 +020091 'termkey' CTRL-C same as |t_CTRL-W_CTRL-C|
Bram Moolenaar69198192017-08-05 14:10:48 +020092 *t_CTRL-\_CTRL-N*
Bram Moolenaardd693ce2017-08-10 23:15:19 +020093The special key combination CTRL-\ CTRL-N can be used to switch to Normal
94mode, just like this works in any other mode.
Bram Moolenaar8e539c52017-08-18 22:57:06 +020095 *t_CTRL-W_CTRL-C*
96CTRL-W CTRL-C can be typed to forcefully end the job. On MS-Windows a
97CTRL-BREAK will also kill the job.
98
99If you type CTRL-C the effect depends on what the pty has been configured to
100do. For simple commands this causes a SIGINT to be sent to the job, which
101would end it. Other commands may ignore the SIGINT or handle the CTRL-C
102themselves (like Vim does).
Bram Moolenaar423802d2017-07-30 16:52:24 +0200103
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200104To change the keys you type use terminal mode mappings, see |:tmap|.
105These are defined like any mapping, but apply only when typing keys that are
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100106sent to the job running in the terminal. For example, to make F1 switch
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200107to Terminal-Normal mode: >
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100108 tnoremap <F1> <C-W>N
109You can use Esc, but you need to make sure it won't cause other keys to
110break: >
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200111 tnoremap <Esc> <C-W>N
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100112 set notimeout ttimeout timeoutlen=100
113
Bram Moolenaar01164a62017-11-02 22:58:42 +0100114< *options-in-terminal*
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200115After opening the terminal window and setting 'buftype' to "terminal" the
116BufWinEnter autocommand event is triggered. This makes it possible to set
117options specifically for the window and buffer. Example: >
118 au BufWinEnter * if &buftype == 'terminal' | setlocal bufhidden=hide | endif
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200119
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100120Mouse events (click and drag) are passed to the terminal. Mouse move events
121are only passed when Vim itself is receiving them. For a terminal that is
122when 'balloonevalterm' is enabled.
123
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200124
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200125Size and color ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100126 *terminal-size-color*
Bram Moolenaar74675a62017-07-15 13:53:23 +0200127See option 'termsize' for controlling the size of the terminal window.
128(TODO: scrolling when the terminal is larger than the window)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200129
Bram Moolenaar38baa3e2017-09-14 16:10:38 +0200130The job running in the terminal can change the colors. The default foreground
131and background colors are taken from Vim, the Normal highlight group.
132
133For a color terminal the 'background' option is used to decide whether the
134terminal window will start with a white or black background.
135
Bram Moolenaardf980db2017-12-24 13:22:00 +0100136To use a different color the Terminal highlight group can be used, for
137example: >
Bram Moolenaar38baa3e2017-09-14 16:10:38 +0200138 hi Terminal ctermbg=lightgrey ctermfg=blue guibg=lightgrey guifg=blue
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200139
Bram Moolenaar423802d2017-07-30 16:52:24 +0200140
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200141Syntax ~
Bram Moolenaar8a773062017-07-24 22:29:21 +0200142
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200143:[range]ter[minal] [options] [command] *:ter* *:terminal*
Bram Moolenaar8a773062017-07-24 22:29:21 +0200144 Open a new terminal window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200145
146 If [command] is provided run it as a job and connect
147 the input and output to the terminal.
148 If [command] is not given the 'shell' option is used.
Bram Moolenaarc572da52017-08-27 16:52:01 +0200149 if [command] is NONE no job is started, the pty of the
150 terminal can be used by a command like gdb.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200151
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100152 If [command] is missing the default behavior is to
153 close the terminal when the shell exits. This can be
154 changed with the ++noclose argument.
155 If [command] is present the default behavior is to
156 keep the terminal open in Terminal-Normal mode. This
157 can be changed with the ++close argument.
158
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200159 A new buffer will be created, using [command] or
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200160 'shell' as the name, prefixed with a "!". If a buffer
161 by this name already exists a number is added in
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200162 parentheses. E.g. if "gdb" exists the second terminal
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200163 buffer will use "!gdb (1)".
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200164
Bram Moolenaarb2412082017-08-20 18:09:14 +0200165 If [range] is given the specified lines are used as
166 input for the job. It will not be possible to type
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200167 keys in the terminal window. For MS-Windows see the
168 ++eof argument below.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200169
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200170 *term++close* *term++open*
171 Supported [options] are:
172 ++close The terminal window will close
173 automatically when the job terminates.
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100174 ++noclose The terminal window will NOT close
175 automatically when the job terminates.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200176 ++open When the job terminates and no window
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200177 shows it, a window will be opened.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200178 Note that this can be interruptive.
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100179 The last of ++close, ++noclose and ++open
180 matters and rules out earlier arguments.
181
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200182 ++curwin Open the terminal in the current
183 window, do not split the current
184 window. Fails if the current buffer
185 cannot be |abandon|ed.
186 ++hidden Open the terminal in a hidden buffer,
187 no window will be used.
Bram Moolenaarb5b75622018-03-09 22:22:21 +0100188 ++norestore Do not include this terminal window
189 in a session file.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100190 ++kill={how} When trying to close the terminal
191 window kill the job with {how}. See
192 |term_setkill()| for the values.
Bram Moolenaarb2412082017-08-20 18:09:14 +0200193 ++rows={height} Use {height} for the terminal window
Bram Moolenaar40962ec2018-01-28 22:47:25 +0100194 height. If the terminal uses the full
195 Vim height (no window above or below
196 th terminal window) the command line
197 height will be reduced as needed.
Bram Moolenaarb2412082017-08-20 18:09:14 +0200198 ++cols={width} Use {width} for the terminal window
Bram Moolenaar40962ec2018-01-28 22:47:25 +0100199 width. If the terminal uses the full
200 Vim width (no window left or right of
201 the terminal window) this value is
202 ignored.
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200203 ++eof={text} when using [range]: text to send after
204 the last line was written. Cannot
205 contain white space. A CR is
206 appended. For MS-Windows the default
207 is to send CTRL-D.
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200208 E.g. for a shell use "++eof=exit" and
209 for Python "++eof=exit()". Special
210 codes can be used like with `:map`,
211 e.g. "<C-Z>" for CTRL-Z.
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200212
213 If you want to use more options use the |term_start()|
214 function.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200215
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100216When the buffer associated with the terminal is forcibly unloaded or wiped out
217the job is killed, similar to calling `job_stop(job, "kill")` .
218Closing the window normally results in |E947|. When a kill method was set
219with "++kill={how}" or |term_setkill()| then closing the window will use that
220way to kill or interrupt the job. For example: >
221 :term ++kill=term tail -f /tmp/log
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200222
Bram Moolenaare561a7e2017-08-29 22:44:59 +0200223So long as the job is running the window behaves like it contains a modified
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200224buffer. Trying to close the window with `CTRL-W :quit` fails. When using
225`CTRL-W :quit!` the job is ended. The text in the window is lost. The buffer
226still exists, but getting it in a window with `:buffer` will show an empty
227buffer.
228
229Trying to close the window with `CTRL-W :close` also fails. Using
230`CTRL-W :close!` will close the window and make the buffer hidden.
Bram Moolenaare561a7e2017-08-29 22:44:59 +0200231
232You can use `CTRL-W :hide` to close the terminal window and make the buffer
233hidden, the job keeps running. The `:buffer` command can be used to turn the
234current window into a terminal window. If there are unsaved changes this
235fails, use ! to force, as usual.
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200236
237To have a background job run without a window, and open the window when it's
238done, use options like this: >
239 :term ++hidden ++open make
240Note that the window will open at an unexpected moment, this will interrupt
241what you are doing.
242
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200243 *E947* *E948*
Bram Moolenaar78712a72017-08-05 14:50:12 +0200244So long as the job is running, the buffer is considered modified and Vim
245cannot be quit easily, see |abandon|.
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200246
247When the job has finished and no changes were made to the buffer: closing the
248window will wipe out the buffer.
249
250Before changes can be made to a terminal buffer, the 'modifiable' option must
251be set. This is only possible when the job has finished. At the first change
252the buffer will become a normal buffer and the highlighting is removed.
253You may want to change the buffer name with |:file| to be able to write, since
254the buffer name will still be set to the command.
255
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200256
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200257Resizing ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100258 *terminal-resizing*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200259The size of the terminal can be in one of three modes:
260
2611. The 'termsize' option is empty: The terminal size follows the window size.
262 The minimal size is 2 screen lines with 10 cells.
263
2642. The 'termsize' option is "rows*cols", where "rows" is the minimal number of
Bram Moolenaar8a773062017-07-24 22:29:21 +0200265 screen rows and "cols" is the minimal number of cells.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200266
2673. The 'termsize' option is "rowsXcols" (where the x is upper or lower case).
268 The terminal size is fixed to the specified number of screen lines and
269 cells. If the window is bigger there will be unused empty space.
270
271If the window is smaller than the terminal size, only part of the terminal can
272be seen (the lower-left part).
273
274The |term_getsize()| function can be used to get the current size of the
275terminal. |term_setsize()| can be used only when in the first or second mode,
276not when 'termsize' is "rowsXcols".
277
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200278
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200279Terminal-Job and Terminal-Normal mode ~
Bram Moolenaar423802d2017-07-30 16:52:24 +0200280 *Terminal-mode*
281When the job is running the contents of the terminal is under control of the
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200282job. That includes the cursor position. Typed keys are sent to the job.
283The terminal contents can change at any time. This is called Terminal-Job
284mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200285
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200286Use CTRL-W N (or 'termkey' N) to switch to Terminal-Normal mode. Now the
287contents of the terminal window is under control of Vim, the job output is
288suspended. CTRL-\ CTRL-N does the same.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200289
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200290Terminal-Job mode is where |:tmap| mappings are applied. Keys sent by
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200291|term_sendkeys()| are not subject to tmap, but keys from |feedkeys()| are.
292
Bram Moolenaar423802d2017-07-30 16:52:24 +0200293 *E946*
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200294In Terminal-Normal mode you can move the cursor around with the usual Vim
295commands, Visually mark text, yank text, etc. But you cannot change the
296contents of the buffer. The commands that would start insert mode, such as
297'i' and 'a', return to Terminal-Job mode. The window will be updated to show
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200298the contents of the terminal. |:startinsert| is ineffective.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200299
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200300In Terminal-Normal mode the statusline and window title show "(Terminal)". If
301the job ends while in Terminal-Normal mode this changes to
302"(Terminal-finished)".
Bram Moolenaar423802d2017-07-30 16:52:24 +0200303
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200304It is not possible to enter Insert mode from Terminal-Job mode.
305
Bram Moolenaar423802d2017-07-30 16:52:24 +0200306
Bram Moolenaarc572da52017-08-27 16:52:01 +0200307Cursor style ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100308 *terminal-cursor-style*
Bram Moolenaarc572da52017-08-27 16:52:01 +0200309By default the cursor in the terminal window uses a not blinking block. The
310normal xterm escape sequences can be used to change the blinking state and the
311shape. Once focus leaves the terminal window Vim will restore the original
312cursor.
313
314An exception is when xterm is started with the "-bc" argument, or another way
315that causes the cursor to blink. This actually means that the blinking flag
316is inverted. Since Vim cannot detect this, the terminal window cursor
317blinking will also be inverted.
318
319
Bram Moolenaarb5b75622018-03-09 22:22:21 +0100320Session ~
321 *terminal-session*
322A terminal window will be restored when using a session file, if possible and
323wanted.
324
325If "terminal" was removed from 'sessionoptions' then no terminal windows will
326be restored.
327
328If the job in the terminal was finished the window will not be restored.
329
330If the terminal can be restored, the command that was used to open it will be
331used again. To change this use the |term_setrestore()| function. This can
332also be used to not restore a specific terminal by setting the command to
333"NONE".
334
335
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100336Special keys ~
337 *terminal-special-keys*
338Since the terminal emulator simulates an xterm, only escape sequences that
339both Vim and xterm recognize will be available in the terminal window. If you
340want to pass on other escape sequences to the job running in the terminal you
341need to set up forwarding. Example: >
342 tmap <expr> <Esc>]b SendToTerm("\<Esc>]b")
Bram Moolenaar60e73f22017-11-12 18:02:06 +0100343 func SendToTerm(what)
344 call term_sendkeys('', a:what)
345 return ''
346 endfunc
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200347
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100348
349Unix ~
350 *terminal-unix*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200351On Unix a pty is used to make it possible to run all kinds of commands. You
352can even run Vim in the terminal! That's used for debugging, see below.
353
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200354Environment variables are used to pass information to the running job:
355 TERM name of the terminal, 'term'
356 ROWS number of rows in the terminal initially
357 LINES same as ROWS
358 COLUMNS number of columns in the terminal initially
359 COLORS number of colors, 't_Co' (256*256*256 in the GUI)
360 VIM_SERVERNAME v:servername
361
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200362
363MS-Windows ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100364 *terminal-ms-windows*
Bram Moolenaar8a773062017-07-24 22:29:21 +0200365On MS-Windows winpty is used to make it possible to run all kind of commands.
366Obviously, they must be commands that run in a terminal, not open their own
367window.
368
369You need the following two files from winpty:
370
371 winpty.dll
372 winpty-agent.exe
373
374You can download them from the following page:
375
376 https://github.com/rprichard/winpty
377
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200378Just put the files somewhere in your PATH. You can set the 'winptydll' option
379to point to the right file, if needed. If you have both the 32-bit and 64-bit
380version, rename to winpty32.dll and winpty64.dll to match the way Vim was
381build.
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200382
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100383Environment variables are used to pass information to the running job:
384 VIM_SERVERNAME v:servername
385
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200386==============================================================================
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003872. Terminal communication *terminal-communication*
388
389There are several ways to communicate with the job running in a terminal:
390- Use |term_sendkeys()| to send text and escape sequences from Vim to the job.
391- Use the JSON API to send encoded commands from the job to Vim.
392- Use the |client-server| mechanism. This works on machines with an X server
393 and on MS-Windows.
394
395
396Vim to job: term_sendkeys() ~
397 *terminal-to-job*
398This allows for remote controlling the job running in the terminal. It is a
399one-way mechanism. The job can update the display to signal back to Vim.
400For example, if a shell is running in a terminal, you can do: >
401 call term_sendkeys(buf, "ls *.java\<CR>")
402
403This requires for the job to be in the right state where it will do the right
404thing when receiving the keys. For the above example, the shell must be
405waiting for a command to be typed.
406
407For a job that was written for the purpose, you can use the JSON API escape
408sequence in the other direction. E.g.: >
409 call term_sendkeys(buf, "\<Esc>]51;["response"]\x07")
410
411
412Job to Vim: JSON API ~
413 *terminal-api*
414The job can send JSON to Vim, using a special escape sequence. The JSON
415encodes a command that Vim understands. Example of such a message: >
416 <Esc>]51;["drop", "README.md"]<07>
417
418The body is always a list, making it easy to find the end: ]<07>.
419The <Esc>]51;msg<07> sequence is reserved by xterm for "Emacs shell", which is
420similar to what we are doing here.
421
422Currently supported commands:
423
424 call {funcname} {argument}
425
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200426 Call a user defined function with {argument}.
427 The function is called with two arguments: the buffer number
428 of the terminal and {argument}, the decoded JSON argument.
429 The function name must start with "Tapi_" to avoid
430 accidentally calling a function not meant to be used for the
431 terminal API
432 The user function should sanity check the argument.
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200433 The function can use |term_sendkeys()| to send back a reply.
434 Example in JSON: >
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200435 ["call", "Tapi_Impression", ["play", 14]]
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200436< Calls a function defined like this: >
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200437 function Tapi_Impression(bufnum, arglist)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200438 if len(a:arglist) == 2
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200439 echomsg "impression " . a:arglist[0]
440 echomsg "count " . a:arglist[1]
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200441 endif
442 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200443< Output from `:echo` may be erased by a redraw, use `:echomsg`
444 to be able to see it with `:messages`.
445
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200446 drop {filename}
447
448 Let Vim open a file, like the `:drop` command. If {filename}
449 is already open in a window, switch to that window. Otherwise
450 open a new window to edit {filename}.
451 Example in JSON: >
452 ["drop", "path/file.txt", {"ff": "dos"}]
453
454A trick to have Vim send this escape sequence: >
455 exe "set t_ts=\<Esc>]51; t_fs=\x07"
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200456 let &titlestring = '["call","Tapi_TryThis",["hello",123]]'
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200457 redraw
458 set t_ts& t_fs&
459
460Rationale: Why not allow for any command or expression? Because that might
461create a security problem.
462
463
464Using the client-server feature ~
465 *terminal-client-server*
466This only works when v:servername is not empty. If needed you can set it,
467before opening the terminal, with: >
468 call remote_startserver('vim-server')
469
470$VIM_SERVERNAME is set in the terminal to pass on the server name.
471
472In the job you can then do something like: >
473 vim --servername $VIM_SERVERNAME --remote +123 some_file.c
474This will open the file "some_file.c" and put the cursor on line 123.
475
476==============================================================================
4773. Remote testing *terminal-testing*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200478
479Most Vim tests execute a script inside Vim. For some tests this does not
480work, running the test interferes with the code being tested. To avoid this
481Vim is executed in a terminal window. The test sends keystrokes to it and
482inspects the resulting screen state.
483
484Functions ~
485
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200486term_sendkeys() send keystrokes to a terminal (not subject to tmap)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200487term_wait() wait for screen to be updated
488term_scrape() inspect terminal screen
489
490
491==============================================================================
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004924. Diffing screen dumps *terminal-diff*
Bram Moolenaarda650582018-02-20 15:51:40 +0100493
494In some cases it can be bothersome to test that Vim displays the right
495characters on the screen. E.g. with syntax highlighting. To make this
496simpler it is possible to take a screen dump of a terminal and compare it to
497an expected screen dump.
498
499Vim uses the window size, text, color and other attributes as displayed. The
500Vim screen size, font and other properties do not matter. Therefore this
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100501mechanism is portable across systems. A conventional screenshot would reflect
Bram Moolenaarda650582018-02-20 15:51:40 +0100502all differences, including font size and family.
503
504
505Writing a screen dump test for Vim ~
506 *terminal-dumptest*
507For an example see the Test_syntax_c() function in
508src/testdir/test_syntax.vim. The main parts are:
509- Write a file you want to test with. This is useful for testing syntax
510 highlighting. You can also start Vim with en empty buffer.
511- Run Vim in a terminal with a specific size. The default is 20 lines of 75
512 characters. This makes sure the dump is always this size. The function
513 RunVimInTerminal() takes care of this. Pass it the arguments for the Vim
514 command.
515- Send any commands to Vim using term_sendkeys(). For example: >
516 call term_sendkeys(buf, ":echo &lines &columns\<CR>")
517- Check that the screen is now in the expected state, using
518 VerifyScreenDump(). This expects the reference screen dump to be in the
519 src/testdir/dumps/ directory. Pass the name without ".dump". It is
520 recommended to use the name of the test function and a sequence number, so
521 that we know what test is using the file.
522- Repeat sending commands and checking the state.
523- Finally stop Vim by calling StopVimInTerminal().
524
525The first time you do this you won't have a screen dump yet. Create an empty
526file for now, e.g.: >
527 touch src/testdir/dumps/Test_function_name_01.dump
528
529The test will then fail, giving you the command to compare the reference dump
530and the failed dump, e.g.: >
531 call term_dumpdiff("Test_func.dump.failed", "dumps/Test_func.dump")
532
533Use this command in Vim, with the current directory set to src/testdir.
534Once you are satisfied with the test, move the failed dump in place of the
535reference: >
536 :!mv Test_func.dump.failed dumps/Test_func.dump
537
538
539Creating a screen dump ~
540 *terminal-screendump*
541
542To create the screen dump, run Vim (or any other program) in a terminal and
543make it show the desired state. Then use the term_dumpwrite() function to
544create a screen dump file. For example: >
545 :call term_dumpwrite(77, "mysyntax.dump")
546
547Here "77" is the buffer number of the terminal. Use `:ls!` to see it.
548
549You can view the screen dump with term_dumpload(): >
550 :call term_dumpload("mysyntax.dump")
551
552To verify that Vim still shows exactly the same screen, run Vim again with
553exactly the same way to show the desired state. Then create a screen dump
554again, using a different file name: >
555 :call term_dumpwrite(88, "test.dump")
556
557To assert that the files are exactly the same use assert_equalfile(): >
558 call assert_equalfile("mysyntax.dump", "test.dump")
559
560If there are differences then v:errors will contain the error message.
561
562
563Comparing screen dumps ~
564 *terminal-diffscreendump*
565
566assert_equalfile() does not make it easy to see what is different.
567To spot the problem use term_dumpdiff(): >
568 call term_dumpdiff("mysyntax.dump", "test.dump")
569
570This will open a window consisting of three parts:
5711. The contents of the first dump
5722. The difference between the first and second dump
5733. The contents of the second dump
574
575You can usually see what differs in the second part. Use the 'ruler' to
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100576relate it to the position in the first or second dump.
Bram Moolenaarda650582018-02-20 15:51:40 +0100577
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100578Alternatively, press "s" to swap the first and second dump. Do this several
Bram Moolenaarda650582018-02-20 15:51:40 +0100579times so that you can spot the difference in the context of the text.
580
581==============================================================================
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02005825. Debugging *terminal-debug*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200583
584The Terminal debugging plugin can be used to debug a program with gdb and view
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200585the source code in a Vim window. Since this is completely contained inside
586Vim this also works remotely over an ssh connection.
587
588
589Starting ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100590 *termdebug-starting*
Bram Moolenaarc572da52017-08-27 16:52:01 +0200591Load the plugin with this command: >
592 packadd termdebug
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200593< *:Termdebug*
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100594To start debugging use `:Termdebug` followed by the command name, for example: >
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200595 :Termdebug vim
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200596
Bram Moolenaarc572da52017-08-27 16:52:01 +0200597This opens two windows:
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100598
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200599gdb window A terminal window in which "gdb vim" is executed. Here you
600 can directly interact with gdb. The buffer name is "!gdb".
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100601
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200602program window A terminal window for the executed program. When "run" is
603 used in gdb the program I/O will happen in this window, so
604 that it does not interfere with controlling gdb. The buffer
605 name is "gdb program".
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200606
607The current window is used to show the source code. When gdb pauses the
608source file location will be displayed, if possible. A sign is used to
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100609highlight the current position, using highlight group debugPC.
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200610
611If the buffer in the current window is modified, another window will be opened
612to display the current gdb position.
613
614Focus the terminal of the executed program to interact with it. This works
615the same as any command running in a terminal window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200616
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200617When the debugger ends, typically by typing "quit" in the gdb window, the two
618opened windows are closed.
Bram Moolenaarc572da52017-08-27 16:52:01 +0200619
620
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200621Example session ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100622 *termdebug-example*
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200623Start in the Vim "src" directory and build Vim: >
624 % make
625Start Vim: >
626 % ./vim
627Load the termdebug plugin and start debugging Vim: >
628 :packadd termdebug
629 :Termdebug vim
630You should now have three windows:
631 source - where you started, has a window toolbar with buttons
632 gdb - you can type gdb commands here
633 program - the executed program will use this window
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100634
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200635You can use CTRL-W CTRL-W or the mouse to move focus between windows.
636Put focus on the gdb window and type: >
637 break ex_help
638 run
639Vim will start running in the program window. Put focus there and type: >
640 :help gui
641Gdb will run into the ex_help breakpoint. The source window now shows the
642ex_cmds.c file. A ">>" marker will appear where the breakpoint was set. The
643line where the debugger stopped is highlighted. You can now step through the
644program. Let's use the mouse: click on the "Next" button in the window
645toolbar. You will see the highlighting move as the debugger executes a line
646of source code.
647
648Click "Next" a few times until the for loop is highlighted. Put the cursor on
649the end of "eap->arg", then click "Eval" in the toolbar. You will see this
650displayed:
651 "eap->arg": 0x555555e68855 "gui" ~
652This way you can inspect the value of local variables. You can also focus the
653gdb window and use a "print" command, e.g.: >
654 print *eap
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100655If mouse pointer movements are working, Vim will also show a balloon when the
656mouse rests on text that can be evaluated by gdb.
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200657
658Now go back to the source window and put the cursor on the first line after
659the for loop, then type: >
660 :Break
661You will see a ">>" marker appear, this indicates the new breakpoint. Now
662click "Cont" in the toolbar and the code until the breakpoint will be
663executed.
664
665You can type more advanced commands in the gdb window. For example, type: >
666 watch curbuf
667Now click "Cont" in the toolbar (or type "cont" in the gdb window). Execution
668will now continue until the value of "curbuf" changes, which is in do_ecmd().
669To remove this watchpoint again type in the gdb window: >
670 delete 3
671
672You can see the stack by typing in the gdb window: >
673 where
674Move through the stack frames, e.g. with: >
675 frame 3
676The source window will show the code, at the point where the call was made to
677a deeper level.
678
679
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200680Stepping through code ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100681 *termdebug-stepping*
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200682Put focus on the gdb window to type commands there. Some common ones are:
Bram Moolenaar60e73f22017-11-12 18:02:06 +0100683- CTRL-C interrupt the program
684- next execute the current line and stop at the next line
685- step execute the current line and stop at the next statement,
686 entering functions
687- finish execute until leaving the current function
688- where show the stack
689- frame N go to the Nth stack frame
690- continue continue execution
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200691
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100692In the window showing the source code these commands can be used to control gdb:
693 `:Run` [args] run the program with [args] or the previous arguments
694 `:Arguments` {args} set arguments for the next `:Run`
Bram Moolenaar60e73f22017-11-12 18:02:06 +0100695
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100696 `:Break` set a breakpoint at the current line; a sign will be displayed
697 `:Clear` delete the breakpoint at the current line
Bram Moolenaar60e73f22017-11-12 18:02:06 +0100698
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100699 `:Step` execute the gdb "step" command
700 `:Over` execute the gdb "next" command (`:Next` is a Vim command)
701 `:Finish` execute the gdb "finish" command
702 `:Continue` execute the gdb "continue" command
703 `:Stop` interrupt the program
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200704
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100705If 'mouse' is set the plugin adds a window toolbar with these entries:
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100706 Step `:Step`
707 Next `:Over`
708 Finish `:Finish`
709 Cont `:Continue`
710 Stop `:Stop`
711 Eval `:Evaluate`
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100712This way you can use the mouse to perform the most common commands. You need
713to have the 'mouse' option set to enable mouse clicks.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200714
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100715You can add the window toolbar in other windows you open with: >
716 :Winbar
717
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200718
719Inspecting variables ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100720 *termdebug-variables*
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100721 `:Evaluate` evaluate the expression under the cursor
722 `K` same
723 `:Evaluate` {expr} evaluate {expr}
724 `:'<,'>Evaluate` evaluate the Visually selected text
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200725
726This is similar to using "print" in the gdb window.
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100727You can usually shorten `:Evaluate` to `:Ev`.
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200728
729
730Other commands ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100731 *termdebug-commands*
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200732 :Gdb jump to the gdb window
733 :Program jump to the window with the running program
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200734
735
736Communication ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100737 *termdebug-communication*
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200738There is another, hidden, buffer, which is used for Vim to communicate with
739gdb. The buffer name is "gdb communication". Do not delete this buffer, it
740will break the debugger.
741
742
Bram Moolenaarc572da52017-08-27 16:52:01 +0200743Customizing ~
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100744
745GDB command *termdebug-customizing*
746
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200747To change the name of the gdb command, set the "termdebugger" variable before
748invoking `:Termdebug`: >
749 let termdebugger = "mygdb"
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100750< *gdb-version*
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200751Only debuggers fully compatible with gdb will work. Vim uses the GDB/MI
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100752interface. The "new-ui" command requires gdb version 7.12 or later. if you
753get this error:
Bram Moolenaar01164a62017-11-02 22:58:42 +0100754 Undefined command: "new-ui". Try "help".~
755Then your gdb is too old.
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200756
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100757
758Colors *hl-debugPC* *hl-debugBreakpoint*
759
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200760The color of the signs can be adjusted with these highlight groups:
761- debugPC the current position
762- debugBreakpoint a breakpoint
763
764The defaults are, when 'background' is "light":
765 hi debugPC term=reverse ctermbg=lightblue guibg=lightblue
766 hi debugBreakpoint term=reverse ctermbg=red guibg=red
767
768When 'background' is "dark":
769 hi debugPC term=reverse ctermbg=darkblue guibg=darkblue
770 hi debugBreakpoint term=reverse ctermbg=red guibg=red
Bram Moolenaarc572da52017-08-27 16:52:01 +0200771
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100772
773Popup menu *termdebug_popup*
774
775By default the Termdebug plugin sets 'mousemodel' to "popup_setpos" and adds
776these entries to the popup menu:
777 Set breakpoint `:Break`
778 Clear breakpoint `:Clear`
779 Evaluate `:Evaluate`
780If you don't want this then disable it with: >
781 let g:termdebug_popup = 0
782
783
784Vim window width *termdebug_wide*
785
Bram Moolenaar38baa3e2017-09-14 16:10:38 +0200786To change the width of the Vim window when debugging starts, and use a
787vertical split: >
788 let g:termdebug_wide = 163
789This will set &columns to 163 when :Termdebug is used. The value is restored
790when quitting the debugger.
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200791If g:termdebug_wide is set and &Columns is already larger than
792g:termdebug_wide then a vertical split will be used without changing &columns.
793Set it to 1 to get a vertical split without every changing &columns (useful
794for when the terminal can't be resized by Vim).
Bram Moolenaar38baa3e2017-09-14 16:10:38 +0200795
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200796
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200797
798 vim:tw=78:ts=8:ft=help:norl: