Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 1 | *terminal.txt* For Vim version 8.0. Last change: 2017 Aug 01 |
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 | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 39 | When the keyboard focus is in the terminal window, typed keys will be send 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 |
| 50 | CTRL-W N go to Terminal Normal mode, see |Terminal-mode| |
Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 51 | 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 Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 54 | |
| 55 | See option 'termkey' for specifying another key instead of CTRL-W that |
| 56 | will work like CTRL-W. However, typing 'termkey' twice sends 'termkey' to |
| 57 | the 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 |
| 64 | |
Bram Moolenaar | 1f28b4c | 2017-07-28 13:48:34 +0200 | [diff] [blame] | 65 | |
| 66 | Size ~ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 67 | |
Bram Moolenaar | 74675a6 | 2017-07-15 13:53:23 +0200 | [diff] [blame] | 68 | See option 'termsize' for controlling the size of the terminal window. |
| 69 | (TODO: scrolling when the terminal is larger than the window) |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 70 | |
Bram Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 71 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 72 | Syntax ~ |
Bram Moolenaar | 8a77306 | 2017-07-24 22:29:21 +0200 | [diff] [blame] | 73 | |
Bram Moolenaar | 1f28b4c | 2017-07-28 13:48:34 +0200 | [diff] [blame] | 74 | :ter[minal] [command] *:ter* *:terminal* |
Bram Moolenaar | 8a77306 | 2017-07-24 22:29:21 +0200 | [diff] [blame] | 75 | Open a new terminal window. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 76 | |
| 77 | If [command] is provided run it as a job and connect |
| 78 | the input and output to the terminal. |
| 79 | If [command] is not given the 'shell' option is used. |
| 80 | |
| 81 | A new buffer will be created, using [command] or |
Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 82 | 'shell' as the name, prefixed with a "!". If a buffer |
| 83 | by this name already exists a number is added in |
| 84 | parenthesis. E.g. if "gdb" exists the second terminal |
| 85 | buffer will use "!gdb (1)". |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 86 | |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 87 | When the buffer associated with the terminal is wiped out the job is killed, |
| 88 | similar to calling `job_stop(job, "kill")` |
| 89 | |
Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 90 | So long as the job is running: If the window is closed the buffer becomes |
| 91 | hidden. The command will not be stopped. The `:buffer` command can be used |
| 92 | to turn the current window into a terminal window. If there are unsaved |
| 93 | changes this fails, use ! to force, as usual. |
| 94 | |
| 95 | When the job has finished and no changes were made to the buffer: closing the |
| 96 | window will wipe out the buffer. |
| 97 | |
| 98 | Before changes can be made to a terminal buffer, the 'modifiable' option must |
| 99 | be set. This is only possible when the job has finished. At the first change |
| 100 | the buffer will become a normal buffer and the highlighting is removed. |
| 101 | You may want to change the buffer name with |:file| to be able to write, since |
| 102 | the buffer name will still be set to the command. |
| 103 | |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 104 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 105 | Resizing ~ |
| 106 | |
| 107 | The size of the terminal can be in one of three modes: |
| 108 | |
| 109 | 1. The 'termsize' option is empty: The terminal size follows the window size. |
| 110 | The minimal size is 2 screen lines with 10 cells. |
| 111 | |
| 112 | 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] | 113 | screen rows and "cols" is the minimal number of cells. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 114 | |
| 115 | 3. The 'termsize' option is "rowsXcols" (where the x is upper or lower case). |
| 116 | The terminal size is fixed to the specified number of screen lines and |
| 117 | cells. If the window is bigger there will be unused empty space. |
| 118 | |
| 119 | If the window is smaller than the terminal size, only part of the terminal can |
| 120 | be seen (the lower-left part). |
| 121 | |
| 122 | The |term_getsize()| function can be used to get the current size of the |
| 123 | terminal. |term_setsize()| can be used only when in the first or second mode, |
| 124 | not when 'termsize' is "rowsXcols". |
| 125 | |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 126 | |
Bram Moolenaar | 423802d | 2017-07-30 16:52:24 +0200 | [diff] [blame] | 127 | Terminal Normal mode ~ |
| 128 | *Terminal-mode* |
| 129 | When the job is running the contents of the terminal is under control of the |
| 130 | job. That includes the cursor position. The terminal contents can change at |
| 131 | any time. |
| 132 | |
| 133 | Use CTRL-W N (or 'termkey' N) to go to Terminal Normal mode. Now the contents |
| 134 | of the terminal window is under control of Vim, the job output is suspended. |
| 135 | *E946* |
| 136 | In this mode you can move the cursor around with the usual Vim commands, |
| 137 | Visually mark text, yank text, etc. But you cannot change the contents of the |
| 138 | buffer. The commands that would start insert mode, such as 'i' and 'a', |
| 139 | return control of the window to the job. Any pending output will now be |
| 140 | displayed. |
| 141 | |
| 142 | In Terminal mode the statusline and window title show "(Terminal)". If the |
| 143 | job ends while in Terminal mode this changes to "(Terminal-finished)". |
| 144 | |
| 145 | |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 146 | Unix ~ |
| 147 | |
| 148 | On Unix a pty is used to make it possible to run all kinds of commands. You |
| 149 | can even run Vim in the terminal! That's used for debugging, see below. |
| 150 | |
Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 151 | Environment variables are used to pass information to the running job: |
| 152 | TERM name of the terminal, 'term' |
| 153 | ROWS number of rows in the terminal initially |
| 154 | LINES same as ROWS |
| 155 | COLUMNS number of columns in the terminal initially |
| 156 | COLORS number of colors, 't_Co' (256*256*256 in the GUI) |
| 157 | VIM_SERVERNAME v:servername |
| 158 | |
| 159 | The |client-server| feature can be used to communicate with the Vim instance |
| 160 | where the job was started. This only works when v:servername is not empty. |
| 161 | If needed you can set it with: > |
| 162 | call remote_startserver('vim-server') |
| 163 | |
| 164 | In the job you can then do something like: > |
| 165 | vim --servername $VIM_SERVERNAME --remote +123 some_file.c |
| 166 | This will open the file "some_file.c" and put the cursor on line 123. |
| 167 | |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 168 | |
| 169 | MS-Windows ~ |
| 170 | |
Bram Moolenaar | 8a77306 | 2017-07-24 22:29:21 +0200 | [diff] [blame] | 171 | On MS-Windows winpty is used to make it possible to run all kind of commands. |
| 172 | Obviously, they must be commands that run in a terminal, not open their own |
| 173 | window. |
| 174 | |
| 175 | You need the following two files from winpty: |
| 176 | |
| 177 | winpty.dll |
| 178 | winpty-agent.exe |
| 179 | |
| 180 | You can download them from the following page: |
| 181 | |
| 182 | https://github.com/rprichard/winpty |
| 183 | |
| 184 | Just put the files somewhere in your PATH. |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 185 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 186 | ============================================================================== |
| 187 | 2. Remote testing *terminal-testing* |
| 188 | |
| 189 | Most Vim tests execute a script inside Vim. For some tests this does not |
| 190 | work, running the test interferes with the code being tested. To avoid this |
| 191 | Vim is executed in a terminal window. The test sends keystrokes to it and |
| 192 | inspects the resulting screen state. |
| 193 | |
| 194 | Functions ~ |
| 195 | |
| 196 | term_sendkeys() send keystrokes to a terminal |
| 197 | term_wait() wait for screen to be updated |
| 198 | term_scrape() inspect terminal screen |
| 199 | |
| 200 | |
| 201 | ============================================================================== |
| 202 | 3. Debugging *terminal-debug* |
| 203 | |
| 204 | The Terminal debugging plugin can be used to debug a program with gdb and view |
| 205 | the source code in a Vim window. For example: > |
| 206 | |
| 207 | :TermDebug vim |
| 208 | |
| 209 | This opens three windows: |
| 210 | - A terminal window in which "gdb vim" is executed. Here you can directly |
| 211 | interact with gdb. |
| 212 | - A terminal window for the executed program. When "run" is used in gdb the |
| 213 | program I/O will happen in this window, so that it does not interfere with |
| 214 | controlling gdb. |
| 215 | - A normal Vim window used to show the source code. When gdb jumps to a |
| 216 | source file location this window will display the code, if possible. Values |
| 217 | of variables can be inspected, breakpoints set and cleared, etc. |
| 218 | |
| 219 | This uses two terminal windows. To open the gdb window: > |
| 220 | :term gdb [arguments] |
Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 221 | 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] | 222 | |
| 223 | TODO |
| 224 | |
| 225 | |
| 226 | vim:tw=78:ts=8:ft=help:norl: |