blob: 7be6927a0bcd6839020891ef4193340548e1a691 [file] [log] [blame]
Bram Moolenaare561a7e2017-08-29 22:44:59 +02001*terminal.txt* For Vim version 8.0. Last change: 2017 Aug 29
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7Terminal window support *terminal*
8
9
10WARNING: THIS IS ONLY PARTLY IMPLEMENTED, ANYTHING CAN STILL CHANGE
11
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +020012The terminal feature is optional, use this to check if your Vim has it: >
13 echo has('terminal')
14If the result is "1" you have it.
15
Bram Moolenaare4f25e42017-07-07 11:54:15 +020016
171. Basic use |terminal-use|
182. Remote testing |terminal-testing|
193. Debugging |terminal-debug|
20
21{Vi does not have any of these commands}
Bram Moolenaarc572da52017-08-27 16:52:01 +020022{only available when compiled with the |+terminal| feature}
23
24The terminal feature requires the |+multi_byte|, |+job| and |+channel| features.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020025
26==============================================================================
271. Basic use *terminal-use*
28
29This feature is for running a terminal emulator in a Vim window. A job can be
30started connected to the terminal emulator. For example, to run a shell: >
31 :term bash
32
33Or to run a debugger: >
34 :term gdb vim
35
36The job runs asynchronously from Vim, the window will be updated to show
37output from the job, also while editing in any other window.
38
Bram Moolenaar423802d2017-07-30 16:52:24 +020039
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +020040Typing ~
41
Bram Moolenaardd693ce2017-08-10 23:15:19 +020042When the keyboard focus is in the terminal window, typed keys will be sent to
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +020043the job. This uses a pty when possible. You can click outside of the
44terminal window to move keyboard focus elsewhere.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020045
Bram Moolenaar423802d2017-07-30 16:52:24 +020046CTRL-W can be used to navigate between windows and other CTRL-W commands, e.g.:
47 CTRL-W CTRL-W move focus to the next window
48 CTRL-W : enter an Ex command
49See |CTRL-W| for more commands.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020050
Bram Moolenaar423802d2017-07-30 16:52:24 +020051Special in the terminal window: *CTRL-W_.* *CTRL-W_N*
52 CTRL-W . send a CTRL-W to the job in the terminal
Bram Moolenaardd693ce2017-08-10 23:15:19 +020053 CTRL-W N go to Terminal-Normal mode, see |Terminal-mode|
54 CTRL-\ CTRL-N go to Terminal-Normal mode, see |Terminal-mode|
Bram Moolenaarf55e4c82017-08-01 20:44:53 +020055 CTRL-W " {reg} paste register {reg} *CTRL-W_quote*
56 Also works with the = register to insert the result of
57 evaluating an expression.
Bram Moolenaar8e539c52017-08-18 22:57:06 +020058 CTRL-W CTRL-C ends the job, see below |t_CTRL-W_CTRL-C|
Bram Moolenaar423802d2017-07-30 16:52:24 +020059
60See option 'termkey' for specifying another key instead of CTRL-W that
61will work like CTRL-W. However, typing 'termkey' twice sends 'termkey' to
62the job. For example:
63 'termkey' CTRL-W move focus to the next window
64 'termkey' : enter an Ex command
65 'termkey' 'termkey' send 'termkey' to the job in the terminal
66 'termkey' . send a CTRL-W to the job in the terminal
67 'termkey' N go to terminal Normal mode, see below
68 'termkey' CTRL-N same as CTRL-W N
Bram Moolenaar8e539c52017-08-18 22:57:06 +020069 'termkey' CTRL-C same as |t_CTRL-W_CTRL-C|
Bram Moolenaar69198192017-08-05 14:10:48 +020070 *t_CTRL-\_CTRL-N*
Bram Moolenaardd693ce2017-08-10 23:15:19 +020071The special key combination CTRL-\ CTRL-N can be used to switch to Normal
72mode, just like this works in any other mode.
Bram Moolenaar8e539c52017-08-18 22:57:06 +020073 *t_CTRL-W_CTRL-C*
74CTRL-W CTRL-C can be typed to forcefully end the job. On MS-Windows a
75CTRL-BREAK will also kill the job.
76
77If you type CTRL-C the effect depends on what the pty has been configured to
78do. For simple commands this causes a SIGINT to be sent to the job, which
79would end it. Other commands may ignore the SIGINT or handle the CTRL-C
80themselves (like Vim does).
Bram Moolenaar423802d2017-07-30 16:52:24 +020081
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +020082
Bram Moolenaar8e539c52017-08-18 22:57:06 +020083Size and color ~
Bram Moolenaare4f25e42017-07-07 11:54:15 +020084
Bram Moolenaar74675a62017-07-15 13:53:23 +020085See option 'termsize' for controlling the size of the terminal window.
86(TODO: scrolling when the terminal is larger than the window)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020087
Bram Moolenaar8e539c52017-08-18 22:57:06 +020088The terminal uses the 'background' option to decide whether the terminal
89window will start with a white or black background. The job running in the
90terminal can change the colors.
91
Bram Moolenaar423802d2017-07-30 16:52:24 +020092
Bram Moolenaare4f25e42017-07-07 11:54:15 +020093Syntax ~
Bram Moolenaar8a773062017-07-24 22:29:21 +020094
Bram Moolenaardd693ce2017-08-10 23:15:19 +020095:[range]ter[minal] [options] [command] *:ter* *:terminal*
Bram Moolenaar8a773062017-07-24 22:29:21 +020096 Open a new terminal window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020097
98 If [command] is provided run it as a job and connect
99 the input and output to the terminal.
100 If [command] is not given the 'shell' option is used.
Bram Moolenaarc572da52017-08-27 16:52:01 +0200101 if [command] is NONE no job is started, the pty of the
102 terminal can be used by a command like gdb.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200103
104 A new buffer will be created, using [command] or
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200105 'shell' as the name, prefixed with a "!". If a buffer
106 by this name already exists a number is added in
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200107 parentheses. E.g. if "gdb" exists the second terminal
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200108 buffer will use "!gdb (1)".
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200109
Bram Moolenaarb2412082017-08-20 18:09:14 +0200110 If [range] is given the specified lines are used as
111 input for the job. It will not be possible to type
112 keys in the terminal window.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200113
114 Two comma separated numbers are used as "rows,cols".
115 E.g. `:24,80gdb` opens a terminal with 24 rows and 80
116 columns. However, if the terminal window spans the
117 Vim window with, there is no vertical split, the Vim
118 window width is used.
119 *term++close* *term++open*
120 Supported [options] are:
121 ++close The terminal window will close
122 automatically when the job terminates.
123 ++open When the job terminates and no window
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200124 shows it, a window will be opened.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200125 Note that this can be interruptive.
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200126 ++curwin Open the terminal in the current
127 window, do not split the current
128 window. Fails if the current buffer
129 cannot be |abandon|ed.
130 ++hidden Open the terminal in a hidden buffer,
131 no window will be used.
Bram Moolenaarb2412082017-08-20 18:09:14 +0200132 ++rows={height} Use {height} for the terminal window
133 height.
134 ++cols={width} Use {width} for the terminal window
135 width.
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200136 ++eof={text} when using [range], text to send after
137 the last line was written. The default
138 is to send CTRL-D. A CR is appended.
139 E.g. for a shell use "++eof=exit" and
140 for Python "++eof=exit()". Special
141 codes can be used like with `:map`,
142 e.g. "<C-Z>" for CTRL-Z.
143 {only on MS-Windows}
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200144
145 If you want to use more options use the |term_start()|
146 function.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200147
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200148When the buffer associated with the terminal is unloaded or wiped out the job
149is killed, similar to calling `job_stop(job, "kill")`
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200150
Bram Moolenaare561a7e2017-08-29 22:44:59 +0200151So long as the job is running the window behaves like it contains a modified
Bram Moolenaaref68e4f2017-09-02 16:28:36 +0200152buffer. Trying to close the window with `CTRL-W :quit` fails. When using
153`CTRL-W :quit!` the job is ended. The text in the window is lost. The buffer
154still exists, but getting it in a window with `:buffer` will show an empty
155buffer.
156
157Trying to close the window with `CTRL-W :close` also fails. Using
158`CTRL-W :close!` will close the window and make the buffer hidden.
Bram Moolenaare561a7e2017-08-29 22:44:59 +0200159
160You can use `CTRL-W :hide` to close the terminal window and make the buffer
161hidden, the job keeps running. The `:buffer` command can be used to turn the
162current window into a terminal window. If there are unsaved changes this
163fails, use ! to force, as usual.
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200164
165To have a background job run without a window, and open the window when it's
166done, use options like this: >
167 :term ++hidden ++open make
168Note that the window will open at an unexpected moment, this will interrupt
169what you are doing.
170
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200171 *E947* *E948*
Bram Moolenaar78712a72017-08-05 14:50:12 +0200172So long as the job is running, the buffer is considered modified and Vim
173cannot be quit easily, see |abandon|.
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200174
175When the job has finished and no changes were made to the buffer: closing the
176window will wipe out the buffer.
177
178Before changes can be made to a terminal buffer, the 'modifiable' option must
179be set. This is only possible when the job has finished. At the first change
180the buffer will become a normal buffer and the highlighting is removed.
181You may want to change the buffer name with |:file| to be able to write, since
182the buffer name will still be set to the command.
183
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200184
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200185Resizing ~
186
187The size of the terminal can be in one of three modes:
188
1891. The 'termsize' option is empty: The terminal size follows the window size.
190 The minimal size is 2 screen lines with 10 cells.
191
1922. The 'termsize' option is "rows*cols", where "rows" is the minimal number of
Bram Moolenaar8a773062017-07-24 22:29:21 +0200193 screen rows and "cols" is the minimal number of cells.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200194
1953. The 'termsize' option is "rowsXcols" (where the x is upper or lower case).
196 The terminal size is fixed to the specified number of screen lines and
197 cells. If the window is bigger there will be unused empty space.
198
199If the window is smaller than the terminal size, only part of the terminal can
200be seen (the lower-left part).
201
202The |term_getsize()| function can be used to get the current size of the
203terminal. |term_setsize()| can be used only when in the first or second mode,
204not when 'termsize' is "rowsXcols".
205
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200206
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200207Terminal-Job and Terminal-Normal mode ~
Bram Moolenaar423802d2017-07-30 16:52:24 +0200208 *Terminal-mode*
209When the job is running the contents of the terminal is under control of the
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200210job. That includes the cursor position. Typed keys are sent to the job.
211The terminal contents can change at any time. This is called Terminal-Job
212mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200213
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200214Use CTRL-W N (or 'termkey' N) to switch to Terminal-Normal mode. Now the
215contents of the terminal window is under control of Vim, the job output is
216suspended. CTRL-\ CTRL-N does the same.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200217 *E946*
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200218In Terminal-Normal mode you can move the cursor around with the usual Vim
219commands, Visually mark text, yank text, etc. But you cannot change the
220contents of the buffer. The commands that would start insert mode, such as
221'i' and 'a', return to Terminal-Job mode. The window will be updated to show
222the contents of the terminal.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200223
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200224In Terminal-Normal mode the statusline and window title show "(Terminal)". If
225the job ends while in Terminal-Normal mode this changes to
226"(Terminal-finished)".
Bram Moolenaar423802d2017-07-30 16:52:24 +0200227
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200228It is not possible to enter Insert mode from Terminal-Job mode.
229
Bram Moolenaar423802d2017-07-30 16:52:24 +0200230
Bram Moolenaarc572da52017-08-27 16:52:01 +0200231Cursor style ~
232
233By default the cursor in the terminal window uses a not blinking block. The
234normal xterm escape sequences can be used to change the blinking state and the
235shape. Once focus leaves the terminal window Vim will restore the original
236cursor.
237
238An exception is when xterm is started with the "-bc" argument, or another way
239that causes the cursor to blink. This actually means that the blinking flag
240is inverted. Since Vim cannot detect this, the terminal window cursor
241blinking will also be inverted.
242
243
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200244Unix ~
245
246On Unix a pty is used to make it possible to run all kinds of commands. You
247can even run Vim in the terminal! That's used for debugging, see below.
248
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200249Environment variables are used to pass information to the running job:
250 TERM name of the terminal, 'term'
251 ROWS number of rows in the terminal initially
252 LINES same as ROWS
253 COLUMNS number of columns in the terminal initially
254 COLORS number of colors, 't_Co' (256*256*256 in the GUI)
255 VIM_SERVERNAME v:servername
256
257The |client-server| feature can be used to communicate with the Vim instance
258where the job was started. This only works when v:servername is not empty.
259If needed you can set it with: >
260 call remote_startserver('vim-server')
261
262In the job you can then do something like: >
263 vim --servername $VIM_SERVERNAME --remote +123 some_file.c
264This will open the file "some_file.c" and put the cursor on line 123.
265
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200266
267MS-Windows ~
268
Bram Moolenaar8a773062017-07-24 22:29:21 +0200269On MS-Windows winpty is used to make it possible to run all kind of commands.
270Obviously, they must be commands that run in a terminal, not open their own
271window.
272
273You need the following two files from winpty:
274
275 winpty.dll
276 winpty-agent.exe
277
278You can download them from the following page:
279
280 https://github.com/rprichard/winpty
281
Bram Moolenaar8e539c52017-08-18 22:57:06 +0200282Just put the files somewhere in your PATH. You can set the 'winptydll' option
283to point to the right file, if needed. If you have both the 32-bit and 64-bit
284version, rename to winpty32.dll and winpty64.dll to match the way Vim was
285build.
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200286
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200287==============================================================================
2882. Remote testing *terminal-testing*
289
290Most Vim tests execute a script inside Vim. For some tests this does not
291work, running the test interferes with the code being tested. To avoid this
292Vim is executed in a terminal window. The test sends keystrokes to it and
293inspects the resulting screen state.
294
295Functions ~
296
297term_sendkeys() send keystrokes to a terminal
298term_wait() wait for screen to be updated
299term_scrape() inspect terminal screen
300
301
302==============================================================================
3033. Debugging *terminal-debug*
304
305The Terminal debugging plugin can be used to debug a program with gdb and view
Bram Moolenaarc572da52017-08-27 16:52:01 +0200306the source code in a Vim window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200307
Bram Moolenaarc572da52017-08-27 16:52:01 +0200308Load the plugin with this command: >
309 packadd termdebug
310
311To start debugging use `:TermDebug` folowed by the command name, for example: >
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200312 :TermDebug vim
313
Bram Moolenaarc572da52017-08-27 16:52:01 +0200314This opens two windows:
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200315- A terminal window in which "gdb vim" is executed. Here you can directly
316 interact with gdb.
317- A terminal window for the executed program. When "run" is used in gdb the
318 program I/O will happen in this window, so that it does not interfere with
319 controlling gdb.
Bram Moolenaarc572da52017-08-27 16:52:01 +0200320The current window is used to show the source code. When gdb jumps to a
321source file location this window will display the code, if possible. Values
322of variables can be inspected, breakpoints set and cleared, etc.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200323
Bram Moolenaarc572da52017-08-27 16:52:01 +0200324When the debugger ends the two opened windows are closed.
325
326
327Customizing ~
328
329g:debugger The debugger command. Default "gdb".
330
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200331
332TODO
333
334
335 vim:tw=78:ts=8:ft=help:norl: