blob: f20c18ca33665c4a2fe1197428d0c9ccc8b90cc4 [file] [log] [blame]
Bram Moolenaarf55e4c82017-08-01 20:44:53 +02001*terminal.txt* For Vim version 8.0. Last change: 2017 Aug 01
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 Moolenaare4f25e42017-07-07 11:54:15 +020039When the keyboard focus is in the terminal window, typed keys will be send 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
50 CTRL-W N go to Terminal Normal mode, see |Terminal-mode|
Bram Moolenaarf55e4c82017-08-01 20:44:53 +020051 CTRL-W " {reg} paste register {reg} *CTRL-W_quote*
52 Also works with the = register to insert the result of
53 evaluating an expression.
Bram Moolenaar423802d2017-07-30 16:52:24 +020054
55See option 'termkey' for specifying another key instead of CTRL-W that
56will work like CTRL-W. However, typing 'termkey' twice sends 'termkey' to
57the job. For example:
58 'termkey' CTRL-W move focus to the next window
59 'termkey' : enter an Ex command
60 'termkey' 'termkey' send 'termkey' to the job in the terminal
61 'termkey' . send a CTRL-W to the job in the terminal
62 'termkey' N go to terminal Normal mode, see below
63 'termkey' CTRL-N same as CTRL-W N
Bram Moolenaar69198192017-08-05 14:10:48 +020064 *t_CTRL-\_CTRL-N*
65The special key combination CTRL-\ CTRL-N can be used to prefix one Normal
66mode command. This is especially useful for remote commands, when you don't
67know whether Vim currently has focus in a terminal window. Note that only one
68Normal mode command can be used.
Bram Moolenaar423802d2017-07-30 16:52:24 +020069
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +020070
71Size ~
Bram Moolenaare4f25e42017-07-07 11:54:15 +020072
Bram Moolenaar74675a62017-07-15 13:53:23 +020073See option 'termsize' for controlling the size of the terminal window.
74(TODO: scrolling when the terminal is larger than the window)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020075
Bram Moolenaar423802d2017-07-30 16:52:24 +020076
Bram Moolenaare4f25e42017-07-07 11:54:15 +020077Syntax ~
Bram Moolenaar8a773062017-07-24 22:29:21 +020078
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +020079:ter[minal] [command] *:ter* *:terminal*
Bram Moolenaar8a773062017-07-24 22:29:21 +020080 Open a new terminal window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020081
82 If [command] is provided run it as a job and connect
83 the input and output to the terminal.
84 If [command] is not given the 'shell' option is used.
85
86 A new buffer will be created, using [command] or
Bram Moolenaarf55e4c82017-08-01 20:44:53 +020087 'shell' as the name, prefixed with a "!". If a buffer
88 by this name already exists a number is added in
89 parenthesis. E.g. if "gdb" exists the second terminal
90 buffer will use "!gdb (1)".
Bram Moolenaare4f25e42017-07-07 11:54:15 +020091
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +020092When the buffer associated with the terminal is wiped out the job is killed,
93similar to calling `job_stop(job, "kill")`
94
Bram Moolenaarf55e4c82017-08-01 20:44:53 +020095So long as the job is running: If the window is closed the buffer becomes
96hidden. The command will not be stopped. The `:buffer` command can be used
97to turn the current window into a terminal window. If there are unsaved
98changes this fails, use ! to force, as usual.
99
100When the job has finished and no changes were made to the buffer: closing the
101window will wipe out the buffer.
102
103Before changes can be made to a terminal buffer, the 'modifiable' option must
104be set. This is only possible when the job has finished. At the first change
105the buffer will become a normal buffer and the highlighting is removed.
106You may want to change the buffer name with |:file| to be able to write, since
107the buffer name will still be set to the command.
108
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200109
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200110Resizing ~
111
112The size of the terminal can be in one of three modes:
113
1141. The 'termsize' option is empty: The terminal size follows the window size.
115 The minimal size is 2 screen lines with 10 cells.
116
1172. The 'termsize' option is "rows*cols", where "rows" is the minimal number of
Bram Moolenaar8a773062017-07-24 22:29:21 +0200118 screen rows and "cols" is the minimal number of cells.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200119
1203. The 'termsize' option is "rowsXcols" (where the x is upper or lower case).
121 The terminal size is fixed to the specified number of screen lines and
122 cells. If the window is bigger there will be unused empty space.
123
124If the window is smaller than the terminal size, only part of the terminal can
125be seen (the lower-left part).
126
127The |term_getsize()| function can be used to get the current size of the
128terminal. |term_setsize()| can be used only when in the first or second mode,
129not when 'termsize' is "rowsXcols".
130
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200131
Bram Moolenaar423802d2017-07-30 16:52:24 +0200132Terminal Normal mode ~
133 *Terminal-mode*
134When the job is running the contents of the terminal is under control of the
135job. That includes the cursor position. The terminal contents can change at
136any time.
137
138Use CTRL-W N (or 'termkey' N) to go to Terminal Normal mode. Now the contents
139of the terminal window is under control of Vim, the job output is suspended.
140 *E946*
141In this mode you can move the cursor around with the usual Vim commands,
142Visually mark text, yank text, etc. But you cannot change the contents of the
143buffer. The commands that would start insert mode, such as 'i' and 'a',
144return control of the window to the job. Any pending output will now be
145displayed.
146
147In Terminal mode the statusline and window title show "(Terminal)". If the
148job ends while in Terminal mode this changes to "(Terminal-finished)".
149
Bram Moolenaar69198192017-08-05 14:10:48 +0200150Environment variables are used to pass information to the running job:
151 TERM name of the terminal, 'term'
152 ROWS number of rows in the terminal initially
153 LINES same as ROWS
154 COLUMNS number of columns in the terminal initially
155 COLORS number of colors, 't_Co' (256*256*256 in the GUI)
156 VIM_SERVERNAME v:servername
157
158The |client-server| feature can be used to communicate with the Vim instance
159where the job was started. This only works when v:servername is not empty.
160If needed you can set it with: >
161 call remote_startserver('vim-server')
162
163In the job you can then do something like: >
164 vim --servername $VIM_SERVERNAME --remote +123 some_file.c
165This will open the file "some_file.c" and put the cursor on line 123.
166
Bram Moolenaar423802d2017-07-30 16:52:24 +0200167
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200168Unix ~
169
170On Unix a pty is used to make it possible to run all kinds of commands. You
171can even run Vim in the terminal! That's used for debugging, see below.
172
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200173Environment variables are used to pass information to the running job:
174 TERM name of the terminal, 'term'
175 ROWS number of rows in the terminal initially
176 LINES same as ROWS
177 COLUMNS number of columns in the terminal initially
178 COLORS number of colors, 't_Co' (256*256*256 in the GUI)
179 VIM_SERVERNAME v:servername
180
181The |client-server| feature can be used to communicate with the Vim instance
182where the job was started. This only works when v:servername is not empty.
183If needed you can set it with: >
184 call remote_startserver('vim-server')
185
186In the job you can then do something like: >
187 vim --servername $VIM_SERVERNAME --remote +123 some_file.c
188This will open the file "some_file.c" and put the cursor on line 123.
189
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200190
191MS-Windows ~
192
Bram Moolenaar8a773062017-07-24 22:29:21 +0200193On MS-Windows winpty is used to make it possible to run all kind of commands.
194Obviously, they must be commands that run in a terminal, not open their own
195window.
196
197You need the following two files from winpty:
198
199 winpty.dll
200 winpty-agent.exe
201
202You can download them from the following page:
203
204 https://github.com/rprichard/winpty
205
206Just put the files somewhere in your PATH.
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200207
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200208==============================================================================
2092. Remote testing *terminal-testing*
210
211Most Vim tests execute a script inside Vim. For some tests this does not
212work, running the test interferes with the code being tested. To avoid this
213Vim is executed in a terminal window. The test sends keystrokes to it and
214inspects the resulting screen state.
215
216Functions ~
217
218term_sendkeys() send keystrokes to a terminal
219term_wait() wait for screen to be updated
220term_scrape() inspect terminal screen
221
222
223==============================================================================
2243. Debugging *terminal-debug*
225
226The Terminal debugging plugin can be used to debug a program with gdb and view
227the source code in a Vim window. For example: >
228
229 :TermDebug vim
230
231This opens three windows:
232- A terminal window in which "gdb vim" is executed. Here you can directly
233 interact with gdb.
234- A terminal window for the executed program. When "run" is used in gdb the
235 program I/O will happen in this window, so that it does not interfere with
236 controlling gdb.
237- A normal Vim window used to show the source code. When gdb jumps to a
238 source file location this window will display the code, if possible. Values
239 of variables can be inspected, breakpoints set and cleared, etc.
240
241This uses two terminal windows. To open the gdb window: >
242 :term gdb [arguments]
Bram Moolenaarf55e4c82017-08-01 20:44:53 +0200243To open the terminal to run the tested program |term_start()| is used.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200244
245TODO
246
247
248 vim:tw=78:ts=8:ft=help:norl: