blob: 18bf96acfdb894a3f2f246ccf840eb39ab242773 [file] [log] [blame]
Bram Moolenaar6bf2c622019-07-04 17:12:09 +02001*terminal.txt* For Vim version 8.1. Last change: 2019 Jul 04
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
Bram Moolenaarb1c91982018-05-17 17:04:55 +02007Terminal window support *terminal* *terminal-window*
Bram Moolenaare4f25e42017-07-07 11:54:15 +02008
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 Moolenaar6bf2c622019-07-04 17:12:09 +0200151. Basic use |terminal-use|
16 Typing |terminal-typing|
17 Size and color |terminal-size-color|
18 Command syntax |:terminal|
19 Resizing |terminal-resizing|
20 Terminal Modes |Terminal-mode|
21 Cursor style |terminal-cursor-style|
22 Session |terminal-session|
23 Special keys |terminal-special-keys|
24 Unix |terminal-unix|
25 MS-Windows |terminal-ms-windows|
262. Terminal functions |terminal-function-details|
273. Terminal communication |terminal-communication|
28 Vim to job: term_sendkeys() |terminal-to-job|
29 Job to Vim: JSON API |terminal-api|
30 Using the client-server feature |terminal-client-server|
314. Remote testing |terminal-testing|
325. Diffing screen dumps |terminal-diff|
33 Writing a screen dump test for Vim |terminal-dumptest|
34 Creating a screen dump |terminal-screendump|
35 Comparing screen dumps |terminal-diffscreendump|
366. Debugging |terminal-debug|
37 Starting |termdebug-starting|
38 Example session |termdebug-example|
39 Stepping through code |termdebug-stepping|
40 Inspecting variables |termdebug-variables|
41 Other commands |termdebug-commands|
42 Prompt mode |termdebug-prompt|
43 Communication |termdebug-communication|
44 Customizing |termdebug-customizing|
Bram Moolenaare4f25e42017-07-07 11:54:15 +020045
Bram Moolenaarc572da52017-08-27 16:52:01 +020046{only available when compiled with the |+terminal| feature}
Bram Moolenaar4c92e752019-02-17 21:18:32 +010047The terminal feature requires the |+job| and |+channel| features.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020048
49==============================================================================
501. Basic use *terminal-use*
51
52This feature is for running a terminal emulator in a Vim window. A job can be
53started connected to the terminal emulator. For example, to run a shell: >
54 :term bash
55
Bram Moolenaare09ba7b2017-09-09 22:19:47 +020056Or to run build command: >
57 :term make myprogram
Bram Moolenaare4f25e42017-07-07 11:54:15 +020058
59The job runs asynchronously from Vim, the window will be updated to show
Bram Moolenaare09ba7b2017-09-09 22:19:47 +020060output from the job, also while editing in another window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020061
Bram Moolenaar423802d2017-07-30 16:52:24 +020062
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +020063Typing ~
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +020064 *terminal-typing*
Bram Moolenaardd693ce2017-08-10 23:15:19 +020065When the keyboard focus is in the terminal window, typed keys will be sent to
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +020066the job. This uses a pty when possible. You can click outside of the
67terminal window to move keyboard focus elsewhere.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020068
Bram Moolenaar26967612019-03-17 17:13:16 +010069 *t_CTRL-W_CTRL-W* *t_CTRL-W_:*
Bram Moolenaar423802d2017-07-30 16:52:24 +020070CTRL-W can be used to navigate between windows and other CTRL-W commands, e.g.:
Bram Moolenaar60e73f22017-11-12 18:02:06 +010071 CTRL-W CTRL-W move focus to the next window
Bram Moolenaar423802d2017-07-30 16:52:24 +020072 CTRL-W : enter an Ex command
73See |CTRL-W| for more commands.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020074
Bram Moolenaar26967612019-03-17 17:13:16 +010075Special in the terminal window: *t_CTRL-W_.* *t_CTRL-W_N*
Bram Moolenaar423802d2017-07-30 16:52:24 +020076 CTRL-W . send a CTRL-W to the job in the terminal
Bram Moolenaarb59118d2018-04-13 22:11:56 +020077 CTRL-W CTRL-\ send a CTRL-\ to the job in the terminal
Bram Moolenaardd693ce2017-08-10 23:15:19 +020078 CTRL-W N go to Terminal-Normal mode, see |Terminal-mode|
79 CTRL-\ CTRL-N go to Terminal-Normal mode, see |Terminal-mode|
Bram Moolenaar26967612019-03-17 17:13:16 +010080 CTRL-W " {reg} paste register {reg} *t_CTRL-W_quote*
Bram Moolenaarf55e4c82017-08-01 20:44:53 +020081 Also works with the = register to insert the result of
82 evaluating an expression.
Bram Moolenaar8e539c52017-08-18 22:57:06 +020083 CTRL-W CTRL-C ends the job, see below |t_CTRL-W_CTRL-C|
Bram Moolenaar26967612019-03-17 17:13:16 +010084 CTRL-W gt go to next tabpage, same as `gt` *t_CTRL-W_gt*
85 CTRL-W gT go to previous tabpage, same as `gT` *t_CTRL-W_gT*
Bram Moolenaar423802d2017-07-30 16:52:24 +020086
Bram Moolenaar7dda86f2018-04-20 22:36:41 +020087See option 'termwinkey' for specifying another key instead of CTRL-W that
88will work like CTRL-W. However, typing 'termwinkey' twice sends 'termwinkey'
89to the job. For example:
90 'termwinkey' CTRL-W move focus to the next window
91 'termwinkey' : enter an Ex command
92 'termwinkey' 'termwinkey' send 'termwinkey' to the job in the terminal
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +020093 'termwinkey' . send 'termwinkey' to the job in the terminal
94 'termwinkey' CTRL-\ send a CTRL-\ to the job in the terminal
Bram Moolenaar7dda86f2018-04-20 22:36:41 +020095 'termwinkey' N go to terminal Normal mode, see below
Bram Moolenaar26967612019-03-17 17:13:16 +010096 'termwinkey' CTRL-N same as CTRL-W N |t_CTRL-W_N|
97 'termwinkey' CTRL-C same as CTRL-W CTRL-C |t_CTRL-W_CTRL-C|
Bram Moolenaar69198192017-08-05 14:10:48 +020098 *t_CTRL-\_CTRL-N*
Bram Moolenaardd693ce2017-08-10 23:15:19 +020099The special key combination CTRL-\ CTRL-N can be used to switch to Normal
100mode, just like this works in any other mode.
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200101 *t_CTRL-W_CTRL-C*
102CTRL-W CTRL-C can be typed to forcefully end the job. On MS-Windows a
103CTRL-BREAK will also kill the job.
104
105If you type CTRL-C the effect depends on what the pty has been configured to
106do. For simple commands this causes a SIGINT to be sent to the job, which
107would end it. Other commands may ignore the SIGINT or handle the CTRL-C
108themselves (like Vim does).
Bram Moolenaar423802d2017-07-30 16:52:24 +0200109
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200110To change the keys you type use terminal mode mappings, see |:tmap|.
111These are defined like any mapping, but apply only when typing keys that are
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100112sent to the job running in the terminal. For example, to make F1 switch
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200113to Terminal-Normal mode: >
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100114 tnoremap <F1> <C-W>N
115You can use Esc, but you need to make sure it won't cause other keys to
Bram Moolenaard47d5222018-12-09 20:43:55 +0100116break (cursor keys start with an Esc, so they may break): >
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200117 tnoremap <Esc> <C-W>N
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100118 set notimeout ttimeout timeoutlen=100
119
Bram Moolenaar4c5d8152018-10-19 22:36:53 +0200120You can also create menus similar to terminal mode mappings, but you have to
121use |:tlmenu| instead of |:tmenu|.
122
Bram Moolenaar01164a62017-11-02 22:58:42 +0100123< *options-in-terminal*
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200124After opening the terminal window and setting 'buftype' to "terminal" the
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +0200125TerminalOpen autocommand event is triggered. This makes it possible to set
Bram Moolenaar24a98a02017-09-27 22:23:55 +0200126options specifically for the window and buffer. Example: >
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +0200127 au TerminalOpen * if &buftype == 'terminal' | setlocal bufhidden=hide | endif
128The <abuf> is set to the terminal buffer, but if there is no window (hidden
129terminal) then setting options will happen in the wrong buffer, therefore the
130check for &buftype in the example.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200131
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100132Mouse events (click and drag) are passed to the terminal. Mouse move events
133are only passed when Vim itself is receiving them. For a terminal that is
134when 'balloonevalterm' is enabled.
135
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200136
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200137Size and color ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100138 *terminal-size-color*
Bram Moolenaar7dda86f2018-04-20 22:36:41 +0200139See option 'termwinsize' for controlling the size of the terminal window.
Bram Moolenaar74675a62017-07-15 13:53:23 +0200140(TODO: scrolling when the terminal is larger than the window)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200141
Bram Moolenaar38baa3e2017-09-14 16:10:38 +0200142The job running in the terminal can change the colors. The default foreground
143and background colors are taken from Vim, the Normal highlight group.
144
145For a color terminal the 'background' option is used to decide whether the
146terminal window will start with a white or black background.
147
Bram Moolenaardf980db2017-12-24 13:22:00 +0100148To use a different color the Terminal highlight group can be used, for
149example: >
Bram Moolenaar38baa3e2017-09-14 16:10:38 +0200150 hi Terminal ctermbg=lightgrey ctermfg=blue guibg=lightgrey guifg=blue
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200151<
152 *g:terminal_ansi_colors*
Bram Moolenaar7dda86f2018-04-20 22:36:41 +0200153In GUI mode or with 'termguicolors', the 16 ANSI colors used by default in new
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200154terminal windows may be configured using the variable
155`g:terminal_ansi_colors`, which should be a list of 16 color names or
156hexadecimal color codes, similar to those accepted by |highlight-guifg|. When
157not using GUI colors, the terminal window always uses the 16 ANSI colors of
158the underlying terminal.
159The |term_setansicolors()| function can be used to change the colors, and
160|term_getansicolors()| to get the currently used colors.
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200161
Bram Moolenaar423802d2017-07-30 16:52:24 +0200162
Bram Moolenaar6bf2c622019-07-04 17:12:09 +0200163Command syntax ~
Bram Moolenaar8a773062017-07-24 22:29:21 +0200164
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200165:[range]ter[minal] [options] [command] *:ter* *:terminal*
Bram Moolenaar8a773062017-07-24 22:29:21 +0200166 Open a new terminal window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200167
168 If [command] is provided run it as a job and connect
169 the input and output to the terminal.
170 If [command] is not given the 'shell' option is used.
Bram Moolenaarc572da52017-08-27 16:52:01 +0200171 if [command] is NONE no job is started, the pty of the
172 terminal can be used by a command like gdb.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200173
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100174 If [command] is missing the default behavior is to
175 close the terminal when the shell exits. This can be
176 changed with the ++noclose argument.
177 If [command] is present the default behavior is to
178 keep the terminal open in Terminal-Normal mode. This
179 can be changed with the ++close argument.
180
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200181 A new buffer will be created, using [command] or
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200182 'shell' as the name, prefixed with a "!". If a buffer
183 by this name already exists a number is added in
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200184 parentheses. E.g. if "gdb" exists the second terminal
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200185 buffer will use "!gdb (1)".
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200186
Bram Moolenaarb2412082017-08-20 18:09:14 +0200187 If [range] is given the specified lines are used as
188 input for the job. It will not be possible to type
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200189 keys in the terminal window. For MS-Windows see the
190 ++eof argument below.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200191
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200192 *term++close* *term++open*
193 Supported [options] are:
194 ++close The terminal window will close
195 automatically when the job terminates.
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100196 ++noclose The terminal window will NOT close
197 automatically when the job terminates.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200198 ++open When the job terminates and no window
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200199 shows it, a window will be opened.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200200 Note that this can be interruptive.
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100201 The last of ++close, ++noclose and ++open
202 matters and rules out earlier arguments.
203
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200204 ++curwin Open the terminal in the current
205 window, do not split the current
206 window. Fails if the current buffer
207 cannot be |abandon|ed.
208 ++hidden Open the terminal in a hidden buffer,
209 no window will be used.
Bram Moolenaarb5b75622018-03-09 22:22:21 +0100210 ++norestore Do not include this terminal window
211 in a session file.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100212 ++kill={how} When trying to close the terminal
213 window kill the job with {how}. See
214 |term_setkill()| for the values.
Bram Moolenaarb2412082017-08-20 18:09:14 +0200215 ++rows={height} Use {height} for the terminal window
Bram Moolenaar40962ec2018-01-28 22:47:25 +0100216 height. If the terminal uses the full
217 Vim height (no window above or below
Bram Moolenaar0b0f0992018-05-22 21:41:30 +0200218 the terminal window) the command line
Bram Moolenaar40962ec2018-01-28 22:47:25 +0100219 height will be reduced as needed.
Bram Moolenaarb2412082017-08-20 18:09:14 +0200220 ++cols={width} Use {width} for the terminal window
Bram Moolenaar40962ec2018-01-28 22:47:25 +0100221 width. If the terminal uses the full
222 Vim width (no window left or right of
223 the terminal window) this value is
224 ignored.
Bram Moolenaare09ba7b2017-09-09 22:19:47 +0200225 ++eof={text} when using [range]: text to send after
226 the last line was written. Cannot
227 contain white space. A CR is
228 appended. For MS-Windows the default
229 is to send CTRL-D.
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200230 E.g. for a shell use "++eof=exit" and
231 for Python "++eof=exit()". Special
232 codes can be used like with `:map`,
233 e.g. "<C-Z>" for CTRL-Z.
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100234 ++type={pty} (MS-Windows only): Use {pty} as the
235 virtual console. See 'termwintype'
236 for the values.
237
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200238 If you want to use more options use the |term_start()|
239 function.
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200240 If you want to split the window vertically, use: >
241 :vertical terminal
242< Or short: >
243 :vert ter
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200244
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100245When the buffer associated with the terminal is forcibly unloaded or wiped out
246the job is killed, similar to calling `job_stop(job, "kill")` .
247Closing the window normally results in |E947|. When a kill method was set
248with "++kill={how}" or |term_setkill()| then closing the window will use that
249way to kill or interrupt the job. For example: >
250 :term ++kill=term tail -f /tmp/log
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200251
Bram Moolenaare561a7e2017-08-29 22:44:59 +0200252So long as the job is running the window behaves like it contains a modified
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200253buffer. Trying to close the window with `CTRL-W :quit` fails. When using
254`CTRL-W :quit!` the job is ended. The text in the window is lost. The buffer
255still exists, but getting it in a window with `:buffer` will show an empty
256buffer.
257
258Trying to close the window with `CTRL-W :close` also fails. Using
259`CTRL-W :close!` will close the window and make the buffer hidden.
Bram Moolenaare561a7e2017-08-29 22:44:59 +0200260
261You can use `CTRL-W :hide` to close the terminal window and make the buffer
262hidden, the job keeps running. The `:buffer` command can be used to turn the
263current window into a terminal window. If there are unsaved changes this
Bram Moolenaar0b0f0992018-05-22 21:41:30 +0200264fails, use ! to force, as usual.
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200265
266To have a background job run without a window, and open the window when it's
267done, use options like this: >
268 :term ++hidden ++open make
269Note that the window will open at an unexpected moment, this will interrupt
270what you are doing.
271
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200272 *E947* *E948*
Bram Moolenaar78712a72017-08-05 14:50:12 +0200273So long as the job is running, the buffer is considered modified and Vim
274cannot be quit easily, see |abandon|.
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200275
276When the job has finished and no changes were made to the buffer: closing the
277window will wipe out the buffer.
278
279Before changes can be made to a terminal buffer, the 'modifiable' option must
280be set. This is only possible when the job has finished. At the first change
281the buffer will become a normal buffer and the highlighting is removed.
282You may want to change the buffer name with |:file| to be able to write, since
283the buffer name will still be set to the command.
284
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200285
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200286Resizing ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100287 *terminal-resizing*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200288The size of the terminal can be in one of three modes:
289
Bram Moolenaar7dda86f2018-04-20 22:36:41 +02002901. The 'termwinsize' option is empty: The terminal size follows the window
291 size. The minimal size is 2 screen lines with 10 cells.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200292
Bram Moolenaar7dda86f2018-04-20 22:36:41 +02002932. The 'termwinsize' option is "rows*cols", where "rows" is the minimal number
294 of screen rows and "cols" is the minimal number of cells.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200295
Bram Moolenaar7dda86f2018-04-20 22:36:41 +02002963. The 'termwinsize' option is "rowsXcols" (where the x is upper or lower
297 case). The terminal size is fixed to the specified number of screen lines
298 and cells. If the window is bigger there will be unused empty space.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200299
300If the window is smaller than the terminal size, only part of the terminal can
301be seen (the lower-left part).
302
303The |term_getsize()| function can be used to get the current size of the
304terminal. |term_setsize()| can be used only when in the first or second mode,
Bram Moolenaar7dda86f2018-04-20 22:36:41 +0200305not when 'termwinsize' is "rowsXcols".
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200306
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200307
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200308Terminal-Job and Terminal-Normal mode ~
Bram Moolenaar8c041b62018-04-14 18:14:06 +0200309 *Terminal-mode* *Terminal-Job*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200310When the job is running the contents of the terminal is under control of the
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200311job. That includes the cursor position. Typed keys are sent to the job.
312The terminal contents can change at any time. This is called Terminal-Job
313mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200314
Bram Moolenaar7dda86f2018-04-20 22:36:41 +0200315Use CTRL-W N (or 'termwinkey' N) to switch to Terminal-Normal mode. Now the
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200316contents of the terminal window is under control of Vim, the job output is
317suspended. CTRL-\ CTRL-N does the same.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200318
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200319Terminal-Job mode is where |:tmap| mappings are applied. Keys sent by
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200320|term_sendkeys()| are not subject to tmap, but keys from |feedkeys()| are.
321
Bram Moolenaar8c041b62018-04-14 18:14:06 +0200322It is not possible to enter Insert mode from Terminal-Job mode.
323
324 *Terminal-Normal* *E946*
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200325In Terminal-Normal mode you can move the cursor around with the usual Vim
326commands, Visually mark text, yank text, etc. But you cannot change the
327contents of the buffer. The commands that would start insert mode, such as
328'i' and 'a', return to Terminal-Job mode. The window will be updated to show
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200329the contents of the terminal. |:startinsert| is ineffective.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200330
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200331In Terminal-Normal mode the statusline and window title show "(Terminal)". If
332the job ends while in Terminal-Normal mode this changes to
333"(Terminal-finished)".
Bram Moolenaar423802d2017-07-30 16:52:24 +0200334
Bram Moolenaar8c041b62018-04-14 18:14:06 +0200335When the job outputs lines in the terminal, such that the contents scrolls off
336the top, those lines are remembered and can be seen in Terminal-Normal mode.
Bram Moolenaar7dda86f2018-04-20 22:36:41 +0200337The number of lines is limited by the 'termwinscroll' option. When going over
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200338this limit, the first 10% of the scrolled lines are deleted and are lost.
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200339
Bram Moolenaar423802d2017-07-30 16:52:24 +0200340
Bram Moolenaarc572da52017-08-27 16:52:01 +0200341Cursor style ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100342 *terminal-cursor-style*
Bram Moolenaarc572da52017-08-27 16:52:01 +0200343By default the cursor in the terminal window uses a not blinking block. The
344normal xterm escape sequences can be used to change the blinking state and the
345shape. Once focus leaves the terminal window Vim will restore the original
346cursor.
347
348An exception is when xterm is started with the "-bc" argument, or another way
349that causes the cursor to blink. This actually means that the blinking flag
350is inverted. Since Vim cannot detect this, the terminal window cursor
351blinking will also be inverted.
352
353
Bram Moolenaarb5b75622018-03-09 22:22:21 +0100354Session ~
355 *terminal-session*
356A terminal window will be restored when using a session file, if possible and
357wanted.
358
359If "terminal" was removed from 'sessionoptions' then no terminal windows will
360be restored.
361
362If the job in the terminal was finished the window will not be restored.
363
364If the terminal can be restored, the command that was used to open it will be
365used again. To change this use the |term_setrestore()| function. This can
366also be used to not restore a specific terminal by setting the command to
367"NONE".
368
369
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100370Special keys ~
371 *terminal-special-keys*
372Since the terminal emulator simulates an xterm, only escape sequences that
373both Vim and xterm recognize will be available in the terminal window. If you
374want to pass on other escape sequences to the job running in the terminal you
375need to set up forwarding. Example: >
376 tmap <expr> <Esc>]b SendToTerm("\<Esc>]b")
Bram Moolenaar60e73f22017-11-12 18:02:06 +0100377 func SendToTerm(what)
378 call term_sendkeys('', a:what)
379 return ''
380 endfunc
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200381
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100382
383Unix ~
384 *terminal-unix*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200385On Unix a pty is used to make it possible to run all kinds of commands. You
386can even run Vim in the terminal! That's used for debugging, see below.
387
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200388Environment variables are used to pass information to the running job:
Bram Moolenaar9a993e32018-04-05 22:15:22 +0200389 TERM the name of the terminal, from the 'term' option or
390 $TERM in the GUI; falls back to "xterm" if it does not
391 start with "xterm"
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200392 ROWS number of rows in the terminal initially
393 LINES same as ROWS
394 COLUMNS number of columns in the terminal initially
395 COLORS number of colors, 't_Co' (256*256*256 in the GUI)
396 VIM_SERVERNAME v:servername
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200397 VIM_TERMINAL v:version
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200398
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200399
400MS-Windows ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100401 *terminal-ms-windows*
Bram Moolenaar8a773062017-07-24 22:29:21 +0200402On MS-Windows winpty is used to make it possible to run all kind of commands.
403Obviously, they must be commands that run in a terminal, not open their own
404window.
405
406You need the following two files from winpty:
407
408 winpty.dll
409 winpty-agent.exe
410
411You can download them from the following page:
412
413 https://github.com/rprichard/winpty
414
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200415Just put the files somewhere in your PATH. You can set the 'winptydll' option
416to point to the right file, if needed. If you have both the 32-bit and 64-bit
417version, rename to winpty32.dll and winpty64.dll to match the way Vim was
418build.
Bram Moolenaar5acd9872019-02-16 13:35:13 +0100419 *ConPTY* *E982*
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100420On more recent versions of MS-Windows 10 (beginning with the "October 2018
421Update"), winpty is no longer required. On those versions, |:terminal| will use
422Windows' built-in support for hosting terminal applications, "ConPTY". When
423ConPTY is in use, there may be rendering artifacts regarding ambiguous-width
Bram Moolenaar5acd9872019-02-16 13:35:13 +0100424characters. If you encounter any such issues, install "winpty". Until the
425ConPTY problems have been fixed "winpty" will be preferred.
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200426
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100427Environment variables are used to pass information to the running job:
428 VIM_SERVERNAME v:servername
429
Bram Moolenaar6bf2c622019-07-04 17:12:09 +0200430
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200431==============================================================================
Bram Moolenaar6bf2c622019-07-04 17:12:09 +02004322. Terminal functions *terminal-function-details*
433
434 *term_dumpdiff()*
435term_dumpdiff({filename}, {filename} [, {options}])
436 Open a new window displaying the difference between the two
437 files. The files must have been created with
438 |term_dumpwrite()|.
439 Returns the buffer number or zero when the diff fails.
440 Also see |terminal-diff|.
441 NOTE: this does not work with double-width characters yet.
442
443 The top part of the buffer contains the contents of the first
444 file, the bottom part of the buffer contains the contents of
445 the second file. The middle part shows the differences.
446 The parts are separated by a line of equals.
447
448 If the {options} argument is present, it must be a Dict with
449 these possible members:
450 "term_name" name to use for the buffer name, instead
451 of the first file name.
452 "term_rows" vertical size to use for the terminal,
453 instead of using 'termwinsize'
454 "term_cols" horizontal size to use for the terminal,
455 instead of using 'termwinsize'
456 "vertical" split the window vertically
457 "curwin" use the current window, do not split the
458 window; fails if the current buffer
459 cannot be |abandon|ed
460 "bufnr" do not create a new buffer, use the
461 existing buffer "bufnr". This buffer
462 must have been previously created with
463 term_dumpdiff() or term_dumpload() and
464 visible in a window.
465 "norestore" do not add the terminal window to a
466 session file
467
468 Each character in the middle part indicates a difference. If
469 there are multiple differences only the first in this list is
470 used:
471 X different character
472 w different width
473 f different foreground color
474 b different background color
475 a different attribute
476 + missing position in first file
477 - missing position in second file
478
479 Using the "s" key the top and bottom parts are swapped. This
480 makes it easy to spot a difference.
481
482 *term_dumpload()*
483term_dumpload({filename} [, {options}])
484 Open a new window displaying the contents of {filename}
485 The file must have been created with |term_dumpwrite()|.
486 Returns the buffer number or zero when it fails.
487 Also see |terminal-diff|.
488
489 For {options} see |term_dumpdiff()|.
490
491 *term_dumpwrite()*
492term_dumpwrite({buf}, {filename} [, {options}])
493 Dump the contents of the terminal screen of {buf} in the file
494 {filename}. This uses a format that can be used with
495 |term_dumpload()| and |term_dumpdiff()|.
496 If the job in the terminal already finished an error is given:
497 *E958*
498 If {filename} already exists an error is given: *E953*
499 Also see |terminal-diff|.
500
501 {options} is a dictionary with these optional entries:
502 "rows" maximum number of rows to dump
503 "columns" maximum number of columns to dump
504
505term_getaltscreen({buf}) *term_getaltscreen()*
506 Returns 1 if the terminal of {buf} is using the alternate
507 screen.
508 {buf} is used as with |term_getsize()|.
509 {only available when compiled with the |+terminal| feature}
510
511term_getansicolors({buf}) *term_getansicolors()*
512 Get the ANSI color palette in use by terminal {buf}.
513 Returns a List of length 16 where each element is a String
514 representing a color in hexadecimal "#rrggbb" format.
515 Also see |term_setansicolors()| and |g:terminal_ansi_colors|.
516 If neither was used returns the default colors.
517
518 {buf} is used as with |term_getsize()|. If the buffer does not
519 exist or is not a terminal window, an empty list is returned.
520 {only available when compiled with the |+terminal| feature and
521 with GUI enabled and/or the |+termguicolors| feature}
522
523term_getattr({attr}, {what}) *term_getattr()*
524 Given {attr}, a value returned by term_scrape() in the "attr"
525 item, return whether {what} is on. {what} can be one of:
526 bold
527 italic
528 underline
529 strike
530 reverse
531 {only available when compiled with the |+terminal| feature}
532
533term_getcursor({buf}) *term_getcursor()*
534 Get the cursor position of terminal {buf}. Returns a list with
535 two numbers and a dictionary: [row, col, dict].
536
537 "row" and "col" are one based, the first screen cell is row
538 1, column 1. This is the cursor position of the terminal
539 itself, not of the Vim window.
540
541 "dict" can have these members:
542 "visible" one when the cursor is visible, zero when it
543 is hidden.
544 "blink" one when the cursor is blinking, zero when it
545 is not blinking.
546 "shape" 1 for a block cursor, 2 for underline and 3
547 for a vertical bar.
548 "color" color of the cursor, e.g. "green"
549
550 {buf} must be the buffer number of a terminal window. If the
551 buffer does not exist or is not a terminal window, an empty
552 list is returned.
553 {only available when compiled with the |+terminal| feature}
554
555term_getjob({buf}) *term_getjob()*
556 Get the Job associated with terminal window {buf}.
557 {buf} is used as with |term_getsize()|.
558 Returns |v:null| when there is no job.
559 {only available when compiled with the |+terminal| feature}
560
561term_getline({buf}, {row}) *term_getline()*
562 Get a line of text from the terminal window of {buf}.
563 {buf} is used as with |term_getsize()|.
564
565 The first line has {row} one. When {row} is "." the cursor
566 line is used. When {row} is invalid an empty string is
567 returned.
568
569 To get attributes of each character use |term_scrape()|.
570 {only available when compiled with the |+terminal| feature}
571
572term_getscrolled({buf}) *term_getscrolled()*
573 Return the number of lines that scrolled to above the top of
574 terminal {buf}. This is the offset between the row number
575 used for |term_getline()| and |getline()|, so that: >
576 term_getline(buf, N)
577< is equal to: >
578 getline(N + term_getscrolled(buf))
579< (if that line exists).
580
581 {buf} is used as with |term_getsize()|.
582 {only available when compiled with the |+terminal| feature}
583
584term_getsize({buf}) *term_getsize()*
585 Get the size of terminal {buf}. Returns a list with two
586 numbers: [rows, cols]. This is the size of the terminal, not
587 the window containing the terminal.
588
589 {buf} must be the buffer number of a terminal window. Use an
590 empty string for the current buffer. If the buffer does not
591 exist or is not a terminal window, an empty list is returned.
592 {only available when compiled with the |+terminal| feature}
593
594term_getstatus({buf}) *term_getstatus()*
595 Get the status of terminal {buf}. This returns a comma
596 separated list of these items:
597 running job is running
598 finished job has finished
599 normal in Terminal-Normal mode
600 One of "running" or "finished" is always present.
601
602 {buf} must be the buffer number of a terminal window. If the
603 buffer does not exist or is not a terminal window, an empty
604 string is returned.
605 {only available when compiled with the |+terminal| feature}
606
607term_gettitle({buf}) *term_gettitle()*
608 Get the title of terminal {buf}. This is the title that the
609 job in the terminal has set.
610
611 {buf} must be the buffer number of a terminal window. If the
612 buffer does not exist or is not a terminal window, an empty
613 string is returned.
614 {only available when compiled with the |+terminal| feature}
615
616term_gettty({buf} [, {input}]) *term_gettty()*
617 Get the name of the controlling terminal associated with
618 terminal window {buf}. {buf} is used as with |term_getsize()|.
619
620 When {input} is omitted or 0, return the name for writing
621 (stdout). When {input} is 1 return the name for reading
622 (stdin). On UNIX, both return same name.
623 {only available when compiled with the |+terminal| feature}
624
625term_list() *term_list()*
626 Return a list with the buffer numbers of all buffers for
627 terminal windows.
628 {only available when compiled with the |+terminal| feature}
629
630term_scrape({buf}, {row}) *term_scrape()*
631 Get the contents of {row} of terminal screen of {buf}.
632 For {buf} see |term_getsize()|.
633
634 The first line has {row} one. When {row} is "." the cursor
635 line is used. When {row} is invalid an empty string is
636 returned.
637
638 Return a List containing a Dict for each screen cell:
639 "chars" character(s) at the cell
640 "fg" foreground color as #rrggbb
641 "bg" background color as #rrggbb
642 "attr" attributes of the cell, use |term_getattr()|
643 to get the individual flags
644 "width" cell width: 1 or 2
645 {only available when compiled with the |+terminal| feature}
646
647term_sendkeys({buf}, {keys}) *term_sendkeys()*
648 Send keystrokes {keys} to terminal {buf}.
649 {buf} is used as with |term_getsize()|.
650
651 {keys} are translated as key sequences. For example, "\<c-x>"
652 means the character CTRL-X.
653 {only available when compiled with the |+terminal| feature}
654
655term_setansicolors({buf}, {colors}) *term_setansicolors()*
656 Set the ANSI color palette used by terminal {buf}.
657 {colors} must be a List of 16 valid color names or hexadecimal
658 color codes, like those accepted by |highlight-guifg|.
659 Also see |term_getansicolors()| and |g:terminal_ansi_colors|.
660
661 The colors normally are:
662 0 black
663 1 dark red
664 2 dark green
665 3 brown
666 4 dark blue
667 5 dark magenta
668 6 dark cyan
669 7 light grey
670 8 dark grey
671 9 red
672 10 green
673 11 yellow
674 12 blue
675 13 magenta
676 14 cyan
677 15 white
678
679 These colors are used in the GUI and in the terminal when
680 'termguicolors' is set. When not using GUI colors (GUI mode
681 or 'termguicolors'), the terminal window always uses the 16
682 ANSI colors of the underlying terminal.
683 {only available when compiled with the |+terminal| feature and
684 with GUI enabled and/or the |+termguicolors| feature}
685
686term_setkill({buf}, {how}) *term_setkill()*
687 When exiting Vim or trying to close the terminal window in
688 another way, {how} defines whether the job in the terminal can
689 be stopped.
690 When {how} is empty (the default), the job will not be
691 stopped, trying to exit will result in |E947|.
692 Otherwise, {how} specifies what signal to send to the job.
693 See |job_stop()| for the values.
694
695 After sending the signal Vim will wait for up to a second to
696 check that the job actually stopped.
697
698term_setrestore({buf}, {command}) *term_setrestore()*
699 Set the command to write in a session file to restore the job
700 in this terminal. The line written in the session file is: >
701 terminal ++curwin ++cols=%d ++rows=%d {command}
702< Make sure to escape the command properly.
703
704 Use an empty {command} to run 'shell'.
705 Use "NONE" to not restore this window.
706 {only available when compiled with the |+terminal| feature}
707
708term_setsize({buf}, {rows}, {cols}) *term_setsize()* *E955*
709 Set the size of terminal {buf}. The size of the window
710 containing the terminal will also be adjusted, if possible.
711 If {rows} or {cols} is zero or negative, that dimension is not
712 changed.
713
714 {buf} must be the buffer number of a terminal window. Use an
715 empty string for the current buffer. If the buffer does not
716 exist or is not a terminal window, an error is given.
717 {only available when compiled with the |+terminal| feature}
718
719term_start({cmd} [, {options}]) *term_start()*
720 Open a terminal window and run {cmd} in it.
721
722 {cmd} can be a string or a List, like with |job_start()|. The
723 string "NONE" can be used to open a terminal window without
724 starting a job, the pty of the terminal can be used by a
725 command like gdb.
726
727 Returns the buffer number of the terminal window. If {cmd}
728 cannot be executed the window does open and shows an error
729 message.
730 If opening the window fails zero is returned.
731
732 {options} are similar to what is used for |job_start()|, see
733 |job-options|. However, not all options can be used. These
734 are supported:
735 all timeout options
736 "stoponexit", "cwd", "env"
737 "callback", "out_cb", "err_cb", "exit_cb", "close_cb"
738 "in_io", "in_top", "in_bot", "in_name", "in_buf"
739 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
740 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
741 However, at least one of stdin, stdout or stderr must be
742 connected to the terminal. When I/O is connected to the
743 terminal then the callback function for that part is not used.
744
745 There are extra options:
746 "term_name" name to use for the buffer name, instead
747 of the command name.
748 "term_rows" vertical size to use for the terminal,
749 instead of using 'termwinsize'
750 "term_cols" horizontal size to use for the terminal,
751 instead of using 'termwinsize'
752 "vertical" split the window vertically; note that
753 other window position can be defined with
754 command modifiers, such as |:belowright|.
755 "curwin" use the current window, do not split the
756 window; fails if the current buffer
757 cannot be |abandon|ed
758 "hidden" do not open a window
759 "norestore" do not add the terminal window to a
760 session file
761 "term_kill" what to do when trying to close the
762 terminal window, see |term_setkill()|
763 "term_finish" What to do when the job is finished:
764 "close": close any windows
765 "open": open window if needed
766 Note that "open" can be interruptive.
767 See |term++close| and |term++open|.
768 "term_opencmd" command to use for opening the window when
769 "open" is used for "term_finish"; must
770 have "%d" where the buffer number goes,
771 e.g. "10split|buffer %d"; when not
772 specified "botright sbuf %d" is used
773 "eof_chars" Text to send after all buffer lines were
774 written to the terminal. When not set
775 CTRL-D is used on MS-Windows. For Python
776 use CTRL-Z or "exit()". For a shell use
777 "exit". A CR is always added.
778 "ansi_colors" A list of 16 color names or hex codes
779 defining the ANSI palette used in GUI
780 color modes. See |g:terminal_ansi_colors|.
781 "tty_type" (MS-Windows only): Specify which pty to
782 use. See 'termwintype' for the values.
783
784 {only available when compiled with the |+terminal| feature}
785
786term_wait({buf} [, {time}]) *term_wait()*
787 Wait for pending updates of {buf} to be handled.
788 {buf} is used as with |term_getsize()|.
789 {time} is how long to wait for updates to arrive in msec. If
790 not set then 10 msec will be used.
791 {only available when compiled with the |+terminal| feature}
792
793==============================================================================
7943. Terminal communication *terminal-communication*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200795
796There are several ways to communicate with the job running in a terminal:
797- Use |term_sendkeys()| to send text and escape sequences from Vim to the job.
798- Use the JSON API to send encoded commands from the job to Vim.
799- Use the |client-server| mechanism. This works on machines with an X server
800 and on MS-Windows.
801
802
803Vim to job: term_sendkeys() ~
804 *terminal-to-job*
805This allows for remote controlling the job running in the terminal. It is a
806one-way mechanism. The job can update the display to signal back to Vim.
807For example, if a shell is running in a terminal, you can do: >
808 call term_sendkeys(buf, "ls *.java\<CR>")
809
810This requires for the job to be in the right state where it will do the right
811thing when receiving the keys. For the above example, the shell must be
812waiting for a command to be typed.
813
814For a job that was written for the purpose, you can use the JSON API escape
815sequence in the other direction. E.g.: >
816 call term_sendkeys(buf, "\<Esc>]51;["response"]\x07")
817
818
819Job to Vim: JSON API ~
820 *terminal-api*
821The job can send JSON to Vim, using a special escape sequence. The JSON
822encodes a command that Vim understands. Example of such a message: >
823 <Esc>]51;["drop", "README.md"]<07>
824
825The body is always a list, making it easy to find the end: ]<07>.
826The <Esc>]51;msg<07> sequence is reserved by xterm for "Emacs shell", which is
827similar to what we are doing here.
828
829Currently supported commands:
830
831 call {funcname} {argument}
832
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200833 Call a user defined function with {argument}.
834 The function is called with two arguments: the buffer number
835 of the terminal and {argument}, the decoded JSON argument.
836 The function name must start with "Tapi_" to avoid
837 accidentally calling a function not meant to be used for the
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +0200838 terminal API.
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200839 The user function should sanity check the argument.
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200840 The function can use |term_sendkeys()| to send back a reply.
841 Example in JSON: >
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200842 ["call", "Tapi_Impression", ["play", 14]]
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200843< Calls a function defined like this: >
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200844 function Tapi_Impression(bufnum, arglist)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200845 if len(a:arglist) == 2
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200846 echomsg "impression " . a:arglist[0]
847 echomsg "count " . a:arglist[1]
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200848 endif
849 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200850< Output from `:echo` may be erased by a redraw, use `:echomsg`
851 to be able to see it with `:messages`.
852
Bram Moolenaar333b80a2018-04-04 22:57:29 +0200853 drop {filename} [options]
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200854
855 Let Vim open a file, like the `:drop` command. If {filename}
856 is already open in a window, switch to that window. Otherwise
857 open a new window to edit {filename}.
Bram Moolenaar85eee132018-05-06 17:57:30 +0200858 Note that both the job and Vim may change the current
859 directory, thus it's best to use the full path.
Bram Moolenaar333b80a2018-04-04 22:57:29 +0200860
861 [options] is only used when opening a new window. If present,
Bram Moolenaar68e65602019-05-26 21:33:31 +0200862 it must be a Dict. Similarly to |++opt|, These entries are
863 recognized:
Bram Moolenaar333b80a2018-04-04 22:57:29 +0200864 "ff" file format: "dos", "mac" or "unix"
865 "fileformat" idem
866 "enc" overrides 'fileencoding'
867 "encoding" idem
868 "bin" sets 'binary'
869 "binary" idem
870 "nobin" resets 'binary'
871 "nobinary" idem
872 "bad" specifies behavior for bad characters, see
873 |++bad|
874
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200875 Example in JSON: >
876 ["drop", "path/file.txt", {"ff": "dos"}]
877
878A trick to have Vim send this escape sequence: >
879 exe "set t_ts=\<Esc>]51; t_fs=\x07"
Bram Moolenaar2a77d212018-03-26 21:38:52 +0200880 let &titlestring = '["call","Tapi_TryThis",["hello",123]]'
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +0200881 redraw
882 set t_ts& t_fs&
883
884Rationale: Why not allow for any command or expression? Because that might
885create a security problem.
886
887
888Using the client-server feature ~
889 *terminal-client-server*
890This only works when v:servername is not empty. If needed you can set it,
891before opening the terminal, with: >
892 call remote_startserver('vim-server')
893
894$VIM_SERVERNAME is set in the terminal to pass on the server name.
895
896In the job you can then do something like: >
897 vim --servername $VIM_SERVERNAME --remote +123 some_file.c
898This will open the file "some_file.c" and put the cursor on line 123.
899
900==============================================================================
Bram Moolenaar6bf2c622019-07-04 17:12:09 +02009014. Remote testing *terminal-testing*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200902
903Most Vim tests execute a script inside Vim. For some tests this does not
904work, running the test interferes with the code being tested. To avoid this
905Vim is executed in a terminal window. The test sends keystrokes to it and
906inspects the resulting screen state.
907
908Functions ~
909
Bram Moolenaar6dc819b2018-07-03 16:42:19 +0200910|term_sendkeys()| send keystrokes to a terminal (not subject to tmap)
911|term_wait()| wait for screen to be updated
912|term_scrape()| inspect terminal screen
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200913
914
915==============================================================================
Bram Moolenaar6bf2c622019-07-04 17:12:09 +02009165. Diffing screen dumps *terminal-diff*
Bram Moolenaarda650582018-02-20 15:51:40 +0100917
918In some cases it can be bothersome to test that Vim displays the right
919characters on the screen. E.g. with syntax highlighting. To make this
920simpler it is possible to take a screen dump of a terminal and compare it to
921an expected screen dump.
922
923Vim uses the window size, text, color and other attributes as displayed. The
924Vim screen size, font and other properties do not matter. Therefore this
Bram Moolenaar98ef2332018-03-18 14:44:37 +0100925mechanism is portable across systems. A conventional screenshot would reflect
Bram Moolenaarda650582018-02-20 15:51:40 +0100926all differences, including font size and family.
927
928
929Writing a screen dump test for Vim ~
930 *terminal-dumptest*
931For an example see the Test_syntax_c() function in
932src/testdir/test_syntax.vim. The main parts are:
933- Write a file you want to test with. This is useful for testing syntax
934 highlighting. You can also start Vim with en empty buffer.
935- Run Vim in a terminal with a specific size. The default is 20 lines of 75
936 characters. This makes sure the dump is always this size. The function
937 RunVimInTerminal() takes care of this. Pass it the arguments for the Vim
938 command.
Bram Moolenaar6dc819b2018-07-03 16:42:19 +0200939- Send any commands to Vim using |term_sendkeys()|. For example: >
Bram Moolenaarda650582018-02-20 15:51:40 +0100940 call term_sendkeys(buf, ":echo &lines &columns\<CR>")
941- Check that the screen is now in the expected state, using
942 VerifyScreenDump(). This expects the reference screen dump to be in the
943 src/testdir/dumps/ directory. Pass the name without ".dump". It is
944 recommended to use the name of the test function and a sequence number, so
945 that we know what test is using the file.
946- Repeat sending commands and checking the state.
947- Finally stop Vim by calling StopVimInTerminal().
948
949The first time you do this you won't have a screen dump yet. Create an empty
950file for now, e.g.: >
951 touch src/testdir/dumps/Test_function_name_01.dump
952
953The test will then fail, giving you the command to compare the reference dump
954and the failed dump, e.g.: >
955 call term_dumpdiff("Test_func.dump.failed", "dumps/Test_func.dump")
956
957Use this command in Vim, with the current directory set to src/testdir.
958Once you are satisfied with the test, move the failed dump in place of the
959reference: >
960 :!mv Test_func.dump.failed dumps/Test_func.dump
961
962
963Creating a screen dump ~
964 *terminal-screendump*
965
966To create the screen dump, run Vim (or any other program) in a terminal and
Bram Moolenaar6dc819b2018-07-03 16:42:19 +0200967make it show the desired state. Then use the |term_dumpwrite()| function to
Bram Moolenaarda650582018-02-20 15:51:40 +0100968create a screen dump file. For example: >
969 :call term_dumpwrite(77, "mysyntax.dump")
970
971Here "77" is the buffer number of the terminal. Use `:ls!` to see it.
972
Bram Moolenaar6dc819b2018-07-03 16:42:19 +0200973You can view the screen dump with |term_dumpload()|: >
Bram Moolenaarda650582018-02-20 15:51:40 +0100974 :call term_dumpload("mysyntax.dump")
975
976To verify that Vim still shows exactly the same screen, run Vim again with
977exactly the same way to show the desired state. Then create a screen dump
978again, using a different file name: >
979 :call term_dumpwrite(88, "test.dump")
980
Bram Moolenaar6dc819b2018-07-03 16:42:19 +0200981To assert that the files are exactly the same use |assert_equalfile()|: >
Bram Moolenaarda650582018-02-20 15:51:40 +0100982 call assert_equalfile("mysyntax.dump", "test.dump")
983
984If there are differences then v:errors will contain the error message.
985
986
987Comparing screen dumps ~
988 *terminal-diffscreendump*
989
Bram Moolenaar6dc819b2018-07-03 16:42:19 +0200990|assert_equalfile()| does not make it easy to see what is different.
991To spot the problem use |term_dumpdiff()|: >
Bram Moolenaarda650582018-02-20 15:51:40 +0100992 call term_dumpdiff("mysyntax.dump", "test.dump")
993
994This will open a window consisting of three parts:
9951. The contents of the first dump
9962. The difference between the first and second dump
9973. The contents of the second dump
998
999You can usually see what differs in the second part. Use the 'ruler' to
Bram Moolenaar93a1df22018-09-10 11:51:50 +02001000relate it to the position in the first or second dump. Letters indicate the
1001kind of difference:
1002 X different character
1003 > cursor in first but not in second
1004 < cursor in second but not in first
1005 w character width differs (single vs double width)
1006 f foreground color differs
1007 b background color differs
1008 a attribute differs (bold, underline, reverse, etc.)
1009 ? character missing in both
1010 + character missing in first
1011 - character missing in second
Bram Moolenaarda650582018-02-20 15:51:40 +01001012
Bram Moolenaar98ef2332018-03-18 14:44:37 +01001013Alternatively, press "s" to swap the first and second dump. Do this several
Bram Moolenaarda650582018-02-20 15:51:40 +01001014times so that you can spot the difference in the context of the text.
1015
1016==============================================================================
Bram Moolenaar6bf2c622019-07-04 17:12:09 +020010176. Debugging *terminal-debug* *terminal-debugger*
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001018
1019The Terminal debugging plugin can be used to debug a program with gdb and view
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001020the source code in a Vim window. Since this is completely contained inside
1021Vim this also works remotely over an ssh connection.
1022
Bram Moolenaarb3307b52018-06-17 21:34:11 +02001023When the |+terminal| feature is missing, the plugin will use the "prompt"
1024buffer type, if possible. The running program will then use a newly opened
1025terminal window. See |termdebug-prompt| below for details.
1026
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001027
1028Starting ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +01001029 *termdebug-starting*
Bram Moolenaarc572da52017-08-27 16:52:01 +02001030Load the plugin with this command: >
1031 packadd termdebug
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001032< *:Termdebug*
Bram Moolenaard473c8c2018-08-11 18:00:22 +02001033To start debugging use `:Termdebug` or `:TermdebugCommand` followed by the
Bram Moolenaar32c67ba2018-04-16 16:21:49 +02001034command name, for example: >
Bram Moolenaar24a98a02017-09-27 22:23:55 +02001035 :Termdebug vim
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001036
Bram Moolenaarc572da52017-08-27 16:52:01 +02001037This opens two windows:
Bram Moolenaarf0b03c42017-12-17 17:17:07 +01001038
Bram Moolenaar45d5f262017-09-10 19:14:31 +02001039gdb window A terminal window in which "gdb vim" is executed. Here you
1040 can directly interact with gdb. The buffer name is "!gdb".
Bram Moolenaarf0b03c42017-12-17 17:17:07 +01001041
Bram Moolenaar45d5f262017-09-10 19:14:31 +02001042program window A terminal window for the executed program. When "run" is
1043 used in gdb the program I/O will happen in this window, so
1044 that it does not interfere with controlling gdb. The buffer
1045 name is "gdb program".
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001046
1047The current window is used to show the source code. When gdb pauses the
1048source file location will be displayed, if possible. A sign is used to
Bram Moolenaar71137fe2018-03-03 20:47:21 +01001049highlight the current position, using highlight group debugPC.
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001050
1051If the buffer in the current window is modified, another window will be opened
Bram Moolenaar32c67ba2018-04-16 16:21:49 +02001052to display the current gdb position. You can use `:Winbar` to add a window
1053toolbar there.
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001054
1055Focus the terminal of the executed program to interact with it. This works
1056the same as any command running in a terminal window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001057
Bram Moolenaar45d5f262017-09-10 19:14:31 +02001058When the debugger ends, typically by typing "quit" in the gdb window, the two
1059opened windows are closed.
Bram Moolenaarc572da52017-08-27 16:52:01 +02001060
Bram Moolenaarb3623a32018-04-14 18:59:50 +02001061Only one debugger can be active at a time.
Bram Moolenaar32c67ba2018-04-16 16:21:49 +02001062 *:TermdebugCommand*
1063If you want to give specific commands to the command being debugged, you can
1064use the `:TermdebugCommand` command followed by the command name and
1065additional parameters. >
1066 :TermdebugCommand vim --clean -c ':set nu'
Bram Moolenaarb3623a32018-04-14 18:59:50 +02001067
Bram Moolenaar32c67ba2018-04-16 16:21:49 +02001068Both the `:Termdebug` and `:TermdebugCommand` support an optional "!" bang
1069argument to start the command right away, without pausing at the gdb window
1070(and cursor will be in the debugged window). For example: >
1071 :TermdebugCommand! vim --clean
1072
1073To attach gdb to an already running executable or use a core file, pass extra
Bram Moolenaarb3623a32018-04-14 18:59:50 +02001074arguments. E.g.: >
1075 :Termdebug vim core
1076 :Termdebug vim 98343
1077
Bram Moolenaar32c67ba2018-04-16 16:21:49 +02001078If no argument is given, you'll end up in a gdb window, in which you need to
1079specify which command to run using e.g. the gdb `file` command.
1080
Bram Moolenaarc572da52017-08-27 16:52:01 +02001081
Bram Moolenaar24a98a02017-09-27 22:23:55 +02001082Example session ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +01001083 *termdebug-example*
Bram Moolenaar24a98a02017-09-27 22:23:55 +02001084Start in the Vim "src" directory and build Vim: >
1085 % make
1086Start Vim: >
1087 % ./vim
1088Load the termdebug plugin and start debugging Vim: >
1089 :packadd termdebug
1090 :Termdebug vim
1091You should now have three windows:
1092 source - where you started, has a window toolbar with buttons
1093 gdb - you can type gdb commands here
1094 program - the executed program will use this window
Bram Moolenaar71137fe2018-03-03 20:47:21 +01001095
Bram Moolenaar24a98a02017-09-27 22:23:55 +02001096You can use CTRL-W CTRL-W or the mouse to move focus between windows.
1097Put focus on the gdb window and type: >
1098 break ex_help
1099 run
1100Vim will start running in the program window. Put focus there and type: >
1101 :help gui
1102Gdb will run into the ex_help breakpoint. The source window now shows the
Bram Moolenaarde1a8312018-06-19 16:59:54 +02001103ex_cmds.c file. A red "1 " marker will appear in the signcolumn where the
1104breakpoint was set. The line where the debugger stopped is highlighted. You
1105can now step through the program. Let's use the mouse: click on the "Next"
1106button in the window toolbar. You will see the highlighting move as the
1107debugger executes a line of source code.
Bram Moolenaar24a98a02017-09-27 22:23:55 +02001108
1109Click "Next" a few times until the for loop is highlighted. Put the cursor on
1110the end of "eap->arg", then click "Eval" in the toolbar. You will see this
1111displayed:
1112 "eap->arg": 0x555555e68855 "gui" ~
1113This way you can inspect the value of local variables. You can also focus the
1114gdb window and use a "print" command, e.g.: >
1115 print *eap
Bram Moolenaar71137fe2018-03-03 20:47:21 +01001116If mouse pointer movements are working, Vim will also show a balloon when the
1117mouse rests on text that can be evaluated by gdb.
Bram Moolenaar24a98a02017-09-27 22:23:55 +02001118
1119Now go back to the source window and put the cursor on the first line after
1120the for loop, then type: >
1121 :Break
1122You will see a ">>" marker appear, this indicates the new breakpoint. Now
1123click "Cont" in the toolbar and the code until the breakpoint will be
1124executed.
1125
1126You can type more advanced commands in the gdb window. For example, type: >
1127 watch curbuf
1128Now click "Cont" in the toolbar (or type "cont" in the gdb window). Execution
1129will now continue until the value of "curbuf" changes, which is in do_ecmd().
1130To remove this watchpoint again type in the gdb window: >
1131 delete 3
1132
1133You can see the stack by typing in the gdb window: >
1134 where
1135Move through the stack frames, e.g. with: >
1136 frame 3
1137The source window will show the code, at the point where the call was made to
1138a deeper level.
1139
1140
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001141Stepping through code ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +01001142 *termdebug-stepping*
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001143Put focus on the gdb window to type commands there. Some common ones are:
Bram Moolenaar60e73f22017-11-12 18:02:06 +01001144- CTRL-C interrupt the program
1145- next execute the current line and stop at the next line
1146- step execute the current line and stop at the next statement,
1147 entering functions
1148- finish execute until leaving the current function
1149- where show the stack
1150- frame N go to the Nth stack frame
1151- continue continue execution
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001152
Bram Moolenaar32c67ba2018-04-16 16:21:49 +02001153 *:Run* *:Arguments*
1154In the window showing the source code these commands can be used to control
1155gdb:
Bram Moolenaar71137fe2018-03-03 20:47:21 +01001156 `:Run` [args] run the program with [args] or the previous arguments
1157 `:Arguments` {args} set arguments for the next `:Run`
Bram Moolenaar60e73f22017-11-12 18:02:06 +01001158
Bram Moolenaar32c67ba2018-04-16 16:21:49 +02001159 *:Break* set a breakpoint at the current line; a sign will be displayed
1160 *:Clear* delete the breakpoint at the current line
Bram Moolenaar60e73f22017-11-12 18:02:06 +01001161
Bram Moolenaar32c67ba2018-04-16 16:21:49 +02001162 *:Step* execute the gdb "step" command
1163 *:Over* execute the gdb "next" command (`:Next` is a Vim command)
1164 *:Finish* execute the gdb "finish" command
1165 *:Continue* execute the gdb "continue" command
1166 *:Stop* interrupt the program
Bram Moolenaar45d5f262017-09-10 19:14:31 +02001167
Bram Moolenaarf0b03c42017-12-17 17:17:07 +01001168If 'mouse' is set the plugin adds a window toolbar with these entries:
Bram Moolenaar71137fe2018-03-03 20:47:21 +01001169 Step `:Step`
1170 Next `:Over`
1171 Finish `:Finish`
1172 Cont `:Continue`
1173 Stop `:Stop`
1174 Eval `:Evaluate`
Bram Moolenaarf0b03c42017-12-17 17:17:07 +01001175This way you can use the mouse to perform the most common commands. You need
1176to have the 'mouse' option set to enable mouse clicks.
Bram Moolenaar32c67ba2018-04-16 16:21:49 +02001177 *:Winbar*
Bram Moolenaar71137fe2018-03-03 20:47:21 +01001178You can add the window toolbar in other windows you open with: >
1179 :Winbar
1180
Bram Moolenaarc4b533e2018-04-06 22:26:25 +02001181If gdb stops at a source line and there is no window currently showing the
1182source code, a new window will be created for the source code. This also
1183happens if the buffer in the source code window has been modified and can't be
1184abandoned.
1185
Bram Moolenaarde1a8312018-06-19 16:59:54 +02001186Gdb gives each breakpoint a number. In Vim the number shows up in the sign
1187column, with a red background. You can use these gdb commands:
1188- info break list breakpoints
1189- delete N delete breakpoint N
1190You can also use the `:Clear` command if the cursor is in the line with the
1191breakpoint, or use the "Clear breakpoint" right-click menu entry.
1192
Bram Moolenaar45d5f262017-09-10 19:14:31 +02001193
1194Inspecting variables ~
Bram Moolenaar32c67ba2018-04-16 16:21:49 +02001195 *termdebug-variables* *:Evaluate*
Bram Moolenaar71137fe2018-03-03 20:47:21 +01001196 `:Evaluate` evaluate the expression under the cursor
1197 `K` same
1198 `:Evaluate` {expr} evaluate {expr}
1199 `:'<,'>Evaluate` evaluate the Visually selected text
Bram Moolenaar45d5f262017-09-10 19:14:31 +02001200
1201This is similar to using "print" in the gdb window.
Bram Moolenaar71137fe2018-03-03 20:47:21 +01001202You can usually shorten `:Evaluate` to `:Ev`.
Bram Moolenaar45d5f262017-09-10 19:14:31 +02001203
1204
1205Other commands ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +01001206 *termdebug-commands*
Bram Moolenaar32c67ba2018-04-16 16:21:49 +02001207 *:Gdb* jump to the gdb window
1208 *:Program* jump to the window with the running program
1209 *:Source* jump to the window with the source code, create it if there
Bram Moolenaarc4b533e2018-04-06 22:26:25 +02001210 isn't one
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001211
1212
Bram Moolenaarb3307b52018-06-17 21:34:11 +02001213Prompt mode ~
1214 *termdebug-prompt*
1215When the |+terminal| feature is not supported and on MS-Windows, gdb will run
1216in a buffer with 'buftype' set to "prompt". This works slightly differently:
1217- The gdb window will be in Insert mode while typing commands. Go to Normal
1218 mode with <Esc>, then you can move around in the buffer, copy/paste, etc.
1219 Go back to editing the gdb command with any command that starts Insert mode,
1220 such as `a` or `i`.
1221- The program being debugged will run in a separate window. On MS-Windows
1222 this is a new console window. On Unix, if the |+terminal| feature is
1223 available a Terminal window will be opened to run the debugged program in.
1224
1225 *termdebug_use_prompt*
1226Prompt mode can be used even when the |+terminal| feature is present with: >
1227 let g:termdebug_use_prompt = 1
1228
1229
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001230Communication ~
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +01001231 *termdebug-communication*
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001232There is another, hidden, buffer, which is used for Vim to communicate with
1233gdb. The buffer name is "gdb communication". Do not delete this buffer, it
1234will break the debugger.
1235
Bram Moolenaarde1a8312018-06-19 16:59:54 +02001236Gdb has some weird behavior, the plugin does its best to work around that.
1237For example, after typing "continue" in the gdb window a CTRL-C can be used to
1238interrupt the running program. But after using the MI command
1239"-exec-continue" pressing CTRL-C does not interrupt. Therefore you will see
1240"continue" being used for the `:Continue` command, instead of using the
1241communication channel.
1242
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001243
Bram Moolenaarc572da52017-08-27 16:52:01 +02001244Customizing ~
Bram Moolenaar71137fe2018-03-03 20:47:21 +01001245
1246GDB command *termdebug-customizing*
1247
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001248To change the name of the gdb command, set the "termdebugger" variable before
1249invoking `:Termdebug`: >
1250 let termdebugger = "mygdb"
Bram Moolenaar71137fe2018-03-03 20:47:21 +01001251< *gdb-version*
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001252Only debuggers fully compatible with gdb will work. Vim uses the GDB/MI
Bram Moolenaar98ef2332018-03-18 14:44:37 +01001253interface. The "new-ui" command requires gdb version 7.12 or later. if you
1254get this error:
Bram Moolenaar01164a62017-11-02 22:58:42 +01001255 Undefined command: "new-ui". Try "help".~
1256Then your gdb is too old.
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001257
Bram Moolenaar71137fe2018-03-03 20:47:21 +01001258
1259Colors *hl-debugPC* *hl-debugBreakpoint*
1260
Bram Moolenaare09ba7b2017-09-09 22:19:47 +02001261The color of the signs can be adjusted with these highlight groups:
1262- debugPC the current position
1263- debugBreakpoint a breakpoint
1264
1265The defaults are, when 'background' is "light":
1266 hi debugPC term=reverse ctermbg=lightblue guibg=lightblue
1267 hi debugBreakpoint term=reverse ctermbg=red guibg=red
1268
1269When 'background' is "dark":
1270 hi debugPC term=reverse ctermbg=darkblue guibg=darkblue
1271 hi debugBreakpoint term=reverse ctermbg=red guibg=red
Bram Moolenaarc572da52017-08-27 16:52:01 +02001272
Bram Moolenaar71137fe2018-03-03 20:47:21 +01001273
Bram Moolenaar7dd64a32019-05-31 21:41:05 +02001274Shortcuts *termdebug_shortcuts*
Bram Moolenaarb3307b52018-06-17 21:34:11 +02001275
1276You can define your own shortcuts (mappings) to control gdb, that can work in
1277any window, using the TermDebugSendCommand() function. Example: >
1278 map ,w :call TermDebugSendCommand('where')<CR>
1279The argument is the gdb command.
1280
1281
Bram Moolenaar71137fe2018-03-03 20:47:21 +01001282Popup menu *termdebug_popup*
1283
1284By default the Termdebug plugin sets 'mousemodel' to "popup_setpos" and adds
1285these entries to the popup menu:
1286 Set breakpoint `:Break`
1287 Clear breakpoint `:Clear`
1288 Evaluate `:Evaluate`
1289If you don't want this then disable it with: >
1290 let g:termdebug_popup = 0
1291
1292
1293Vim window width *termdebug_wide*
1294
Bram Moolenaar38baa3e2017-09-14 16:10:38 +02001295To change the width of the Vim window when debugging starts, and use a
1296vertical split: >
1297 let g:termdebug_wide = 163
Bram Moolenaar6dc819b2018-07-03 16:42:19 +02001298This will set &columns to 163 when `:Termdebug` is used. The value is restored
Bram Moolenaar38baa3e2017-09-14 16:10:38 +02001299when quitting the debugger.
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +02001300If g:termdebug_wide is set and &columns is already larger than
Bram Moolenaar24a98a02017-09-27 22:23:55 +02001301g:termdebug_wide then a vertical split will be used without changing &columns.
1302Set it to 1 to get a vertical split without every changing &columns (useful
1303for when the terminal can't be resized by Vim).
Bram Moolenaar38baa3e2017-09-14 16:10:38 +02001304
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001305
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001306
Bram Moolenaar91f84f62018-07-29 15:07:52 +02001307 vim:tw=78:ts=8:noet:ft=help:norl: