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