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