blob: 23e210dc84832b91597c9af903d963270a658b5d [file] [log] [blame]
Bram Moolenaar51ad4ea2018-04-06 11:14:11 +02001*terminal.txt* For Vim version 8.0. Last change: 2018 Apr 05
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|
Bram Moolenaar333b80a2018-04-04 22:57:29 +020028 Job to Vim: JSON API |terminal-api|
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +020029 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 Moolenaarb59118d2018-04-13 22:11:56 +020075 CTRL-W CTRL-\ send a CTRL-\ to the job in the terminal
Bram Moolenaardd693ce2017-08-10 23:15:19 +020076 CTRL-W N go to Terminal-Normal mode, see |Terminal-mode|
77 CTRL-\ CTRL-N go to Terminal-Normal mode, see |Terminal-mode|
Bram Moolenaarf55e4c82017-08-01 20:44:53 +020078 CTRL-W " {reg} paste register {reg} *CTRL-W_quote*
79 Also works with the = register to insert the result of
80 evaluating an expression.
Bram Moolenaar8e539c52017-08-18 22:57:06 +020081 CTRL-W CTRL-C ends the job, see below |t_CTRL-W_CTRL-C|
Bram Moolenaar423802d2017-07-30 16:52:24 +020082
83See option 'termkey' for specifying another key instead of CTRL-W that
84will work like CTRL-W. However, typing 'termkey' twice sends 'termkey' to
85the job. For example:
86 'termkey' CTRL-W move focus to the next window
87 'termkey' : enter an Ex command
88 'termkey' 'termkey' send 'termkey' to the job in the terminal
89 'termkey' . send a CTRL-W to the job in the terminal
Bram Moolenaar60e73f22017-11-12 18:02:06 +010090 'termkey' N go to terminal Normal mode, see below
Bram Moolenaar423802d2017-07-30 16:52:24 +020091 'termkey' CTRL-N same as CTRL-W N
Bram Moolenaar8e539c52017-08-18 22:57:06 +020092 'termkey' CTRL-C same as |t_CTRL-W_CTRL-C|
Bram Moolenaar69198192017-08-05 14:10:48 +020093 *t_CTRL-\_CTRL-N*
Bram Moolenaardd693ce2017-08-10 23:15:19 +020094The special key combination CTRL-\ CTRL-N can be used to switch to Normal
95mode, just like this works in any other mode.
Bram Moolenaar8e539c52017-08-18 22:57:06 +020096 *t_CTRL-W_CTRL-C*
97CTRL-W CTRL-C can be typed to forcefully end the job. On MS-Windows a
98CTRL-BREAK will also kill the job.
99
100If you type CTRL-C the effect depends on what the pty has been configured to
101do. For simple commands this causes a SIGINT to be sent to the job, which
102would end it. Other commands may ignore the SIGINT or handle the CTRL-C
103themselves (like Vim does).
Bram Moolenaar423802d2017-07-30 16:52:24 +0200104
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200105To change the keys you type use terminal mode mappings, see |:tmap|.
106These are defined like any mapping, but apply only when typing keys that are
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100107sent to the job running in the terminal. For example, to make F1 switch
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200108to Terminal-Normal mode: >
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100109 tnoremap <F1> <C-W>N
110You can use Esc, but you need to make sure it won't cause other keys to
111break: >
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200112 tnoremap <Esc> <C-W>N
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100113 set notimeout ttimeout timeoutlen=100
114
Bram Moolenaar01164a62017-11-02 22:58:42 +0100115< *options-in-terminal*
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200116After opening the terminal window and setting 'buftype' to "terminal" the
117BufWinEnter autocommand event is triggered. This makes it possible to set
118options specifically for the window and buffer. Example: >
119 au BufWinEnter * if &buftype == 'terminal' | setlocal bufhidden=hide | endif
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200120
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100121Mouse events (click and drag) are passed to the terminal. Mouse move events
122are only passed when Vim itself is receiving them. For a terminal that is
123when 'balloonevalterm' is enabled.
124
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200125
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200126Size and color ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100127 *terminal-size-color*
Bram Moolenaar74675a62017-07-15 13:53:23 +0200128See option 'termsize' for controlling the size of the terminal window.
129(TODO: scrolling when the terminal is larger than the window)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200130
Bram Moolenaar38baa3e2017-09-14 16:10:38 +0200131The job running in the terminal can change the colors. The default foreground
132and background colors are taken from Vim, the Normal highlight group.
133
134For a color terminal the 'background' option is used to decide whether the
135terminal window will start with a white or black background.
136
Bram Moolenaardf980db2017-12-24 13:22:00 +0100137To use a different color the Terminal highlight group can be used, for
138example: >
Bram Moolenaar38baa3e2017-09-14 16:10:38 +0200139 hi Terminal ctermbg=lightgrey ctermfg=blue guibg=lightgrey guifg=blue
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200140<
141 *g:terminal_ansi_colors*
142In GUI mode or with |termguicolors|, the 16 ANSI colors used by default in new
143terminal windows may be configured using the variable
144`g:terminal_ansi_colors`, which should be a list of 16 color names or
145hexadecimal color codes, similar to those accepted by |highlight-guifg|. When
146not using GUI colors, the terminal window always uses the 16 ANSI colors of
147the underlying terminal.
148The |term_setansicolors()| function can be used to change the colors, and
149|term_getansicolors()| to get the currently used colors.
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200150
Bram Moolenaar423802d2017-07-30 16:52:24 +0200151
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200152Syntax ~
Bram Moolenaar8a773062017-07-24 22:29:21 +0200153
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200154:[range]ter[minal] [options] [command] *:ter* *:terminal*
Bram Moolenaar8a773062017-07-24 22:29:21 +0200155 Open a new terminal window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200156
157 If [command] is provided run it as a job and connect
158 the input and output to the terminal.
159 If [command] is not given the 'shell' option is used.
Bram Moolenaarc572da52017-08-27 16:52:01 +0200160 if [command] is NONE no job is started, the pty of the
161 terminal can be used by a command like gdb.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200162
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100163 If [command] is missing the default behavior is to
164 close the terminal when the shell exits. This can be
165 changed with the ++noclose argument.
166 If [command] is present the default behavior is to
167 keep the terminal open in Terminal-Normal mode. This
168 can be changed with the ++close argument.
169
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200170 A new buffer will be created, using [command] or
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200171 'shell' as the name, prefixed with a "!". If a buffer
172 by this name already exists a number is added in
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200173 parentheses. E.g. if "gdb" exists the second terminal
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200174 buffer will use "!gdb (1)".
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200175
Bram Moolenaarb2412082017-08-20 18:09:14 +0200176 If [range] is given the specified lines are used as
177 input for the job. It will not be possible to type
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200178 keys in the terminal window. For MS-Windows see the
179 ++eof argument below.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200180
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200181 *term++close* *term++open*
182 Supported [options] are:
183 ++close The terminal window will close
184 automatically when the job terminates.
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100185 ++noclose The terminal window will NOT close
186 automatically when the job terminates.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200187 ++open When the job terminates and no window
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200188 shows it, a window will be opened.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200189 Note that this can be interruptive.
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100190 The last of ++close, ++noclose and ++open
191 matters and rules out earlier arguments.
192
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200193 ++curwin Open the terminal in the current
194 window, do not split the current
195 window. Fails if the current buffer
196 cannot be |abandon|ed.
197 ++hidden Open the terminal in a hidden buffer,
198 no window will be used.
Bram Moolenaarb5b75622018-03-09 22:22:21 +0100199 ++norestore Do not include this terminal window
200 in a session file.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100201 ++kill={how} When trying to close the terminal
202 window kill the job with {how}. See
203 |term_setkill()| for the values.
Bram Moolenaarb2412082017-08-20 18:09:14 +0200204 ++rows={height} Use {height} for the terminal window
Bram Moolenaar40962ec2018-01-28 22:47:25 +0100205 height. If the terminal uses the full
206 Vim height (no window above or below
207 th terminal window) the command line
208 height will be reduced as needed.
Bram Moolenaarb2412082017-08-20 18:09:14 +0200209 ++cols={width} Use {width} for the terminal window
Bram Moolenaar40962ec2018-01-28 22:47:25 +0100210 width. If the terminal uses the full
211 Vim width (no window left or right of
212 the terminal window) this value is
213 ignored.
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200214 ++eof={text} when using [range]: text to send after
215 the last line was written. Cannot
216 contain white space. A CR is
217 appended. For MS-Windows the default
218 is to send CTRL-D.
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200219 E.g. for a shell use "++eof=exit" and
220 for Python "++eof=exit()". Special
221 codes can be used like with `:map`,
222 e.g. "<C-Z>" for CTRL-Z.
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200223
224 If you want to use more options use the |term_start()|
225 function.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200226
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100227When the buffer associated with the terminal is forcibly unloaded or wiped out
228the job is killed, similar to calling `job_stop(job, "kill")` .
229Closing the window normally results in |E947|. When a kill method was set
230with "++kill={how}" or |term_setkill()| then closing the window will use that
231way to kill or interrupt the job. For example: >
232 :term ++kill=term tail -f /tmp/log
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200233
Bram Moolenaare561a7e2017-08-29 22:44:59 +0200234So long as the job is running the window behaves like it contains a modified
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200235buffer. Trying to close the window with `CTRL-W :quit` fails. When using
236`CTRL-W :quit!` the job is ended. The text in the window is lost. The buffer
237still exists, but getting it in a window with `:buffer` will show an empty
238buffer.
239
240Trying to close the window with `CTRL-W :close` also fails. Using
241`CTRL-W :close!` will close the window and make the buffer hidden.
Bram Moolenaare561a7e2017-08-29 22:44:59 +0200242
243You can use `CTRL-W :hide` to close the terminal window and make the buffer
244hidden, the job keeps running. The `:buffer` command can be used to turn the
245current window into a terminal window. If there are unsaved changes this
246fails, use ! to force, as usual.
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200247
248To have a background job run without a window, and open the window when it's
249done, use options like this: >
250 :term ++hidden ++open make
251Note that the window will open at an unexpected moment, this will interrupt
252what you are doing.
253
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200254 *E947* *E948*
Bram Moolenaar78712a72017-08-05 14:50:12 +0200255So long as the job is running, the buffer is considered modified and Vim
256cannot be quit easily, see |abandon|.
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200257
258When the job has finished and no changes were made to the buffer: closing the
259window will wipe out the buffer.
260
261Before changes can be made to a terminal buffer, the 'modifiable' option must
262be set. This is only possible when the job has finished. At the first change
263the buffer will become a normal buffer and the highlighting is removed.
264You may want to change the buffer name with |:file| to be able to write, since
265the buffer name will still be set to the command.
266
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200267
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200268Resizing ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100269 *terminal-resizing*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200270The size of the terminal can be in one of three modes:
271
2721. The 'termsize' option is empty: The terminal size follows the window size.
273 The minimal size is 2 screen lines with 10 cells.
274
2752. The 'termsize' option is "rows*cols", where "rows" is the minimal number of
Bram Moolenaar8a773062017-07-24 22:29:21 +0200276 screen rows and "cols" is the minimal number of cells.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200277
2783. The 'termsize' option is "rowsXcols" (where the x is upper or lower case).
279 The terminal size is fixed to the specified number of screen lines and
280 cells. If the window is bigger there will be unused empty space.
281
282If the window is smaller than the terminal size, only part of the terminal can
283be seen (the lower-left part).
284
285The |term_getsize()| function can be used to get the current size of the
286terminal. |term_setsize()| can be used only when in the first or second mode,
287not when 'termsize' is "rowsXcols".
288
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200289
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200290Terminal-Job and Terminal-Normal mode ~
Bram Moolenaar8c041b62018-04-14 18:14:06 +0200291 *Terminal-mode* *Terminal-Job*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200292When the job is running the contents of the terminal is under control of the
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200293job. That includes the cursor position. Typed keys are sent to the job.
294The terminal contents can change at any time. This is called Terminal-Job
295mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200296
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200297Use CTRL-W N (or 'termkey' N) to switch to Terminal-Normal mode. Now the
298contents of the terminal window is under control of Vim, the job output is
299suspended. CTRL-\ CTRL-N does the same.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200300
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200301Terminal-Job mode is where |:tmap| mappings are applied. Keys sent by
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200302|term_sendkeys()| are not subject to tmap, but keys from |feedkeys()| are.
303
Bram Moolenaar8c041b62018-04-14 18:14:06 +0200304It is not possible to enter Insert mode from Terminal-Job mode.
305
306 *Terminal-Normal* *E946*
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200307In Terminal-Normal mode you can move the cursor around with the usual Vim
308commands, Visually mark text, yank text, etc. But you cannot change the
309contents of the buffer. The commands that would start insert mode, such as
310'i' and 'a', return to Terminal-Job mode. The window will be updated to show
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200311the contents of the terminal. |:startinsert| is ineffective.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200312
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200313In Terminal-Normal mode the statusline and window title show "(Terminal)". If
314the job ends while in Terminal-Normal mode this changes to
315"(Terminal-finished)".
Bram Moolenaar423802d2017-07-30 16:52:24 +0200316
Bram Moolenaar8c041b62018-04-14 18:14:06 +0200317When the job outputs lines in the terminal, such that the contents scrolls off
318the top, those lines are remembered and can be seen in Terminal-Normal mode.
319The number of lines is limited by the 'terminalscroll' option. When going over
320this limit, the first 10% of the scrolled lins are deleted and are lost.
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200321
Bram Moolenaar423802d2017-07-30 16:52:24 +0200322
Bram Moolenaarc572da52017-08-27 16:52:01 +0200323Cursor style ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100324 *terminal-cursor-style*
Bram Moolenaarc572da52017-08-27 16:52:01 +0200325By default the cursor in the terminal window uses a not blinking block. The
326normal xterm escape sequences can be used to change the blinking state and the
327shape. Once focus leaves the terminal window Vim will restore the original
328cursor.
329
330An exception is when xterm is started with the "-bc" argument, or another way
331that causes the cursor to blink. This actually means that the blinking flag
332is inverted. Since Vim cannot detect this, the terminal window cursor
333blinking will also be inverted.
334
335
Bram Moolenaarb5b75622018-03-09 22:22:21 +0100336Session ~
337 *terminal-session*
338A terminal window will be restored when using a session file, if possible and
339wanted.
340
341If "terminal" was removed from 'sessionoptions' then no terminal windows will
342be restored.
343
344If the job in the terminal was finished the window will not be restored.
345
346If the terminal can be restored, the command that was used to open it will be
347used again. To change this use the |term_setrestore()| function. This can
348also be used to not restore a specific terminal by setting the command to
349"NONE".
350
351
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100352Special keys ~
353 *terminal-special-keys*
354Since the terminal emulator simulates an xterm, only escape sequences that
355both Vim and xterm recognize will be available in the terminal window. If you
356want to pass on other escape sequences to the job running in the terminal you
357need to set up forwarding. Example: >
358 tmap <expr> <Esc>]b SendToTerm("\<Esc>]b")
Bram Moolenaar60e73f22017-11-12 18:02:06 +0100359 func SendToTerm(what)
360 call term_sendkeys('', a:what)
361 return ''
362 endfunc
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200363
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100364
365Unix ~
366 *terminal-unix*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200367On Unix a pty is used to make it possible to run all kinds of commands. You
368can even run Vim in the terminal! That's used for debugging, see below.
369
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200370Environment variables are used to pass information to the running job:
Bram Moolenaar9a993e32018-04-05 22:15:22 +0200371 TERM the name of the terminal, from the 'term' option or
372 $TERM in the GUI; falls back to "xterm" if it does not
373 start with "xterm"
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200374 ROWS number of rows in the terminal initially
375 LINES same as ROWS
376 COLUMNS number of columns in the terminal initially
377 COLORS number of colors, 't_Co' (256*256*256 in the GUI)
378 VIM_SERVERNAME v:servername
379
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200380
381MS-Windows ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100382 *terminal-ms-windows*
Bram Moolenaar8a773062017-07-24 22:29:21 +0200383On MS-Windows winpty is used to make it possible to run all kind of commands.
384Obviously, they must be commands that run in a terminal, not open their own
385window.
386
387You need the following two files from winpty:
388
389 winpty.dll
390 winpty-agent.exe
391
392You can download them from the following page:
393
394 https://github.com/rprichard/winpty
395
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200396Just put the files somewhere in your PATH. You can set the 'winptydll' option
397to point to the right file, if needed. If you have both the 32-bit and 64-bit
398version, rename to winpty32.dll and winpty64.dll to match the way Vim was
399build.
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200400
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100401Environment variables are used to pass information to the running job:
402 VIM_SERVERNAME v:servername
403
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200404==============================================================================
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004052. Terminal communication *terminal-communication*
406
407There are several ways to communicate with the job running in a terminal:
408- Use |term_sendkeys()| to send text and escape sequences from Vim to the job.
409- Use the JSON API to send encoded commands from the job to Vim.
410- Use the |client-server| mechanism. This works on machines with an X server
411 and on MS-Windows.
412
413
414Vim to job: term_sendkeys() ~
415 *terminal-to-job*
416This allows for remote controlling the job running in the terminal. It is a
417one-way mechanism. The job can update the display to signal back to Vim.
418For example, if a shell is running in a terminal, you can do: >
419 call term_sendkeys(buf, "ls *.java\<CR>")
420
421This requires for the job to be in the right state where it will do the right
422thing when receiving the keys. For the above example, the shell must be
423waiting for a command to be typed.
424
425For a job that was written for the purpose, you can use the JSON API escape
426sequence in the other direction. E.g.: >
427 call term_sendkeys(buf, "\<Esc>]51;["response"]\x07")
428
429
430Job to Vim: JSON API ~
431 *terminal-api*
432The job can send JSON to Vim, using a special escape sequence. The JSON
433encodes a command that Vim understands. Example of such a message: >
434 <Esc>]51;["drop", "README.md"]<07>
435
436The body is always a list, making it easy to find the end: ]<07>.
437The <Esc>]51;msg<07> sequence is reserved by xterm for "Emacs shell", which is
438similar to what we are doing here.
439
440Currently supported commands:
441
442 call {funcname} {argument}
443
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200444 Call a user defined function with {argument}.
445 The function is called with two arguments: the buffer number
446 of the terminal and {argument}, the decoded JSON argument.
447 The function name must start with "Tapi_" to avoid
448 accidentally calling a function not meant to be used for the
449 terminal API
450 The user function should sanity check the argument.
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200451 The function can use |term_sendkeys()| to send back a reply.
452 Example in JSON: >
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200453 ["call", "Tapi_Impression", ["play", 14]]
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200454< Calls a function defined like this: >
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200455 function Tapi_Impression(bufnum, arglist)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200456 if len(a:arglist) == 2
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200457 echomsg "impression " . a:arglist[0]
458 echomsg "count " . a:arglist[1]
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200459 endif
460 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200461< Output from `:echo` may be erased by a redraw, use `:echomsg`
462 to be able to see it with `:messages`.
463
Bram Moolenaar333b80a2018-04-04 22:57:29 +0200464 drop {filename} [options]
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200465
466 Let Vim open a file, like the `:drop` command. If {filename}
467 is already open in a window, switch to that window. Otherwise
468 open a new window to edit {filename}.
Bram Moolenaar333b80a2018-04-04 22:57:29 +0200469
470 [options] is only used when opening a new window. If present,
471 it must be a Dict. Similarly to |++opt|, These entries are recognized:
472 "ff" file format: "dos", "mac" or "unix"
473 "fileformat" idem
474 "enc" overrides 'fileencoding'
475 "encoding" idem
476 "bin" sets 'binary'
477 "binary" idem
478 "nobin" resets 'binary'
479 "nobinary" idem
480 "bad" specifies behavior for bad characters, see
481 |++bad|
482
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200483 Example in JSON: >
484 ["drop", "path/file.txt", {"ff": "dos"}]
485
486A trick to have Vim send this escape sequence: >
487 exe "set t_ts=\<Esc>]51; t_fs=\x07"
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200488 let &titlestring = '["call","Tapi_TryThis",["hello",123]]'
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200489 redraw
490 set t_ts& t_fs&
491
492Rationale: Why not allow for any command or expression? Because that might
493create a security problem.
494
495
496Using the client-server feature ~
497 *terminal-client-server*
498This only works when v:servername is not empty. If needed you can set it,
499before opening the terminal, with: >
500 call remote_startserver('vim-server')
501
502$VIM_SERVERNAME is set in the terminal to pass on the server name.
503
504In the job you can then do something like: >
505 vim --servername $VIM_SERVERNAME --remote +123 some_file.c
506This will open the file "some_file.c" and put the cursor on line 123.
507
508==============================================================================
5093. Remote testing *terminal-testing*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200510
511Most Vim tests execute a script inside Vim. For some tests this does not
512work, running the test interferes with the code being tested. To avoid this
513Vim is executed in a terminal window. The test sends keystrokes to it and
514inspects the resulting screen state.
515
516Functions ~
517
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200518term_sendkeys() send keystrokes to a terminal (not subject to tmap)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200519term_wait() wait for screen to be updated
520term_scrape() inspect terminal screen
521
522
523==============================================================================
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02005244. Diffing screen dumps *terminal-diff*
Bram Moolenaarda650582018-02-20 15:51:40 +0100525
526In some cases it can be bothersome to test that Vim displays the right
527characters on the screen. E.g. with syntax highlighting. To make this
528simpler it is possible to take a screen dump of a terminal and compare it to
529an expected screen dump.
530
531Vim uses the window size, text, color and other attributes as displayed. The
532Vim screen size, font and other properties do not matter. Therefore this
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100533mechanism is portable across systems. A conventional screenshot would reflect
Bram Moolenaarda650582018-02-20 15:51:40 +0100534all differences, including font size and family.
535
536
537Writing a screen dump test for Vim ~
538 *terminal-dumptest*
539For an example see the Test_syntax_c() function in
540src/testdir/test_syntax.vim. The main parts are:
541- Write a file you want to test with. This is useful for testing syntax
542 highlighting. You can also start Vim with en empty buffer.
543- Run Vim in a terminal with a specific size. The default is 20 lines of 75
544 characters. This makes sure the dump is always this size. The function
545 RunVimInTerminal() takes care of this. Pass it the arguments for the Vim
546 command.
547- Send any commands to Vim using term_sendkeys(). For example: >
548 call term_sendkeys(buf, ":echo &lines &columns\<CR>")
549- Check that the screen is now in the expected state, using
550 VerifyScreenDump(). This expects the reference screen dump to be in the
551 src/testdir/dumps/ directory. Pass the name without ".dump". It is
552 recommended to use the name of the test function and a sequence number, so
553 that we know what test is using the file.
554- Repeat sending commands and checking the state.
555- Finally stop Vim by calling StopVimInTerminal().
556
557The first time you do this you won't have a screen dump yet. Create an empty
558file for now, e.g.: >
559 touch src/testdir/dumps/Test_function_name_01.dump
560
561The test will then fail, giving you the command to compare the reference dump
562and the failed dump, e.g.: >
563 call term_dumpdiff("Test_func.dump.failed", "dumps/Test_func.dump")
564
565Use this command in Vim, with the current directory set to src/testdir.
566Once you are satisfied with the test, move the failed dump in place of the
567reference: >
568 :!mv Test_func.dump.failed dumps/Test_func.dump
569
570
571Creating a screen dump ~
572 *terminal-screendump*
573
574To create the screen dump, run Vim (or any other program) in a terminal and
575make it show the desired state. Then use the term_dumpwrite() function to
576create a screen dump file. For example: >
577 :call term_dumpwrite(77, "mysyntax.dump")
578
579Here "77" is the buffer number of the terminal. Use `:ls!` to see it.
580
581You can view the screen dump with term_dumpload(): >
582 :call term_dumpload("mysyntax.dump")
583
584To verify that Vim still shows exactly the same screen, run Vim again with
585exactly the same way to show the desired state. Then create a screen dump
586again, using a different file name: >
587 :call term_dumpwrite(88, "test.dump")
588
589To assert that the files are exactly the same use assert_equalfile(): >
590 call assert_equalfile("mysyntax.dump", "test.dump")
591
592If there are differences then v:errors will contain the error message.
593
594
595Comparing screen dumps ~
596 *terminal-diffscreendump*
597
598assert_equalfile() does not make it easy to see what is different.
599To spot the problem use term_dumpdiff(): >
600 call term_dumpdiff("mysyntax.dump", "test.dump")
601
602This will open a window consisting of three parts:
6031. The contents of the first dump
6042. The difference between the first and second dump
6053. The contents of the second dump
606
607You can usually see what differs in the second part. Use the 'ruler' to
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100608relate it to the position in the first or second dump.
Bram Moolenaarda650582018-02-20 15:51:40 +0100609
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100610Alternatively, press "s" to swap the first and second dump. Do this several
Bram Moolenaarda650582018-02-20 15:51:40 +0100611times so that you can spot the difference in the context of the text.
612
613==============================================================================
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02006145. Debugging *terminal-debug*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200615
616The Terminal debugging plugin can be used to debug a program with gdb and view
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200617the source code in a Vim window. Since this is completely contained inside
618Vim this also works remotely over an ssh connection.
619
620
621Starting ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100622 *termdebug-starting*
Bram Moolenaarc572da52017-08-27 16:52:01 +0200623Load the plugin with this command: >
624 packadd termdebug
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200625< *:Termdebug*
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100626To start debugging use `:Termdebug` followed by the command name, for example: >
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200627 :Termdebug vim
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200628
Bram Moolenaarc572da52017-08-27 16:52:01 +0200629This opens two windows:
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100630
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200631gdb window A terminal window in which "gdb vim" is executed. Here you
632 can directly interact with gdb. The buffer name is "!gdb".
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100633
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200634program window A terminal window for the executed program. When "run" is
635 used in gdb the program I/O will happen in this window, so
636 that it does not interfere with controlling gdb. The buffer
637 name is "gdb program".
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200638
639The current window is used to show the source code. When gdb pauses the
640source file location will be displayed, if possible. A sign is used to
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100641highlight the current position, using highlight group debugPC.
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200642
643If the buffer in the current window is modified, another window will be opened
644to display the current gdb position.
645
646Focus the terminal of the executed program to interact with it. This works
647the same as any command running in a terminal window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200648
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200649When the debugger ends, typically by typing "quit" in the gdb window, the two
650opened windows are closed.
Bram Moolenaarc572da52017-08-27 16:52:01 +0200651
652
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200653Example session ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100654 *termdebug-example*
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200655Start in the Vim "src" directory and build Vim: >
656 % make
657Start Vim: >
658 % ./vim
659Load the termdebug plugin and start debugging Vim: >
660 :packadd termdebug
661 :Termdebug vim
662You should now have three windows:
663 source - where you started, has a window toolbar with buttons
664 gdb - you can type gdb commands here
665 program - the executed program will use this window
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100666
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200667You can use CTRL-W CTRL-W or the mouse to move focus between windows.
668Put focus on the gdb window and type: >
669 break ex_help
670 run
671Vim will start running in the program window. Put focus there and type: >
672 :help gui
673Gdb will run into the ex_help breakpoint. The source window now shows the
674ex_cmds.c file. A ">>" marker will appear where the breakpoint was set. The
675line where the debugger stopped is highlighted. You can now step through the
676program. Let's use the mouse: click on the "Next" button in the window
677toolbar. You will see the highlighting move as the debugger executes a line
678of source code.
679
680Click "Next" a few times until the for loop is highlighted. Put the cursor on
681the end of "eap->arg", then click "Eval" in the toolbar. You will see this
682displayed:
683 "eap->arg": 0x555555e68855 "gui" ~
684This way you can inspect the value of local variables. You can also focus the
685gdb window and use a "print" command, e.g.: >
686 print *eap
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100687If mouse pointer movements are working, Vim will also show a balloon when the
688mouse rests on text that can be evaluated by gdb.
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200689
690Now go back to the source window and put the cursor on the first line after
691the for loop, then type: >
692 :Break
693You will see a ">>" marker appear, this indicates the new breakpoint. Now
694click "Cont" in the toolbar and the code until the breakpoint will be
695executed.
696
697You can type more advanced commands in the gdb window. For example, type: >
698 watch curbuf
699Now click "Cont" in the toolbar (or type "cont" in the gdb window). Execution
700will now continue until the value of "curbuf" changes, which is in do_ecmd().
701To remove this watchpoint again type in the gdb window: >
702 delete 3
703
704You can see the stack by typing in the gdb window: >
705 where
706Move through the stack frames, e.g. with: >
707 frame 3
708The source window will show the code, at the point where the call was made to
709a deeper level.
710
711
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200712Stepping through code ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100713 *termdebug-stepping*
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200714Put focus on the gdb window to type commands there. Some common ones are:
Bram Moolenaar60e73f22017-11-12 18:02:06 +0100715- CTRL-C interrupt the program
716- next execute the current line and stop at the next line
717- step execute the current line and stop at the next statement,
718 entering functions
719- finish execute until leaving the current function
720- where show the stack
721- frame N go to the Nth stack frame
722- continue continue execution
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200723
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100724In the window showing the source code these commands can be used to control gdb:
725 `:Run` [args] run the program with [args] or the previous arguments
726 `:Arguments` {args} set arguments for the next `:Run`
Bram Moolenaar60e73f22017-11-12 18:02:06 +0100727
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100728 `:Break` set a breakpoint at the current line; a sign will be displayed
729 `:Clear` delete the breakpoint at the current line
Bram Moolenaar60e73f22017-11-12 18:02:06 +0100730
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100731 `:Step` execute the gdb "step" command
732 `:Over` execute the gdb "next" command (`:Next` is a Vim command)
733 `:Finish` execute the gdb "finish" command
734 `:Continue` execute the gdb "continue" command
735 `:Stop` interrupt the program
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200736
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100737If 'mouse' is set the plugin adds a window toolbar with these entries:
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100738 Step `:Step`
739 Next `:Over`
740 Finish `:Finish`
741 Cont `:Continue`
742 Stop `:Stop`
743 Eval `:Evaluate`
Bram Moolenaarf0b03c42017-12-17 17:17:07 +0100744This way you can use the mouse to perform the most common commands. You need
745to have the 'mouse' option set to enable mouse clicks.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200746
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100747You can add the window toolbar in other windows you open with: >
748 :Winbar
749
Bram Moolenaarc4b533e2018-04-06 22:26:25 +0200750If gdb stops at a source line and there is no window currently showing the
751source code, a new window will be created for the source code. This also
752happens if the buffer in the source code window has been modified and can't be
753abandoned.
754
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200755
756Inspecting variables ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100757 *termdebug-variables*
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100758 `:Evaluate` evaluate the expression under the cursor
759 `K` same
760 `:Evaluate` {expr} evaluate {expr}
761 `:'<,'>Evaluate` evaluate the Visually selected text
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200762
763This is similar to using "print" in the gdb window.
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100764You can usually shorten `:Evaluate` to `:Ev`.
Bram Moolenaar45d5f262017-09-10 19:14:31 +0200765
766
767Other commands ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100768 *termdebug-commands*
Bram Moolenaarc4b533e2018-04-06 22:26:25 +0200769 :Gdb jump to the gdb window
770 :Program jump to the window with the running program
771 :Source jump to the window with the source code, create it if there
772 isn't one
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200773
774
775Communication ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100776 *termdebug-communication*
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200777There is another, hidden, buffer, which is used for Vim to communicate with
778gdb. The buffer name is "gdb communication". Do not delete this buffer, it
779will break the debugger.
780
781
Bram Moolenaarc572da52017-08-27 16:52:01 +0200782Customizing ~
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100783
784GDB command *termdebug-customizing*
785
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200786To change the name of the gdb command, set the "termdebugger" variable before
787invoking `:Termdebug`: >
788 let termdebugger = "mygdb"
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100789< *gdb-version*
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200790Only debuggers fully compatible with gdb will work. Vim uses the GDB/MI
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100791interface. The "new-ui" command requires gdb version 7.12 or later. if you
792get this error:
Bram Moolenaar01164a62017-11-02 22:58:42 +0100793 Undefined command: "new-ui". Try "help".~
794Then your gdb is too old.
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200795
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100796
797Colors *hl-debugPC* *hl-debugBreakpoint*
798
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200799The color of the signs can be adjusted with these highlight groups:
800- debugPC the current position
801- debugBreakpoint a breakpoint
802
803The defaults are, when 'background' is "light":
804 hi debugPC term=reverse ctermbg=lightblue guibg=lightblue
805 hi debugBreakpoint term=reverse ctermbg=red guibg=red
806
807When 'background' is "dark":
808 hi debugPC term=reverse ctermbg=darkblue guibg=darkblue
809 hi debugBreakpoint term=reverse ctermbg=red guibg=red
Bram Moolenaarc572da52017-08-27 16:52:01 +0200810
Bram Moolenaar71137fe2018-03-03 20:47:21 +0100811
812Popup menu *termdebug_popup*
813
814By default the Termdebug plugin sets 'mousemodel' to "popup_setpos" and adds
815these entries to the popup menu:
816 Set breakpoint `:Break`
817 Clear breakpoint `:Clear`
818 Evaluate `:Evaluate`
819If you don't want this then disable it with: >
820 let g:termdebug_popup = 0
821
822
823Vim window width *termdebug_wide*
824
Bram Moolenaar38baa3e2017-09-14 16:10:38 +0200825To change the width of the Vim window when debugging starts, and use a
826vertical split: >
827 let g:termdebug_wide = 163
828This will set &columns to 163 when :Termdebug is used. The value is restored
829when quitting the debugger.
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200830If g:termdebug_wide is set and &Columns is already larger than
831g:termdebug_wide then a vertical split will be used without changing &columns.
832Set it to 1 to get a vertical split without every changing &columns (useful
833for when the terminal can't be resized by Vim).
Bram Moolenaar38baa3e2017-09-14 16:10:38 +0200834
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200835
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200836
837 vim:tw=78:ts=8:ft=help:norl: