blob: ae99107aa8c50288ffa4362022f92e2c6bbbc441 [file] [log] [blame]
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001*terminal.txt* For Vim version 8.0. Last change: 2017 Aug 12
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}
22
23==============================================================================
241. Basic use *terminal-use*
25
26This feature is for running a terminal emulator in a Vim window. A job can be
27started connected to the terminal emulator. For example, to run a shell: >
28 :term bash
29
30Or to run a debugger: >
31 :term gdb vim
32
33The job runs asynchronously from Vim, the window will be updated to show
34output from the job, also while editing in any other window.
35
Bram Moolenaar423802d2017-07-30 16:52:24 +020036
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +020037Typing ~
38
Bram Moolenaardd693ce2017-08-10 23:15:19 +020039When the keyboard focus is in the terminal window, typed keys will be sent to
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +020040the job. This uses a pty when possible. You can click outside of the
41terminal window to move keyboard focus elsewhere.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020042
Bram Moolenaar423802d2017-07-30 16:52:24 +020043CTRL-W can be used to navigate between windows and other CTRL-W commands, e.g.:
44 CTRL-W CTRL-W move focus to the next window
45 CTRL-W : enter an Ex command
46See |CTRL-W| for more commands.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020047
Bram Moolenaar423802d2017-07-30 16:52:24 +020048Special in the terminal window: *CTRL-W_.* *CTRL-W_N*
49 CTRL-W . send a CTRL-W to the job in the terminal
Bram Moolenaardd693ce2017-08-10 23:15:19 +020050 CTRL-W N go to Terminal-Normal mode, see |Terminal-mode|
51 CTRL-\ CTRL-N go to Terminal-Normal mode, see |Terminal-mode|
Bram Moolenaarf55e4c82017-08-01 20:44:53 +020052 CTRL-W " {reg} paste register {reg} *CTRL-W_quote*
53 Also works with the = register to insert the result of
54 evaluating an expression.
Bram Moolenaar423802d2017-07-30 16:52:24 +020055
56See option 'termkey' for specifying another key instead of CTRL-W that
57will work like CTRL-W. However, typing 'termkey' twice sends 'termkey' to
58the job. For example:
59 'termkey' CTRL-W move focus to the next window
60 'termkey' : enter an Ex command
61 'termkey' 'termkey' send 'termkey' to the job in the terminal
62 'termkey' . send a CTRL-W to the job in the terminal
63 'termkey' N go to terminal Normal mode, see below
64 'termkey' CTRL-N same as CTRL-W N
Bram Moolenaar69198192017-08-05 14:10:48 +020065 *t_CTRL-\_CTRL-N*
Bram Moolenaardd693ce2017-08-10 23:15:19 +020066The special key combination CTRL-\ CTRL-N can be used to switch to Normal
67mode, just like this works in any other mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +020068
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +020069
70Size ~
Bram Moolenaare4f25e42017-07-07 11:54:15 +020071
Bram Moolenaar74675a62017-07-15 13:53:23 +020072See option 'termsize' for controlling the size of the terminal window.
73(TODO: scrolling when the terminal is larger than the window)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020074
Bram Moolenaar423802d2017-07-30 16:52:24 +020075
Bram Moolenaare4f25e42017-07-07 11:54:15 +020076Syntax ~
Bram Moolenaar8a773062017-07-24 22:29:21 +020077
Bram Moolenaardd693ce2017-08-10 23:15:19 +020078:[range]ter[minal] [options] [command] *:ter* *:terminal*
Bram Moolenaar8a773062017-07-24 22:29:21 +020079 Open a new terminal window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020080
81 If [command] is provided run it as a job and connect
82 the input and output to the terminal.
83 If [command] is not given the 'shell' option is used.
84
85 A new buffer will be created, using [command] or
Bram Moolenaarf55e4c82017-08-01 20:44:53 +020086 'shell' as the name, prefixed with a "!". If a buffer
87 by this name already exists a number is added in
Bram Moolenaardd693ce2017-08-10 23:15:19 +020088 parentheses. E.g. if "gdb" exists the second terminal
Bram Moolenaarf55e4c82017-08-01 20:44:53 +020089 buffer will use "!gdb (1)".
Bram Moolenaare4f25e42017-07-07 11:54:15 +020090
Bram Moolenaardd693ce2017-08-10 23:15:19 +020091 If [range] is given it is used for the terminal size.
92 One number specifies the number of rows. Unless the
93 "vertical" modifier is used, then it is the number of
94 columns.
95
96 Two comma separated numbers are used as "rows,cols".
97 E.g. `:24,80gdb` opens a terminal with 24 rows and 80
98 columns. However, if the terminal window spans the
99 Vim window with, there is no vertical split, the Vim
100 window width is used.
101 *term++close* *term++open*
102 Supported [options] are:
103 ++close The terminal window will close
104 automatically when the job terminates.
105 ++open When the job terminates and no window
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200106 shows it, a window will be opened.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200107 Note that this can be interruptive.
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200108 ++curwin Open the terminal in the current
109 window, do not split the current
110 window. Fails if the current buffer
111 cannot be |abandon|ed.
112 ++hidden Open the terminal in a hidden buffer,
113 no window will be used.
114
115 If you want to use more options use the |term_start()|
116 function.
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200117
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200118When the buffer associated with the terminal is wiped out the job is killed,
119similar to calling `job_stop(job, "kill")`
120
Bram Moolenaar78712a72017-08-05 14:50:12 +0200121By default the 'bufhidden' option of the buffer will be set to "hide".
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200122So long as the job is running: If the window is closed the buffer becomes
123hidden. The command will not be stopped. The `:buffer` command can be used
124to turn the current window into a terminal window. If there are unsaved
125changes this fails, use ! to force, as usual.
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200126
127To have a background job run without a window, and open the window when it's
128done, use options like this: >
129 :term ++hidden ++open make
130Note that the window will open at an unexpected moment, this will interrupt
131what you are doing.
132
Bram Moolenaar78712a72017-08-05 14:50:12 +0200133 *E947*
134So long as the job is running, the buffer is considered modified and Vim
135cannot be quit easily, see |abandon|.
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200136
137When the job has finished and no changes were made to the buffer: closing the
138window will wipe out the buffer.
139
140Before changes can be made to a terminal buffer, the 'modifiable' option must
141be set. This is only possible when the job has finished. At the first change
142the buffer will become a normal buffer and the highlighting is removed.
143You may want to change the buffer name with |:file| to be able to write, since
144the buffer name will still be set to the command.
145
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200146
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200147Resizing ~
148
149The size of the terminal can be in one of three modes:
150
1511. The 'termsize' option is empty: The terminal size follows the window size.
152 The minimal size is 2 screen lines with 10 cells.
153
1542. The 'termsize' option is "rows*cols", where "rows" is the minimal number of
Bram Moolenaar8a773062017-07-24 22:29:21 +0200155 screen rows and "cols" is the minimal number of cells.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200156
1573. The 'termsize' option is "rowsXcols" (where the x is upper or lower case).
158 The terminal size is fixed to the specified number of screen lines and
159 cells. If the window is bigger there will be unused empty space.
160
161If the window is smaller than the terminal size, only part of the terminal can
162be seen (the lower-left part).
163
164The |term_getsize()| function can be used to get the current size of the
165terminal. |term_setsize()| can be used only when in the first or second mode,
166not when 'termsize' is "rowsXcols".
167
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200168
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200169Terminal-Job and Terminal-Normal mode ~
Bram Moolenaar423802d2017-07-30 16:52:24 +0200170 *Terminal-mode*
171When the job is running the contents of the terminal is under control of the
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200172job. That includes the cursor position. Typed keys are sent to the job.
173The terminal contents can change at any time. This is called Terminal-Job
174mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200175
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200176Use CTRL-W N (or 'termkey' N) to switch to Terminal-Normal mode. Now the
177contents of the terminal window is under control of Vim, the job output is
178suspended. CTRL-\ CTRL-N does the same.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200179 *E946*
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200180In Terminal-Normal mode you can move the cursor around with the usual Vim
181commands, Visually mark text, yank text, etc. But you cannot change the
182contents of the buffer. The commands that would start insert mode, such as
183'i' and 'a', return to Terminal-Job mode. The window will be updated to show
184the contents of the terminal.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200185
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200186In Terminal-Normal mode the statusline and window title show "(Terminal)". If
187the job ends while in Terminal-Normal mode this changes to
188"(Terminal-finished)".
Bram Moolenaar423802d2017-07-30 16:52:24 +0200189
190
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200191Unix ~
192
193On Unix a pty is used to make it possible to run all kinds of commands. You
194can even run Vim in the terminal! That's used for debugging, see below.
195
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200196Environment variables are used to pass information to the running job:
197 TERM name of the terminal, 'term'
198 ROWS number of rows in the terminal initially
199 LINES same as ROWS
200 COLUMNS number of columns in the terminal initially
201 COLORS number of colors, 't_Co' (256*256*256 in the GUI)
202 VIM_SERVERNAME v:servername
203
204The |client-server| feature can be used to communicate with the Vim instance
205where the job was started. This only works when v:servername is not empty.
206If needed you can set it with: >
207 call remote_startserver('vim-server')
208
209In the job you can then do something like: >
210 vim --servername $VIM_SERVERNAME --remote +123 some_file.c
211This will open the file "some_file.c" and put the cursor on line 123.
212
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200213
214MS-Windows ~
215
Bram Moolenaar8a773062017-07-24 22:29:21 +0200216On MS-Windows winpty is used to make it possible to run all kind of commands.
217Obviously, they must be commands that run in a terminal, not open their own
218window.
219
220You need the following two files from winpty:
221
222 winpty.dll
223 winpty-agent.exe
224
225You can download them from the following page:
226
227 https://github.com/rprichard/winpty
228
229Just put the files somewhere in your PATH.
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200230
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200231==============================================================================
2322. Remote testing *terminal-testing*
233
234Most Vim tests execute a script inside Vim. For some tests this does not
235work, running the test interferes with the code being tested. To avoid this
236Vim is executed in a terminal window. The test sends keystrokes to it and
237inspects the resulting screen state.
238
239Functions ~
240
241term_sendkeys() send keystrokes to a terminal
242term_wait() wait for screen to be updated
243term_scrape() inspect terminal screen
244
245
246==============================================================================
2473. Debugging *terminal-debug*
248
249The Terminal debugging plugin can be used to debug a program with gdb and view
250the source code in a Vim window. For example: >
251
252 :TermDebug vim
253
254This opens three windows:
255- A terminal window in which "gdb vim" is executed. Here you can directly
256 interact with gdb.
257- A terminal window for the executed program. When "run" is used in gdb the
258 program I/O will happen in this window, so that it does not interfere with
259 controlling gdb.
260- A normal Vim window used to show the source code. When gdb jumps to a
261 source file location this window will display the code, if possible. Values
262 of variables can be inspected, breakpoints set and cleared, etc.
263
264This uses two terminal windows. To open the gdb window: >
265 :term gdb [arguments]
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200266To open the terminal to run the tested program |term_start()| is used.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200267
268TODO
269
270
271 vim:tw=78:ts=8:ft=help:norl: