Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 1 | *terminal.txt* For Vim version 8.0. Last change: 2017 Aug 20 |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
| 5 | |
| 6 | |
| 7 | Terminal window support *terminal* |
| 8 | |
| 9 | |
| 10 | WARNING: THIS IS ONLY PARTLY IMPLEMENTED, ANYTHING CAN STILL CHANGE |
| 11 | |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 12 | The terminal feature is optional, use this to check if your Vim has it: > |
| 13 | echo has('terminal') |
| 14 | If the result is "1" you have it. |
| 15 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 16 | |
| 17 | 1. Basic use |terminal-use| |
| 18 | 2. Remote testing |terminal-testing| |
| 19 | 3. Debugging |terminal-debug| |
| 20 | |
| 21 | {Vi does not have any of these commands} |
| 22 | |
| 23 | ============================================================================== |
| 24 | 1. Basic use *terminal-use* |
| 25 | |
| 26 | This feature is for running a terminal emulator in a Vim window. A job can be |
| 27 | started connected to the terminal emulator. For example, to run a shell: > |
| 28 | :term bash |
| 29 | |
| 30 | Or to run a debugger: > |
| 31 | :term gdb vim |
| 32 | |
| 33 | The job runs asynchronously from Vim, the window will be updated to show |
| 34 | output from the job, also while editing in any other window. |
| 35 | |
Bram Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 36 | |
Bram Moolenaar | 1f28b4c | 2017-07-28 13:48:34 +0200 | [diff] [blame] | 37 | Typing ~ |
| 38 | |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 39 | When the keyboard focus is in the terminal window, typed keys will be sent to |
Bram Moolenaar | 1f28b4c | 2017-07-28 13:48:34 +0200 | [diff] [blame] | 40 | the job. This uses a pty when possible. You can click outside of the |
| 41 | terminal window to move keyboard focus elsewhere. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 42 | |
Bram Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 43 | CTRL-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 |
| 46 | See |CTRL-W| for more commands. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 47 | |
Bram Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 48 | Special in the terminal window: *CTRL-W_.* *CTRL-W_N* |
| 49 | CTRL-W . send a CTRL-W to the job in the terminal |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 50 | CTRL-W N go to Terminal-Normal mode, see |Terminal-mode| |
| 51 | CTRL-\ CTRL-N go to Terminal-Normal mode, see |Terminal-mode| |
Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 52 | 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 Moolenaar | 8e539c5 | 2017-08-18 22:57:06 +0200 | [diff] [blame] | 55 | CTRL-W CTRL-C ends the job, see below |t_CTRL-W_CTRL-C| |
Bram Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 56 | |
| 57 | See option 'termkey' for specifying another key instead of CTRL-W that |
| 58 | will work like CTRL-W. However, typing 'termkey' twice sends 'termkey' to |
| 59 | the job. For example: |
| 60 | 'termkey' CTRL-W move focus to the next window |
| 61 | 'termkey' : enter an Ex command |
| 62 | 'termkey' 'termkey' send 'termkey' to the job in the terminal |
| 63 | 'termkey' . send a CTRL-W to the job in the terminal |
| 64 | 'termkey' N go to terminal Normal mode, see below |
| 65 | 'termkey' CTRL-N same as CTRL-W N |
Bram Moolenaar | 8e539c5 | 2017-08-18 22:57:06 +0200 | [diff] [blame] | 66 | 'termkey' CTRL-C same as |t_CTRL-W_CTRL-C| |
Bram Moolenaar | 6919819 | 2017-08-05 14:10:48 +0200 | [diff] [blame] | 67 | *t_CTRL-\_CTRL-N* |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 68 | The special key combination CTRL-\ CTRL-N can be used to switch to Normal |
| 69 | mode, just like this works in any other mode. |
Bram Moolenaar | 8e539c5 | 2017-08-18 22:57:06 +0200 | [diff] [blame] | 70 | *t_CTRL-W_CTRL-C* |
| 71 | CTRL-W CTRL-C can be typed to forcefully end the job. On MS-Windows a |
| 72 | CTRL-BREAK will also kill the job. |
| 73 | |
| 74 | If you type CTRL-C the effect depends on what the pty has been configured to |
| 75 | do. For simple commands this causes a SIGINT to be sent to the job, which |
| 76 | would end it. Other commands may ignore the SIGINT or handle the CTRL-C |
| 77 | themselves (like Vim does). |
Bram Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 78 | |
Bram Moolenaar | 1f28b4c | 2017-07-28 13:48:34 +0200 | [diff] [blame] | 79 | |
Bram Moolenaar | 8e539c5 | 2017-08-18 22:57:06 +0200 | [diff] [blame] | 80 | Size and color ~ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 81 | |
Bram Moolenaar | 74675a6 | 2017-07-15 13:53:23 +0200 | [diff] [blame] | 82 | See option 'termsize' for controlling the size of the terminal window. |
| 83 | (TODO: scrolling when the terminal is larger than the window) |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 84 | |
Bram Moolenaar | 8e539c5 | 2017-08-18 22:57:06 +0200 | [diff] [blame] | 85 | The terminal uses the 'background' option to decide whether the terminal |
| 86 | window will start with a white or black background. The job running in the |
| 87 | terminal can change the colors. |
| 88 | |
Bram Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 89 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 90 | Syntax ~ |
Bram Moolenaar | 8a77306 | 2017-07-24 22:29:21 +0200 | [diff] [blame] | 91 | |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 92 | :[range]ter[minal] [options] [command] *:ter* *:terminal* |
Bram Moolenaar | 8a77306 | 2017-07-24 22:29:21 +0200 | [diff] [blame] | 93 | Open a new terminal window. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 94 | |
| 95 | If [command] is provided run it as a job and connect |
| 96 | the input and output to the terminal. |
| 97 | If [command] is not given the 'shell' option is used. |
| 98 | |
| 99 | A new buffer will be created, using [command] or |
Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 100 | 'shell' as the name, prefixed with a "!". If a buffer |
| 101 | by this name already exists a number is added in |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 102 | parentheses. E.g. if "gdb" exists the second terminal |
Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 103 | buffer will use "!gdb (1)". |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 104 | |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 105 | If [range] is given the specified lines are used as |
| 106 | input for the job. It will not be possible to type |
| 107 | keys in the terminal window. |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 108 | |
| 109 | Two comma separated numbers are used as "rows,cols". |
| 110 | E.g. `:24,80gdb` opens a terminal with 24 rows and 80 |
| 111 | columns. However, if the terminal window spans the |
| 112 | Vim window with, there is no vertical split, the Vim |
| 113 | window width is used. |
| 114 | *term++close* *term++open* |
| 115 | Supported [options] are: |
| 116 | ++close The terminal window will close |
| 117 | automatically when the job terminates. |
| 118 | ++open When the job terminates and no window |
Bram Moolenaar | 8cad930 | 2017-08-12 14:32:32 +0200 | [diff] [blame] | 119 | shows it, a window will be opened. |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 120 | Note that this can be interruptive. |
Bram Moolenaar | 8cad930 | 2017-08-12 14:32:32 +0200 | [diff] [blame] | 121 | ++curwin Open the terminal in the current |
| 122 | window, do not split the current |
| 123 | window. Fails if the current buffer |
| 124 | cannot be |abandon|ed. |
| 125 | ++hidden Open the terminal in a hidden buffer, |
| 126 | no window will be used. |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 127 | ++rows={height} Use {height} for the terminal window |
| 128 | height. |
| 129 | ++cols={width} Use {width} for the terminal window |
| 130 | width. |
Bram Moolenaar | 8cad930 | 2017-08-12 14:32:32 +0200 | [diff] [blame] | 131 | |
| 132 | If you want to use more options use the |term_start()| |
| 133 | function. |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 134 | |
Bram Moolenaar | 8e539c5 | 2017-08-18 22:57:06 +0200 | [diff] [blame] | 135 | When the buffer associated with the terminal is unloaded or wiped out the job |
| 136 | is killed, similar to calling `job_stop(job, "kill")` |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 137 | |
Bram Moolenaar | 78712a7 | 2017-08-05 14:50:12 +0200 | [diff] [blame] | 138 | By default the 'bufhidden' option of the buffer will be set to "hide". |
Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 139 | So long as the job is running: If the window is closed the buffer becomes |
| 140 | hidden. The command will not be stopped. The `:buffer` command can be used |
| 141 | to turn the current window into a terminal window. If there are unsaved |
| 142 | changes this fails, use ! to force, as usual. |
Bram Moolenaar | 8cad930 | 2017-08-12 14:32:32 +0200 | [diff] [blame] | 143 | |
| 144 | To have a background job run without a window, and open the window when it's |
| 145 | done, use options like this: > |
| 146 | :term ++hidden ++open make |
| 147 | Note that the window will open at an unexpected moment, this will interrupt |
| 148 | what you are doing. |
| 149 | |
Bram Moolenaar | 8e539c5 | 2017-08-18 22:57:06 +0200 | [diff] [blame] | 150 | *E947* *E948* |
Bram Moolenaar | 78712a7 | 2017-08-05 14:50:12 +0200 | [diff] [blame] | 151 | So long as the job is running, the buffer is considered modified and Vim |
| 152 | cannot be quit easily, see |abandon|. |
Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 153 | |
| 154 | When the job has finished and no changes were made to the buffer: closing the |
| 155 | window will wipe out the buffer. |
| 156 | |
| 157 | Before changes can be made to a terminal buffer, the 'modifiable' option must |
| 158 | be set. This is only possible when the job has finished. At the first change |
| 159 | the buffer will become a normal buffer and the highlighting is removed. |
| 160 | You may want to change the buffer name with |:file| to be able to write, since |
| 161 | the buffer name will still be set to the command. |
| 162 | |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 163 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 164 | Resizing ~ |
| 165 | |
| 166 | The size of the terminal can be in one of three modes: |
| 167 | |
| 168 | 1. The 'termsize' option is empty: The terminal size follows the window size. |
| 169 | The minimal size is 2 screen lines with 10 cells. |
| 170 | |
| 171 | 2. The 'termsize' option is "rows*cols", where "rows" is the minimal number of |
Bram Moolenaar | 8a77306 | 2017-07-24 22:29:21 +0200 | [diff] [blame] | 172 | screen rows and "cols" is the minimal number of cells. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 173 | |
| 174 | 3. The 'termsize' option is "rowsXcols" (where the x is upper or lower case). |
| 175 | The terminal size is fixed to the specified number of screen lines and |
| 176 | cells. If the window is bigger there will be unused empty space. |
| 177 | |
| 178 | If the window is smaller than the terminal size, only part of the terminal can |
| 179 | be seen (the lower-left part). |
| 180 | |
| 181 | The |term_getsize()| function can be used to get the current size of the |
| 182 | terminal. |term_setsize()| can be used only when in the first or second mode, |
| 183 | not when 'termsize' is "rowsXcols". |
| 184 | |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 185 | |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 186 | Terminal-Job and Terminal-Normal mode ~ |
Bram Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 187 | *Terminal-mode* |
| 188 | When the job is running the contents of the terminal is under control of the |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 189 | job. That includes the cursor position. Typed keys are sent to the job. |
| 190 | The terminal contents can change at any time. This is called Terminal-Job |
| 191 | mode. |
Bram Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 192 | |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 193 | Use CTRL-W N (or 'termkey' N) to switch to Terminal-Normal mode. Now the |
| 194 | contents of the terminal window is under control of Vim, the job output is |
| 195 | suspended. CTRL-\ CTRL-N does the same. |
Bram Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 196 | *E946* |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 197 | In Terminal-Normal mode you can move the cursor around with the usual Vim |
| 198 | commands, Visually mark text, yank text, etc. But you cannot change the |
| 199 | contents of the buffer. The commands that would start insert mode, such as |
| 200 | 'i' and 'a', return to Terminal-Job mode. The window will be updated to show |
| 201 | the contents of the terminal. |
Bram Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 202 | |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 203 | In Terminal-Normal mode the statusline and window title show "(Terminal)". If |
| 204 | the job ends while in Terminal-Normal mode this changes to |
| 205 | "(Terminal-finished)". |
Bram Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 206 | |
Bram Moolenaar | 8e539c5 | 2017-08-18 22:57:06 +0200 | [diff] [blame] | 207 | It is not possible to enter Insert mode from Terminal-Job mode. |
| 208 | |
Bram Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 209 | |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 210 | Unix ~ |
| 211 | |
| 212 | On Unix a pty is used to make it possible to run all kinds of commands. You |
| 213 | can even run Vim in the terminal! That's used for debugging, see below. |
| 214 | |
Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 215 | Environment variables are used to pass information to the running job: |
| 216 | TERM name of the terminal, 'term' |
| 217 | ROWS number of rows in the terminal initially |
| 218 | LINES same as ROWS |
| 219 | COLUMNS number of columns in the terminal initially |
| 220 | COLORS number of colors, 't_Co' (256*256*256 in the GUI) |
| 221 | VIM_SERVERNAME v:servername |
| 222 | |
| 223 | The |client-server| feature can be used to communicate with the Vim instance |
| 224 | where the job was started. This only works when v:servername is not empty. |
| 225 | If needed you can set it with: > |
| 226 | call remote_startserver('vim-server') |
| 227 | |
| 228 | In the job you can then do something like: > |
| 229 | vim --servername $VIM_SERVERNAME --remote +123 some_file.c |
| 230 | This will open the file "some_file.c" and put the cursor on line 123. |
| 231 | |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 232 | |
| 233 | MS-Windows ~ |
| 234 | |
Bram Moolenaar | 8a77306 | 2017-07-24 22:29:21 +0200 | [diff] [blame] | 235 | On MS-Windows winpty is used to make it possible to run all kind of commands. |
| 236 | Obviously, they must be commands that run in a terminal, not open their own |
| 237 | window. |
| 238 | |
| 239 | You need the following two files from winpty: |
| 240 | |
| 241 | winpty.dll |
| 242 | winpty-agent.exe |
| 243 | |
| 244 | You can download them from the following page: |
| 245 | |
| 246 | https://github.com/rprichard/winpty |
| 247 | |
Bram Moolenaar | 8e539c5 | 2017-08-18 22:57:06 +0200 | [diff] [blame] | 248 | Just put the files somewhere in your PATH. You can set the 'winptydll' option |
| 249 | to point to the right file, if needed. If you have both the 32-bit and 64-bit |
| 250 | version, rename to winpty32.dll and winpty64.dll to match the way Vim was |
| 251 | build. |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 252 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 253 | ============================================================================== |
| 254 | 2. Remote testing *terminal-testing* |
| 255 | |
| 256 | Most Vim tests execute a script inside Vim. For some tests this does not |
| 257 | work, running the test interferes with the code being tested. To avoid this |
| 258 | Vim is executed in a terminal window. The test sends keystrokes to it and |
| 259 | inspects the resulting screen state. |
| 260 | |
| 261 | Functions ~ |
| 262 | |
| 263 | term_sendkeys() send keystrokes to a terminal |
| 264 | term_wait() wait for screen to be updated |
| 265 | term_scrape() inspect terminal screen |
| 266 | |
| 267 | |
| 268 | ============================================================================== |
| 269 | 3. Debugging *terminal-debug* |
| 270 | |
| 271 | The Terminal debugging plugin can be used to debug a program with gdb and view |
| 272 | the source code in a Vim window. For example: > |
| 273 | |
| 274 | :TermDebug vim |
| 275 | |
| 276 | This opens three windows: |
| 277 | - A terminal window in which "gdb vim" is executed. Here you can directly |
| 278 | interact with gdb. |
| 279 | - A terminal window for the executed program. When "run" is used in gdb the |
| 280 | program I/O will happen in this window, so that it does not interfere with |
| 281 | controlling gdb. |
| 282 | - A normal Vim window used to show the source code. When gdb jumps to a |
| 283 | source file location this window will display the code, if possible. Values |
| 284 | of variables can be inspected, breakpoints set and cleared, etc. |
| 285 | |
| 286 | This uses two terminal windows. To open the gdb window: > |
| 287 | :term gdb [arguments] |
Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 288 | To open the terminal to run the tested program |term_start()| is used. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 289 | |
| 290 | TODO |
| 291 | |
| 292 | |
| 293 | vim:tw=78:ts=8:ft=help:norl: |