terminal.txt 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. *terminal.txt* For Vim version 9.0. Last change: 2022 Nov 10
  2. VIM REFERENCE MANUAL by Bram Moolenaar
  3. Terminal window support *terminal* *terminal-window*
  4. The terminal feature is optional, use this to check if your Vim has it: >
  5. echo has('terminal')
  6. If the result is "1" you have it.
  7. 1. Basic use |terminal-use|
  8. Typing |terminal-typing|
  9. Size and color |terminal-size-color|
  10. Command syntax |:terminal|
  11. Resizing |terminal-resizing|
  12. Terminal Modes |Terminal-mode|
  13. Cursor style |terminal-cursor-style|
  14. Session |terminal-session|
  15. Special keys |terminal-special-keys|
  16. Unix |terminal-unix|
  17. MS-Windows |terminal-ms-windows|
  18. 2. Terminal functions |terminal-function-details|
  19. 3. Terminal communication |terminal-communication|
  20. Vim to job: term_sendkeys() |terminal-to-job|
  21. Job to Vim: JSON API |terminal-api|
  22. Using the client-server feature |terminal-client-server|
  23. 4. Remote testing |terminal-testing|
  24. 5. Diffing screen dumps |terminal-diff|
  25. Writing a screen dump test for Vim |terminal-dumptest|
  26. Creating a screen dump |terminal-screendump|
  27. Comparing screen dumps |terminal-diffscreendump|
  28. 6. Debugging |terminal-debug|
  29. Starting |termdebug-starting|
  30. Example session |termdebug-example|
  31. Stepping through code |termdebug-stepping|
  32. Inspecting variables |termdebug-variables|
  33. Other commands |termdebug-commands|
  34. Events |termdebug-events|
  35. Prompt mode |termdebug-prompt|
  36. Communication |termdebug-communication|
  37. Customizing |termdebug-customizing|
  38. {only available when compiled with the |+terminal| feature}
  39. The terminal feature requires the |+job| and |+channel| features.
  40. ==============================================================================
  41. 1. Basic use *terminal-use*
  42. This feature is for running a terminal emulator in a Vim window. A job can be
  43. started connected to the terminal emulator. For example, to run a shell: >
  44. :term bash
  45. Or to run build command: >
  46. :term make myprogram
  47. The job runs asynchronously from Vim, the window will be updated to show
  48. output from the job, also while editing in another window.
  49. Typing ~
  50. *terminal-typing*
  51. When the keyboard focus is in the terminal window, typed keys will be sent to
  52. the job. This uses a pty when possible. You can click outside of the
  53. terminal window to move keyboard focus elsewhere.
  54. *t_CTRL-W_CTRL-W* *t_CTRL-W_:*
  55. CTRL-W can be used to navigate between windows and other CTRL-W commands, e.g.:
  56. CTRL-W CTRL-W move focus to the next window
  57. CTRL-W : enter an Ex command
  58. See |CTRL-W| for more commands.
  59. Special in the terminal window: *t_CTRL-W_.* *t_CTRL-W_N*
  60. CTRL-W . send a CTRL-W to the job in the terminal
  61. CTRL-W CTRL-\ send a CTRL-\ to the job in the terminal
  62. CTRL-W N go to Terminal-Normal mode, see |Terminal-mode|
  63. CTRL-\ CTRL-N go to Terminal-Normal mode, see |Terminal-mode|
  64. CTRL-W " {reg} paste register {reg} *t_CTRL-W_quote*
  65. Also works with the = register to insert the result of
  66. evaluating an expression.
  67. CTRL-W CTRL-C ends the job, see below |t_CTRL-W_CTRL-C|
  68. CTRL-W gt go to next tabpage, same as `gt` *t_CTRL-W_gt*
  69. CTRL-W gT go to previous tabpage, same as `gT` *t_CTRL-W_gT*
  70. See option 'termwinkey' for specifying another key instead of CTRL-W that
  71. will work like CTRL-W. However, typing 'termwinkey' twice sends 'termwinkey'
  72. to the job. For example:
  73. 'termwinkey' CTRL-W move focus to the next window
  74. 'termwinkey' : enter an Ex command
  75. 'termwinkey' 'termwinkey' send 'termwinkey' to the job in the terminal
  76. 'termwinkey' . send 'termwinkey' to the job in the terminal
  77. 'termwinkey' CTRL-\ send a CTRL-\ to the job in the terminal
  78. 'termwinkey' N go to terminal Normal mode, see below
  79. 'termwinkey' CTRL-N same as CTRL-W N |t_CTRL-W_N|
  80. 'termwinkey' CTRL-C same as CTRL-W CTRL-C |t_CTRL-W_CTRL-C|
  81. *t_CTRL-\_CTRL-N*
  82. The special key combination CTRL-\ CTRL-N can be used to switch to Normal
  83. mode, just like this works in any other mode.
  84. *t_CTRL-W_CTRL-C*
  85. CTRL-W CTRL-C can be typed to forcefully end the job. On MS-Windows a
  86. CTRL-Break will also kill the job.
  87. If you type CTRL-C the effect depends on what the pty has been configured to
  88. do. For simple commands this causes a SIGINT to be sent to the job, which
  89. would end it. Other commands may ignore the SIGINT or handle the CTRL-C
  90. themselves (like Vim does).
  91. To change the keys you type use terminal mode mappings, see |:tmap|.
  92. These are defined like any mapping, but apply only when typing keys that are
  93. sent to the job running in the terminal. For example, to make F1 switch
  94. to Terminal-Normal mode: >
  95. tnoremap <F1> <C-W>N
  96. You can use Esc, but you need to make sure it won't cause other keys to
  97. break (cursor keys start with an Esc, so they may break), this probably only
  98. works in the GUI: >
  99. tnoremap <Esc> <C-W>N
  100. set notimeout ttimeout timeoutlen=100
  101. You can also create menus similar to terminal mode mappings, but you have to
  102. use |:tlmenu| instead of |:tmenu|.
  103. *options-in-terminal*
  104. After opening the terminal window and setting 'buftype' to "terminal" the
  105. |TerminalWinOpen| autocommand event is triggered. This makes it possible to set
  106. options specifically for the terminal window and buffer. Example: >
  107. au TerminalWinOpen * setlocal bufhidden=hide
  108. This only works properly if the terminal is not hidden.
  109. For both hidden and non-hidden terminals this works, both for buffer-local and
  110. window-local options: >
  111. au TerminalWinOpen,BufWinEnter * if &buftype == 'terminal'
  112. \ | setlocal bufhidden=hide colorcolumn=123
  113. \ | endif
  114. Note that for a hidden terminal the options are not set until the terminal is
  115. no longer hidden.
  116. There is also the |TerminalOpen| event. Keep in mind this may be triggered
  117. for a hidden terminal, then the current window and buffer are not that of the
  118. new terminal.
  119. You need to use <abuf>, which is set to the terminal buffer. Example: >
  120. au TerminalOpen * call setbufvar(expand('<abuf>')->str2nr(),
  121. \ '&termwinscroll', 1000)
  122. For a window-local option, you need to delay setting the option until the
  123. terminal window has been created (this only works for a hidden terminal): >
  124. au TerminalOpen * exe printf(
  125. \ 'au BufWinEnter <buffer=%d> ++once setlocal colorcolumn=%d',
  126. \ expand('<abuf>')->str2nr(), 123)
  127. For a non-hidden terminal use |TerminalWinOpen|.
  128. Mouse events (click and drag) are passed to the terminal. Mouse move events
  129. are only passed when Vim itself is receiving them. For a terminal that is
  130. when 'balloonevalterm' is enabled.
  131. Size and color ~
  132. *terminal-size-color*
  133. See option 'termwinsize' for controlling the size of the terminal window.
  134. (TODO: scrolling when the terminal is larger than the window)
  135. The job running in the terminal can change the colors. The default foreground
  136. and background colors are taken from Vim, the Normal highlight group.
  137. For a color terminal the 'background' option is used to decide whether the
  138. terminal window will start with a white or black background.
  139. To use a different color the Terminal highlight group can be used, for
  140. example: >
  141. hi Terminal ctermbg=lightgrey ctermfg=blue guibg=lightgrey guifg=blue
  142. Instead of Terminal another group can be specified with the "term_highlight"
  143. option for `term_start()`.
  144. *g:terminal_ansi_colors*
  145. In GUI mode or with 'termguicolors', the 16 ANSI colors used by default in new
  146. terminal windows may be configured using the variable
  147. `g:terminal_ansi_colors`, which should be a list of 16 color names or
  148. hexadecimal color codes, similar to those accepted by |highlight-guifg|. When
  149. not using GUI colors, the terminal window always uses the 16 ANSI colors of
  150. the underlying terminal.
  151. When using `term_start()` the colors can be set with the "ansi_colors" option.
  152. The |term_setansicolors()| function can be used to change the colors, and
  153. |term_getansicolors()| to get the currently used colors.
  154. Command syntax ~
  155. :[range]ter[minal] [options] [command] *:ter* *:terminal*
  156. Open a new terminal window.
  157. If [command] is provided run it as a job and connect
  158. the input and output to the terminal.
  159. If [command] is not given the 'shell' option is used.
  160. if [command] is NONE no job is started, the pty of the
  161. terminal can be used by a command like gdb.
  162. If [command] is missing the default behavior is to
  163. close the terminal when the shell exits. This can be
  164. changed with the ++noclose argument.
  165. If [command] is present the default behavior is to
  166. keep the terminal open in Terminal-Normal mode. This
  167. can be changed with the ++close argument.
  168. No Vim command can follow, any | is included in
  169. [command]. Use `:execute` if you must have a Vim
  170. command following in the same line.
  171. A new buffer will be created, using [command] or
  172. 'shell' as the name, prefixed with a "!". If a buffer
  173. by this name already exists a number is added in
  174. parentheses. E.g. if "gdb" exists the second terminal
  175. buffer will use "!gdb (1)".
  176. If [range] is given the specified lines are used as
  177. input for the job. It will not be possible to type
  178. keys in the terminal window. For MS-Windows see the
  179. ++eof argument below.
  180. *term++close* *term++open*
  181. Supported [options] are:
  182. ++close The terminal window will close
  183. automatically when the job terminates.
  184. |terminal-close|
  185. ++noclose The terminal window will NOT close
  186. automatically when the job terminates.
  187. ++open When the job terminates and no window
  188. shows it, a window will be opened.
  189. Note that this can be interruptive.
  190. The last of ++close, ++noclose and ++open
  191. matters and rules out earlier arguments.
  192. ++curwin Open the terminal in the current
  193. window, do not split the current
  194. window. Fails if the current buffer
  195. cannot be |abandon|ed.
  196. ++hidden Open the terminal in a hidden buffer,
  197. no window will be used.
  198. ++norestore Do not include this terminal window
  199. in a session file.
  200. ++shell Instead of executing {command}
  201. directly, use a shell, like with
  202. `:!command` *E279*
  203. {only works on Unix and MS-Windows}
  204. ++kill={how} When trying to close the terminal
  205. window kill the job with {how}. See
  206. |term_setkill()| for the values.
  207. ++rows={height} Use {height} for the terminal window
  208. height. If the terminal uses the full
  209. Vim height (no window above or below
  210. the terminal window) the command line
  211. height will be reduced as needed.
  212. ++cols={width} Use {width} for the terminal window
  213. width. If the terminal uses the full
  214. Vim width (no window left or right of
  215. the terminal window) this value is
  216. ignored.
  217. ++eof={text} When using [range]: text to send after
  218. the last line was written. Cannot
  219. contain white space. A CR is
  220. appended. For MS-Windows the default
  221. is to send CTRL-D.
  222. E.g. for a shell use "++eof=exit" and
  223. for Python "++eof=exit()". Special
  224. codes can be used like with `:map`,
  225. e.g. "<C-Z>" for CTRL-Z.
  226. ++type={pty} (MS-Windows only): Use {pty} as the
  227. virtual console. See 'termwintype'
  228. for the values.
  229. ++api={expr} Permit the function name starting with
  230. {expr} to be called as |terminal-api|
  231. function. If {expr} is empty then no
  232. function can be called.
  233. If you want to use more options use the |term_start()|
  234. function.
  235. If you want to split the window vertically, use: >
  236. :vertical terminal
  237. < Or short: >
  238. :vert ter
  239. When the buffer associated with the terminal is forcibly unloaded or wiped out
  240. the job is killed, similar to calling `job_stop(job, "kill")` .
  241. Closing the window normally results in |E947|. When a kill method was set
  242. with "++kill={how}" or |term_setkill()| then closing the window will use that
  243. way to kill or interrupt the job. For example: >
  244. :term ++kill=term tail -f /tmp/log
  245. So long as the job is running the window behaves like it contains a modified
  246. buffer. Trying to close the window with `CTRL-W :quit` fails. When using
  247. `CTRL-W :quit!` the job is ended. The text in the window is lost, the buffer
  248. is deleted. With `CTRL-W :bunload!` the buffer remains but will be empty.
  249. Trying to close the window with `CTRL-W :close` also fails. Using
  250. `CTRL-W :close!` will close the window and make the buffer hidden.
  251. You can use `CTRL-W :hide` to close the terminal window and make the buffer
  252. hidden, the job keeps running. The `:buffer` command can be used to turn the
  253. current window into a terminal window. If there are unsaved changes this
  254. fails, use ! to force, as usual.
  255. *terminal-close*
  256. When the terminal window is closed, e.g. when the shell exits and "++close"
  257. argument was used, and this is the last normal Vim window, then Vim will exit.
  258. This is like using |:quit| in a normal window. Help and preview windows are
  259. not counted.
  260. To have a background job run without a window, and open the window when it's
  261. done, use options like this: >
  262. :term ++hidden ++open make
  263. Note that the window will open at an unexpected moment, this will interrupt
  264. what you are doing.
  265. *E947* *E948*
  266. So long as the job is running, the buffer is considered modified and Vim
  267. cannot be quit easily, see |abandon|.
  268. When the job has finished and no changes were made to the buffer: closing the
  269. window will wipe out the buffer.
  270. Before changes can be made to a terminal buffer, the 'modifiable' option must
  271. be set. This is only possible when the job has finished. At the first change
  272. the buffer will become a normal buffer and the highlighting is removed.
  273. You may want to change the buffer name with |:file| to be able to write, since
  274. the buffer name will still be set to the command.
  275. Resizing ~
  276. *terminal-resizing*
  277. The size of the terminal can be in one of three modes:
  278. 1. The 'termwinsize' option is empty: The terminal size follows the window
  279. size. The minimal size is 2 screen lines with 10 cells.
  280. 2. The 'termwinsize' option is "rows*cols", where "rows" is the minimal number
  281. of screen rows and "cols" is the minimal number of cells.
  282. 3. The 'termwinsize' option is "rowsXcols" (where the x is upper or lower
  283. case). The terminal size is fixed to the specified number of screen lines
  284. and cells. If the window is bigger there will be unused empty space.
  285. If the window is smaller than the terminal size, only part of the terminal can
  286. be seen (the lower-left part).
  287. The |term_getsize()| function can be used to get the current size of the
  288. terminal. |term_setsize()| can be used only when in the first or second mode,
  289. not when 'termwinsize' is "rowsXcols".
  290. Terminal-Job and Terminal-Normal mode ~
  291. *Terminal-mode* *Terminal-Job*
  292. When the job is running the contents of the terminal is under control of the
  293. job. That includes the cursor position. Typed keys are sent to the job.
  294. The terminal contents can change at any time. This is called Terminal-Job
  295. mode.
  296. Use CTRL-W N (or 'termwinkey' N) to switch to Terminal-Normal mode. Now the
  297. contents of the terminal window is under control of Vim, the job output is
  298. suspended. CTRL-\ CTRL-N does the same.
  299. Terminal-Job mode is where |:tmap| mappings are applied. Keys sent by
  300. |term_sendkeys()| are not subject to tmap, but keys from |feedkeys()| are.
  301. It is not possible to enter Insert mode from Terminal-Job mode.
  302. *Terminal-Normal* *E946*
  303. In Terminal-Normal mode you can move the cursor around with the usual Vim
  304. commands, Visually mark text, yank text, etc. But you cannot change the
  305. contents of the buffer. The commands that would start insert mode, such as
  306. 'i' and 'a', return to Terminal-Job mode. The window will be updated to show
  307. the contents of the terminal. |:startinsert| is ineffective.
  308. In Terminal-Normal mode the statusline and window title show "(Terminal)". If
  309. the job ends while in Terminal-Normal mode this changes to
  310. "(Terminal-finished)".
  311. When the job outputs lines in the terminal, such that the contents scrolls off
  312. the top, those lines are remembered and can be seen in Terminal-Normal mode.
  313. The number of lines is limited by the 'termwinscroll' option. When going over
  314. this limit, the first 10% of the scrolled lines are deleted and are lost.
  315. Cursor style ~
  316. *terminal-cursor-style*
  317. By default the cursor in the terminal window uses a not blinking block. The
  318. normal xterm escape sequences can be used to change the blinking state and the
  319. shape. Once focus leaves the terminal window Vim will restore the original
  320. cursor.
  321. An exception is when xterm is started with the "-bc" argument, or another way
  322. that causes the cursor to blink. This actually means that the blinking flag
  323. is inverted. Since Vim cannot detect this, the terminal window cursor
  324. blinking will also be inverted.
  325. Session ~
  326. *terminal-session*
  327. A terminal window will be restored when using a session file, if possible and
  328. wanted.
  329. If "terminal" was removed from 'sessionoptions' then no terminal windows will
  330. be restored.
  331. If the job in the terminal was finished the window will not be restored.
  332. If the terminal can be restored, the command that was used to open it will be
  333. used again. To change this use the |term_setrestore()| function. This can
  334. also be used to not restore a specific terminal by setting the command to
  335. "NONE".
  336. Special keys ~
  337. *terminal-special-keys*
  338. Since the terminal emulator simulates an xterm, only escape sequences that
  339. both Vim and xterm recognize will be available in the terminal window. If you
  340. want to pass on other escape sequences to the job running in the terminal you
  341. need to set up forwarding. Example: >
  342. tmap <expr> <Esc>]b SendToTerm("\<Esc>]b")
  343. func SendToTerm(what)
  344. call term_sendkeys('', a:what)
  345. return ''
  346. endfunc
  347. Unix ~
  348. *terminal-unix*
  349. On Unix a pty is used to make it possible to run all kinds of commands. You
  350. can even run Vim in the terminal! That's used for debugging, see below.
  351. Environment variables are used to pass information to the running job:
  352. TERM the name of the terminal, from the 'term' option or
  353. $TERM in the GUI; falls back to "xterm" if it does not
  354. start with "xterm"
  355. ROWS number of rows in the terminal initially
  356. LINES same as ROWS
  357. COLUMNS number of columns in the terminal initially
  358. COLORS number of colors, 't_Co' (256*256*256 in the GUI)
  359. VIM_SERVERNAME v:servername
  360. VIM_TERMINAL v:version
  361. MS-Windows ~
  362. *terminal-ms-windows*
  363. On MS-Windows winpty is used to make it possible to run all kind of commands.
  364. Obviously, they must be commands that run in a terminal, not open their own
  365. window.
  366. You need the following two files from winpty:
  367. winpty.dll
  368. winpty-agent.exe
  369. You can download them from the following page:
  370. https://github.com/rprichard/winpty
  371. Just put the files somewhere in your PATH. You can set the 'winptydll' option
  372. to point to the right file, if needed. If you have both the 32-bit and 64-bit
  373. version, rename to winpty32.dll and winpty64.dll to match the way Vim was
  374. build.
  375. *ConPTY* *E982*
  376. On more recent versions of MS-Windows 10 (beginning with the "October 2018
  377. Update"), winpty is no longer required. On those versions, |:terminal| will use
  378. Windows' built-in support for hosting terminal applications, "ConPTY". When
  379. ConPTY is in use, there may be rendering artifacts regarding ambiguous-width
  380. characters. If you encounter any such issues, install "winpty". Until the
  381. ConPTY problems have been fixed "winpty" will be preferred.
  382. Environment variables are used to pass information to the running job:
  383. VIM_SERVERNAME v:servername
  384. ==============================================================================
  385. 2. Terminal functions *terminal-function-details*
  386. *term_dumpdiff()*
  387. term_dumpdiff({filename}, {filename} [, {options}])
  388. Open a new window displaying the difference between the two
  389. files. The files must have been created with
  390. |term_dumpwrite()|.
  391. Returns the buffer number or zero when the diff fails.
  392. Also see |terminal-diff|.
  393. NOTE: this does not work with double-width characters yet.
  394. The top part of the buffer contains the contents of the first
  395. file, the bottom part of the buffer contains the contents of
  396. the second file. The middle part shows the differences.
  397. The parts are separated by a line of equals.
  398. If the {options} argument is present, it must be a Dict with
  399. these possible members:
  400. "term_name" name to use for the buffer name, instead
  401. of the first file name.
  402. "term_rows" vertical size to use for the terminal,
  403. instead of using 'termwinsize', but
  404. respecting the minimal size
  405. "term_cols" horizontal size to use for the terminal,
  406. instead of using 'termwinsize', but
  407. respecting the minimal size
  408. "vertical" split the window vertically
  409. "curwin" use the current window, do not split the
  410. window; fails if the current buffer
  411. cannot be |abandon|ed
  412. "bufnr" do not create a new buffer, use the
  413. existing buffer "bufnr". This buffer
  414. must have been previously created with
  415. term_dumpdiff() or term_dumpload() and
  416. visible in a window.
  417. "norestore" do not add the terminal window to a
  418. session file
  419. Each character in the middle part indicates a difference. If
  420. there are multiple differences only the first in this list is
  421. used:
  422. X different character
  423. w different width
  424. f different foreground color
  425. b different background color
  426. a different attribute
  427. + missing position in first file
  428. - missing position in second file
  429. > cursor position in first file, not in second
  430. < cursor position in second file, not in first
  431. Using the "s" key the top and bottom parts are swapped. This
  432. makes it easy to spot a difference.
  433. Can also be used as a |method|: >
  434. GetFilename()->term_dumpdiff(otherfile)
  435. <
  436. *term_dumpload()*
  437. term_dumpload({filename} [, {options}])
  438. Open a new window displaying the contents of {filename}
  439. The file must have been created with |term_dumpwrite()|.
  440. Returns the buffer number or zero when it fails.
  441. Also see |terminal-diff|.
  442. For {options} see |term_dumpdiff()|.
  443. Can also be used as a |method|: >
  444. GetFilename()->term_dumpload()
  445. <
  446. *term_dumpwrite()*
  447. term_dumpwrite({buf}, {filename} [, {options}])
  448. Dump the contents of the terminal screen of {buf} in the file
  449. {filename}. This uses a format that can be used with
  450. |term_dumpload()| and |term_dumpdiff()|.
  451. If the job in the terminal already finished an error is given:
  452. *E958*
  453. If {filename} already exists an error is given: *E953*
  454. Also see |terminal-diff|.
  455. {options} is a dictionary with these optional entries:
  456. "rows" maximum number of rows to dump
  457. "columns" maximum number of columns to dump
  458. Can also be used as a |method|, the base is used for the file
  459. name: >
  460. GetFilename()->term_dumpwrite(bufnr)
  461. term_getaltscreen({buf}) *term_getaltscreen()*
  462. Returns 1 if the terminal of {buf} is using the alternate
  463. screen.
  464. {buf} is used as with |term_getsize()|.
  465. Can also be used as a |method|: >
  466. GetBufnr()->term_getaltscreen()
  467. term_getansicolors({buf}) *term_getansicolors()*
  468. Get the ANSI color palette in use by terminal {buf}.
  469. Returns a List of length 16 where each element is a String
  470. representing a color in hexadecimal "#rrggbb" format.
  471. Also see |term_setansicolors()| and |g:terminal_ansi_colors|.
  472. If neither was used returns the default colors.
  473. {buf} is used as with |term_getsize()|. If the buffer does not
  474. exist or is not a terminal window, an empty list is returned.
  475. Can also be used as a |method|: >
  476. GetBufnr()->term_getansicolors()
  477. < {only available when compiled with GUI enabled and/or the
  478. |+termguicolors| feature}
  479. term_getattr({attr}, {what}) *term_getattr()*
  480. Given {attr}, a value returned by term_scrape() in the "attr"
  481. item, return whether {what} is on. {what} can be one of:
  482. bold
  483. italic
  484. underline
  485. strike
  486. reverse
  487. Can also be used as a |method|: >
  488. GetAttr()->term_getattr()
  489. term_getcursor({buf}) *term_getcursor()*
  490. Get the cursor position of terminal {buf}. Returns a list with
  491. two numbers and a dictionary: [row, col, dict].
  492. "row" and "col" are one based, the first screen cell is row
  493. 1, column 1. This is the cursor position of the terminal
  494. itself, not of the Vim window.
  495. "dict" can have these members:
  496. "visible" one when the cursor is visible, zero when it
  497. is hidden.
  498. "blink" one when the cursor is blinking, zero when it
  499. is not blinking.
  500. "shape" 1 for a block cursor, 2 for underline and 3
  501. for a vertical bar.
  502. "color" color of the cursor, e.g. "green"
  503. {buf} must be the buffer number of a terminal window. If the
  504. buffer does not exist or is not a terminal window, an empty
  505. list is returned.
  506. Can also be used as a |method|: >
  507. GetBufnr()->term_getcursor()
  508. term_getjob({buf}) *term_getjob()*
  509. Get the Job associated with terminal window {buf}.
  510. {buf} is used as with |term_getsize()|.
  511. Returns |v:null| when there is no job.
  512. Can also be used as a |method|: >
  513. GetBufnr()->term_getjob()
  514. term_getline({buf}, {row}) *term_getline()*
  515. Get a line of text from the terminal window of {buf}.
  516. {buf} is used as with |term_getsize()|.
  517. The first line has {row} one. When {row} is "." the cursor
  518. line is used. When {row} is invalid an empty string is
  519. returned.
  520. To get attributes of each character use |term_scrape()|.
  521. Can also be used as a |method|: >
  522. GetBufnr()->term_getline(row)
  523. term_getscrolled({buf}) *term_getscrolled()*
  524. Return the number of lines that scrolled to above the top of
  525. terminal {buf}. This is the offset between the row number
  526. used for |term_getline()| and |getline()|, so that: >
  527. term_getline(buf, N)
  528. < is equal to: >
  529. getline(N + term_getscrolled(buf))
  530. < (if that line exists).
  531. {buf} is used as with |term_getsize()|.
  532. Can also be used as a |method|: >
  533. GetBufnr()->term_getscrolled()
  534. term_getsize({buf}) *term_getsize()*
  535. Get the size of terminal {buf}. Returns a list with two
  536. numbers: [rows, cols]. This is the size of the terminal, not
  537. the window containing the terminal.
  538. {buf} must be the buffer number of a terminal window. Use an
  539. empty string for the current buffer. If the buffer does not
  540. exist or is not a terminal window, an empty list is returned.
  541. Can also be used as a |method|: >
  542. GetBufnr()->term_getsize()
  543. term_getstatus({buf}) *term_getstatus()*
  544. Get the status of terminal {buf}. This returns a String with
  545. a comma-separated list of these items:
  546. running job is running
  547. finished job has finished
  548. normal in Terminal-Normal mode
  549. One of "running" or "finished" is always present.
  550. {buf} must be the buffer number of a terminal window. If the
  551. buffer does not exist or is not a terminal window, an empty
  552. string is returned.
  553. Can also be used as a |method|: >
  554. GetBufnr()->term_getstatus()
  555. term_gettitle({buf}) *term_gettitle()*
  556. Get the title of terminal {buf}. This is the title that the
  557. job in the terminal has set.
  558. {buf} must be the buffer number of a terminal window. If the
  559. buffer does not exist or is not a terminal window, an empty
  560. string is returned.
  561. Can also be used as a |method|: >
  562. GetBufnr()->term_gettitle()
  563. term_gettty({buf} [, {input}]) *term_gettty()*
  564. Get the name of the controlling terminal associated with
  565. terminal window {buf}. {buf} is used as with |term_getsize()|.
  566. When {input} is omitted or 0, return the name for writing
  567. (stdout). When {input} is 1 return the name for reading
  568. (stdin). On UNIX, both return same name.
  569. Can also be used as a |method|: >
  570. GetBufnr()->term_gettty()
  571. term_list() *term_list()*
  572. Return a list with the buffer numbers of all buffers for
  573. terminal windows.
  574. term_scrape({buf}, {row}) *term_scrape()*
  575. Get the contents of {row} of terminal screen of {buf}.
  576. For {buf} see |term_getsize()|.
  577. The first line has {row} one. When {row} is "." the cursor
  578. line is used. When {row} is invalid an empty string is
  579. returned.
  580. Return a List containing a Dict for each screen cell:
  581. "chars" character(s) at the cell
  582. "fg" foreground color as #rrggbb
  583. "bg" background color as #rrggbb
  584. "attr" attributes of the cell, use |term_getattr()|
  585. to get the individual flags
  586. "width" cell width: 1 or 2
  587. For a double-width cell there is one item, thus the list can
  588. be shorter than the width of the terminal.
  589. Can also be used as a |method|: >
  590. GetBufnr()->term_scrape(row)
  591. term_sendkeys({buf}, {keys}) *term_sendkeys()*
  592. Send keystrokes {keys} to terminal {buf}.
  593. {buf} is used as with |term_getsize()|.
  594. {keys} are translated as key sequences. For example, "\<c-x>"
  595. means the character CTRL-X.
  596. Can also be used as a |method|: >
  597. GetBufnr()->term_sendkeys(keys)
  598. term_setansicolors({buf}, {colors}) *term_setansicolors()*
  599. Set the ANSI color palette used by terminal {buf}.
  600. {colors} must be a List of 16 valid color names or hexadecimal
  601. color codes, like those accepted by |highlight-guifg|.
  602. Also see |term_getansicolors()| and |g:terminal_ansi_colors|.
  603. The colors normally are:
  604. 0 black
  605. 1 dark red
  606. 2 dark green
  607. 3 brown
  608. 4 dark blue
  609. 5 dark magenta
  610. 6 dark cyan
  611. 7 light grey
  612. 8 dark grey
  613. 9 red
  614. 10 green
  615. 11 yellow
  616. 12 blue
  617. 13 magenta
  618. 14 cyan
  619. 15 white
  620. These colors are used in the GUI and in the terminal when
  621. 'termguicolors' is set. When not using GUI colors (GUI mode
  622. or 'termguicolors'), the terminal window always uses the 16
  623. ANSI colors of the underlying terminal.
  624. Can also be used as a |method|: >
  625. GetBufnr()->term_setansicolors(colors)
  626. < {only available with GUI enabled and/or the |+termguicolors|
  627. feature}
  628. term_setapi({buf}, {expr}) *term_setapi()*
  629. Set the function name prefix to be used for the |terminal-api|
  630. function in terminal {buf}. For example: >
  631. :call term_setapi(buf, "Myapi_")
  632. :call term_setapi(buf, "")
  633. <
  634. The default is "Tapi_". When {expr} is an empty string then
  635. no |terminal-api| function can be used for {buf}.
  636. When used as a method the base is used for {buf}: >
  637. GetBufnr()->term_setapi({expr})
  638. term_setkill({buf}, {how}) *term_setkill()*
  639. When exiting Vim or trying to close the terminal window in
  640. another way, {how} defines whether the job in the terminal can
  641. be stopped.
  642. When {how} is empty (the default), the job will not be
  643. stopped, trying to exit will result in |E947|.
  644. Otherwise, {how} specifies what signal to send to the job.
  645. See |job_stop()| for the values.
  646. After sending the signal Vim will wait for up to a second to
  647. check that the job actually stopped.
  648. Can also be used as a |method|: >
  649. GetBufnr()->term_setkill(how)
  650. term_setrestore({buf}, {command}) *term_setrestore()*
  651. Set the command to write in a session file to restore the job
  652. in this terminal. The line written in the session file is: >
  653. terminal ++curwin ++cols=%d ++rows=%d {command}
  654. < Make sure to escape the command properly.
  655. Use an empty {command} to run 'shell'.
  656. Use "NONE" to not restore this window.
  657. Can also be used as a |method|: >
  658. GetBufnr()->term_setrestore(command)
  659. term_setsize({buf}, {rows}, {cols}) *term_setsize()* *E955*
  660. Set the size of terminal {buf}. The size of the window
  661. containing the terminal will also be adjusted, if possible.
  662. If {rows} or {cols} is zero or negative, that dimension is not
  663. changed.
  664. {buf} must be the buffer number of a terminal window. Use an
  665. empty string for the current buffer. If the buffer does not
  666. exist or is not a terminal window, an error is given.
  667. Can also be used as a |method|: >
  668. GetBufnr()->term_setsize(rows, cols)
  669. term_start({cmd} [, {options}]) *term_start()*
  670. Open a terminal window and run {cmd} in it.
  671. {cmd} can be a string or a List, like with |job_start()|. The
  672. string "NONE" can be used to open a terminal window without
  673. starting a job, the pty of the terminal can be used by a
  674. command like gdb.
  675. Returns the buffer number of the terminal window. If {cmd}
  676. cannot be executed the window does open and shows an error
  677. message.
  678. If opening the window fails zero is returned.
  679. {options} are similar to what is used for |job_start()|, see
  680. |job-options|. However, not all options can be used. These
  681. are supported:
  682. all timeout options
  683. "stoponexit", "cwd", "env"
  684. "callback", "out_cb", "err_cb", "exit_cb", "close_cb"
  685. "in_io", "in_top", "in_bot", "in_name", "in_buf"
  686. "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
  687. "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
  688. However, at least one of stdin, stdout or stderr must be
  689. connected to the terminal. When I/O is connected to the
  690. terminal then the callback function for that part is not used.
  691. There are extra options:
  692. "term_name" name to use for the buffer name, instead
  693. of the command name.
  694. "term_rows" vertical size to use for the terminal,
  695. instead of using 'termwinsize'; valid
  696. range is from zero to 1000
  697. "term_cols" horizontal size to use for the terminal,
  698. instead of using 'termwinsize'
  699. "vertical" split the window vertically; note that
  700. other window position can be defined with
  701. command modifiers, such as |:belowright|.
  702. "curwin" use the current window, do not split the
  703. window; fails if the current buffer
  704. cannot be |abandon|ed
  705. "hidden" do not open a window
  706. "norestore" do not add the terminal window to a
  707. session file
  708. "term_kill" what to do when trying to close the
  709. terminal window, see |term_setkill()|
  710. "term_finish" What to do when the job is finished:
  711. "close": close any windows
  712. "open": open window if needed
  713. Note that "open" can be interruptive.
  714. See |term++close| and |term++open|.
  715. "term_opencmd" command to use for opening the window when
  716. "open" is used for "term_finish"; must
  717. have "%d" where the buffer number goes,
  718. e.g. "10split|buffer %d"; when not
  719. specified "botright sbuf %d" is used
  720. "term_highlight" highlight group to use instead of
  721. "Terminal"
  722. "eof_chars" Text to send after all buffer lines were
  723. written to the terminal. When not set
  724. CTRL-D is used on MS-Windows. For Python
  725. use CTRL-Z or "exit()". For a shell use
  726. "exit". A CR is always added.
  727. "ansi_colors" A list of 16 color names or hex codes
  728. defining the ANSI palette used in GUI
  729. color modes. See |g:terminal_ansi_colors|.
  730. "tty_type" (MS-Windows only): Specify which pty to
  731. use. See 'termwintype' for the values.
  732. "term_api" function name prefix for the
  733. |terminal-api| function. See
  734. |term_setapi()|.
  735. Can also be used as a |method|: >
  736. GetCommand()->term_start()
  737. term_wait({buf} [, {time}]) *term_wait()*
  738. Wait for pending updates of {buf} to be handled.
  739. {buf} is used as with |term_getsize()|.
  740. {time} is how long to wait for updates to arrive in msec. If
  741. not set then 10 msec will be used.
  742. Can also be used as a |method|: >
  743. GetBufnr()->term_wait()
  744. ==============================================================================
  745. 3. Terminal communication *terminal-communication*
  746. There are several ways to communicate with the job running in a terminal:
  747. - Use |term_sendkeys()| to send text and escape sequences from Vim to the job.
  748. - Use the JSON API to send encoded commands from the job to Vim.
  749. - Use the |client-server| mechanism. This works on machines with an X server
  750. and on MS-Windows.
  751. Vim to job: term_sendkeys() ~
  752. *terminal-to-job*
  753. This allows for remote controlling the job running in the terminal. It is a
  754. one-way mechanism. The job can update the display to signal back to Vim.
  755. For example, if a shell is running in a terminal, you can do: >
  756. call term_sendkeys(buf, "ls *.java\<CR>")
  757. This requires for the job to be in the right state where it will do the right
  758. thing when receiving the keys. For the above example, the shell must be
  759. waiting for a command to be typed.
  760. For a job that was written for the purpose, you can use the JSON API escape
  761. sequence in the other direction. E.g.: >
  762. call term_sendkeys(buf, "\<Esc>]51;["response"]\x07")
  763. Job to Vim: JSON API ~
  764. *terminal-api*
  765. The job can send JSON to Vim, using a special escape sequence. The JSON
  766. encodes a command that Vim understands. Example of such a message: >
  767. <Esc>]51;["drop", "README.md"]<07>
  768. The body is always a list, making it easy to find the end: ]<07>.
  769. The <Esc>]51;msg<07> sequence is reserved by xterm for "Emacs shell", which is
  770. similar to what we are doing here.
  771. Currently supported commands:
  772. call {funcname} {argument}
  773. Call a user defined function with {argument}.
  774. The function is called with two arguments: the buffer number
  775. of the terminal and {argument}, the decoded JSON argument.
  776. By default, the function name must start with "Tapi_" to avoid
  777. accidentally calling a function not meant to be used for the
  778. terminal API. This can be changed with |term_setapi()|.
  779. The user function should sanity check the argument.
  780. The function can use |term_sendkeys()| to send back a reply.
  781. Example in JSON: >
  782. ["call", "Tapi_Impression", ["play", 14]]
  783. < Calls a function defined like this: >
  784. function Tapi_Impression(bufnum, arglist)
  785. if len(a:arglist) == 2
  786. echomsg "impression " .. a:arglist[0]
  787. echomsg "count " .. a:arglist[1]
  788. endif
  789. endfunc
  790. < Output from `:echo` may be erased by a redraw, use `:echomsg`
  791. to be able to see it with `:messages`.
  792. drop {filename} [options]
  793. Let Vim open a file, like the `:drop` command. If {filename}
  794. is already open in a window, switch to that window. Otherwise
  795. open a new window to edit {filename}.
  796. Note that both the job and Vim may change the current
  797. directory, thus it's best to use the full path.
  798. [options] is only used when opening a new window. If present,
  799. it must be a Dict. Similarly to |++opt|, these entries are
  800. recognized:
  801. "ff" file format: "dos", "mac" or "unix"
  802. "fileformat" idem
  803. "enc" overrides 'fileencoding'
  804. "encoding" idem
  805. "bin" sets 'binary'
  806. "binary" idem
  807. "nobin" resets 'binary'
  808. "nobinary" idem
  809. "bad" specifies behavior for bad characters, see
  810. |++bad|
  811. Example in JSON: >
  812. ["drop", "path/file.txt", {"ff": "dos"}]
  813. A trick to have Vim send this escape sequence: >
  814. exe "set t_ts=\<Esc>]51; t_fs=\x07"
  815. let &titlestring = '["call","Tapi_TryThis",["hello",123]]'
  816. redraw
  817. set t_ts& t_fs&
  818. Rationale: Why not allow for any command or expression? Because that might
  819. create a security problem.
  820. *terminal-autoshelldir*
  821. This can be used to pass the current directory from a shell to Vim.
  822. Put this in your .vimrc: >
  823. def g:Tapi_lcd(_, path: string)
  824. if isdirectory(path)
  825. execute 'silent lcd ' .. fnameescape(path)
  826. endif
  827. enddef
  828. <
  829. And, in a bash init file: >
  830. if [[ -n "$VIM_TERMINAL" ]]; then
  831. PROMPT_COMMAND='_vim_sync_PWD'
  832. function _vim_sync_PWD() {
  833. printf '\033]51;["call", "Tapi_lcd", "%q"]\007' "$PWD"
  834. }
  835. fi
  836. <
  837. Or, for zsh: >
  838. if [[ -n "$VIM_TERMINAL" ]]; then
  839. autoload -Uz add-zsh-hook
  840. add-zsh-hook -Uz chpwd _vim_sync_PWD
  841. function _vim_sync_PWD() {
  842. printf '\033]51;["call", "Tapi_lcd", "%q"]\007' "$PWD"
  843. }
  844. fi
  845. <
  846. Or, for fish: >
  847. if test -n "$VIM_TERMINAL"
  848. function _vim_sync_PWD --on-variable=PWD
  849. printf '\033]51;["call", "Tapi_lcd", "%s"]\007' "$PWD"
  850. end
  851. end
  852. Using the client-server feature ~
  853. *terminal-client-server*
  854. This only works when v:servername is not empty. If needed you can set it,
  855. before opening the terminal, with: >
  856. call remote_startserver('vim-server')
  857. $VIM_SERVERNAME is set in the terminal to pass on the server name.
  858. In the job you can then do something like: >
  859. vim --servername $VIM_SERVERNAME --remote +123 some_file.c
  860. This will open the file "some_file.c" and put the cursor on line 123.
  861. ==============================================================================
  862. 4. Remote testing *terminal-testing*
  863. Most Vim tests execute a script inside Vim. For some tests this does not
  864. work, running the test interferes with the code being tested. To avoid this
  865. Vim is executed in a terminal window. The test sends keystrokes to it and
  866. inspects the resulting screen state.
  867. Functions ~
  868. |term_sendkeys()| send keystrokes to a terminal (not subject to tmap)
  869. |term_wait()| wait for screen to be updated
  870. |term_scrape()| inspect terminal screen
  871. ==============================================================================
  872. 5. Diffing screen dumps *terminal-diff*
  873. In some cases it can be bothersome to test that Vim displays the right
  874. characters on the screen. E.g. with syntax highlighting. To make this
  875. simpler it is possible to take a screen dump of a terminal and compare it to
  876. an expected screen dump.
  877. Vim uses the window size, text, color and other attributes as displayed. The
  878. Vim screen size, font and other properties do not matter. Therefore this
  879. mechanism is portable across systems. A conventional screenshot would reflect
  880. all differences, including font size and family.
  881. Writing a screen dump test for Vim ~
  882. *terminal-dumptest*
  883. For an example see the Test_syntax_c() function in
  884. src/testdir/test_syntax.vim. The main parts are:
  885. - Write a file you want to test with. This is useful for testing syntax
  886. highlighting. You can also start Vim with an empty buffer.
  887. - Run Vim in a terminal with a specific size. The default is 20 lines of 75
  888. characters. This makes sure the dump is always this size. The function
  889. RunVimInTerminal() takes care of this. Pass it the arguments for the Vim
  890. command.
  891. - Send any commands to Vim using |term_sendkeys()|. For example: >
  892. call term_sendkeys(buf, ":echo &lines &columns\<CR>")
  893. - Check that the screen is now in the expected state, using
  894. VerifyScreenDump(). This expects the reference screen dump to be in the
  895. src/testdir/dumps/ directory. Pass the name without ".dump". It is
  896. recommended to use the name of the test function and a sequence number, so
  897. that we know what test is using the file.
  898. - Repeat sending commands and checking the state.
  899. - Finally stop Vim by calling StopVimInTerminal().
  900. The first time you do this you won't have a screen dump yet. Create an empty
  901. file for now, e.g.: >
  902. touch src/testdir/dumps/Test_function_name_01.dump
  903. The test will then fail, giving you the command to compare the reference dump
  904. and the failed dump, e.g.: >
  905. call term_dumpdiff("failed/Test_func.dump", "dumps/Test_func.dump")
  906. Use this command in Vim, with the current directory set to src/testdir.
  907. Once you are satisfied with the test, move the failed dump in place of the
  908. reference: >
  909. :!mv failed/Test_func.dump dumps/Test_func.dump
  910. Creating a screen dump ~
  911. *terminal-screendump*
  912. To create the screen dump, run Vim (or any other program) in a terminal and
  913. make it show the desired state. Then use the |term_dumpwrite()| function to
  914. create a screen dump file. For example: >
  915. :call term_dumpwrite(77, "mysyntax.dump")
  916. Here "77" is the buffer number of the terminal. Use `:ls!` to see it.
  917. You can view the screen dump with |term_dumpload()|: >
  918. :call term_dumpload("mysyntax.dump")
  919. To verify that Vim still shows exactly the same screen, run Vim again with
  920. exactly the same way to show the desired state. Then create a screen dump
  921. again, using a different file name: >
  922. :call term_dumpwrite(88, "test.dump")
  923. To assert that the files are exactly the same use |assert_equalfile()|: >
  924. call assert_equalfile("mysyntax.dump", "test.dump")
  925. If there are differences then v:errors will contain the error message.
  926. Comparing screen dumps ~
  927. *terminal-diffscreendump*
  928. |assert_equalfile()| does not make it easy to see what is different.
  929. To spot the problem use |term_dumpdiff()|: >
  930. call term_dumpdiff("mysyntax.dump", "test.dump")
  931. This will open a window consisting of three parts:
  932. 1. The contents of the first dump
  933. 2. The difference between the first and second dump
  934. 3. The contents of the second dump
  935. You can usually see what differs in the second part. Use the 'ruler' to
  936. relate it to the position in the first or second dump. Letters indicate the
  937. kind of difference:
  938. X different character
  939. > cursor in first but not in second
  940. < cursor in second but not in first
  941. w character width differs (single vs double width)
  942. f foreground color differs
  943. b background color differs
  944. a attribute differs (bold, underline, reverse, etc.)
  945. ? character missing in both
  946. + character missing in first
  947. - character missing in second
  948. Alternatively, press "s" to swap the first and second dump. Do this several
  949. times so that you can spot the difference in the context of the text.
  950. ==============================================================================
  951. 6. Debugging *terminal-debug* *terminal-debugger*
  952. The Terminal debugging plugin can be used to debug a program with gdb and view
  953. the source code in a Vim window. Since this is completely contained inside
  954. Vim this also works remotely over an ssh connection.
  955. When the |+terminal| feature is missing, the plugin will use the "prompt"
  956. buffer type, if possible. The running program will then use a newly opened
  957. terminal window. See |termdebug-prompt| below for details.
  958. Starting ~
  959. *termdebug-starting*
  960. Load the plugin with this command: >
  961. packadd termdebug
  962. < *:Termdebug*
  963. To start debugging use `:Termdebug` or `:TermdebugCommand` followed by the
  964. command name, for example: >
  965. :Termdebug vim
  966. This opens two windows:
  967. gdb window A terminal window in which "gdb vim" is executed. Here you
  968. can directly interact with gdb. The buffer name is "!gdb".
  969. program window A terminal window for the executed program. When "run" is
  970. used in gdb the program I/O will happen in this window, so
  971. that it does not interfere with controlling gdb. The buffer
  972. name is "debugged program".
  973. The current window is used to show the source code. When gdb pauses the
  974. source file location will be displayed, if possible. A sign is used to
  975. highlight the current position, using highlight group debugPC.
  976. If the buffer in the current window is modified, another window will be opened
  977. to display the current gdb position. You can use `:Winbar` to add a window
  978. toolbar there.
  979. Focus the terminal of the executed program to interact with it. This works
  980. the same as any command running in a terminal window.
  981. When the debugger ends, typically by typing "quit" in the gdb window, the two
  982. opened windows are closed.
  983. Only one debugger can be active at a time.
  984. *:TermdebugCommand*
  985. If you want to give specific commands to the command being debugged, you can
  986. use the `:TermdebugCommand` command followed by the command name and
  987. additional parameters. >
  988. :TermdebugCommand vim --clean -c ':set nu'
  989. Both the `:Termdebug` and `:TermdebugCommand` support an optional "!" bang
  990. argument to start the command right away, without pausing at the gdb window
  991. (and cursor will be in the debugged window). For example: >
  992. :TermdebugCommand! vim --clean
  993. To attach gdb to an already running executable or use a core file, pass extra
  994. arguments. E.g.: >
  995. :Termdebug vim core
  996. :Termdebug vim 98343
  997. If no argument is given, you'll end up in a gdb window, in which you need to
  998. specify which command to run using e.g. the gdb `file` command.
  999. Example session ~
  1000. *termdebug-example*
  1001. Start in the Vim "src" directory and build Vim: >
  1002. % make
  1003. Make sure that debug symbols are present, usually that means that $CFLAGS
  1004. includes "-g".
  1005. Start Vim: >
  1006. % ./vim
  1007. Load the termdebug plugin and start debugging Vim: >
  1008. :packadd termdebug
  1009. :Termdebug vim
  1010. You should now have three windows:
  1011. source - where you started, has a window toolbar with buttons
  1012. gdb - you can type gdb commands here
  1013. program - the executed program will use this window
  1014. You can use CTRL-W CTRL-W or the mouse to move focus between windows.
  1015. Put focus on the gdb window and type: >
  1016. break ex_help
  1017. run
  1018. Vim will start running in the program window. Put focus there and type: >
  1019. :help gui
  1020. Gdb will run into the ex_help breakpoint. The source window now shows the
  1021. ex_cmds.c file. A red "1 " marker will appear in the signcolumn where the
  1022. breakpoint was set. The line where the debugger stopped is highlighted. You
  1023. can now step through the program. Let's use the mouse: click on the "Next"
  1024. button in the window toolbar. You will see the highlighting move as the
  1025. debugger executes a line of source code.
  1026. Click "Next" a few times until the for loop is highlighted. Put the cursor on
  1027. the end of "eap->arg", then click "Eval" in the toolbar. You will see this
  1028. displayed:
  1029. "eap->arg": 0x555555e68855 "gui" ~
  1030. This way you can inspect the value of local variables. You can also focus the
  1031. gdb window and use a "print" command, e.g.: >
  1032. print *eap
  1033. If mouse pointer movements are working, Vim will also show a balloon when the
  1034. mouse rests on text that can be evaluated by gdb.
  1035. Now go back to the source window and put the cursor on the first line after
  1036. the for loop, then type: >
  1037. :Break
  1038. You will see a ">>" marker appear, this indicates the new breakpoint. Now
  1039. click "Cont" in the toolbar and the code until the breakpoint will be
  1040. executed.
  1041. You can type more advanced commands in the gdb window. For example, type: >
  1042. watch curbuf
  1043. Now click "Cont" in the toolbar (or type "cont" in the gdb window). Execution
  1044. will now continue until the value of "curbuf" changes, which is in do_ecmd().
  1045. To remove this watchpoint again type in the gdb window: >
  1046. delete 3
  1047. You can see the stack by typing in the gdb window: >
  1048. where
  1049. Move through the stack frames, e.g. with: >
  1050. frame 3
  1051. The source window will show the code, at the point where the call was made to
  1052. a deeper level.
  1053. Stepping through code ~
  1054. *termdebug-stepping*
  1055. Put focus on the gdb window to type commands there. Some common ones are:
  1056. - CTRL-C interrupt the program
  1057. - next execute the current line and stop at the next line
  1058. - step execute the current line and stop at the next statement,
  1059. entering functions
  1060. - until execute until past the current cursor line or past a specified
  1061. position or the current stack frame returns
  1062. - finish execute until leaving the current function
  1063. - where show the stack
  1064. - frame N go to the Nth stack frame
  1065. - continue continue execution
  1066. *:Run* *:Arguments*
  1067. In the window showing the source code these commands can be used to control
  1068. gdb:
  1069. `:Run` [args] run the program with [args] or the previous arguments
  1070. `:Arguments` {args} set arguments for the next `:Run`
  1071. *:Break* set a breakpoint at the cursor position
  1072. :Break {position}
  1073. set a breakpoint at the specified position
  1074. *:Clear* delete the breakpoint at the cursor position
  1075. *:Step* execute the gdb "step" command
  1076. *:Over* execute the gdb "next" command (`:Next` is a Vim command)
  1077. *:Until* execute the gdb "until" command
  1078. *:Finish* execute the gdb "finish" command
  1079. *:Continue* execute the gdb "continue" command
  1080. *:Stop* interrupt the program
  1081. If 'mouse' is set the plugin adds a window toolbar with these entries:
  1082. Step `:Step`
  1083. Next `:Over`
  1084. Finish `:Finish`
  1085. Cont `:Continue`
  1086. Stop `:Stop`
  1087. Eval `:Evaluate`
  1088. This way you can use the mouse to perform the most common commands. You need
  1089. to have the 'mouse' option set to enable mouse clicks.
  1090. See |termdebug_winbar| for configuring this toolbar.
  1091. *:Winbar*
  1092. You can add the window toolbar in other windows you open with: >
  1093. :Winbar
  1094. If gdb stops at a source line and there is no window currently showing the
  1095. source code, a new window will be created for the source code. This also
  1096. happens if the buffer in the source code window has been modified and can't be
  1097. abandoned.
  1098. Gdb gives each breakpoint a number. In Vim the number shows up in the sign
  1099. column, with a red background. You can use these gdb commands:
  1100. - info break list breakpoints
  1101. - delete N delete breakpoint N
  1102. You can also use the `:Clear` command if the cursor is in the line with the
  1103. breakpoint, or use the "Clear breakpoint" right-click menu entry.
  1104. Inspecting variables ~
  1105. *termdebug-variables* *:Evaluate*
  1106. `:Evaluate` evaluate the expression under the cursor
  1107. `K` same (see |termdebug_map_K| to disable)
  1108. `:Evaluate` {expr} evaluate {expr}
  1109. `:'<,'>Evaluate` evaluate the Visually selected text
  1110. This is similar to using "print" in the gdb window.
  1111. You can usually shorten `:Evaluate` to `:Ev`.
  1112. Other commands ~
  1113. *termdebug-commands*
  1114. *:Gdb* jump to the gdb window
  1115. *:Program* jump to the window with the running program
  1116. *:Source* jump to the window with the source code, create it if there
  1117. isn't one
  1118. *:Asm* jump to the window with the disassembly, create it if there
  1119. isn't one
  1120. Events ~
  1121. *termdebug-events*
  1122. Four autocommands can be used: >
  1123. au User TermdebugStartPre echomsg 'debugging starting'
  1124. au User TermdebugStartPost echomsg 'debugging started'
  1125. au User TermdebugStopPre echomsg 'debugging stopping'
  1126. au User TermdebugStopPost echomsg 'debugging stopped'
  1127. <
  1128. *TermdebugStartPre*
  1129. TermdebugStartPre Before starting debugging.
  1130. Not triggered if the debugger is already
  1131. running or the debugger command cannot be
  1132. executed.
  1133. *TermdebugStartPost*
  1134. TermdebugStartPost After debugging has initialized.
  1135. If a "!" bang is passed to `:Termdebug` or
  1136. `:TermdebugCommand` the event is triggered
  1137. before running the provided command in gdb.
  1138. *TermdebugStopPre*
  1139. TermdebugStopPre Before debugging ends, when gdb is terminated,
  1140. most likely after issuing a "quit" command in
  1141. the gdb window.
  1142. *TermdebugStopPost*
  1143. TermdebugStopPost After debugging has ended, gdb-related windows
  1144. are closed, debug buffers wiped out and
  1145. the state before the debugging was restored.
  1146. Customizing ~
  1147. *termdebug-customizing* *g:termdebug_config*
  1148. In the past several global variables were used for configuration. These are
  1149. deprecated and using the g:termdebug_config dictionary is preferred. When
  1150. g:termdebug_config exists the other global variables will NOT be used.
  1151. The recommended way is to start with an empty dictionary: >
  1152. let g:termdebug_config = {}
  1153. Then you can add entries to the dictionary as mentioned below. The
  1154. deprecated global variable names are mentioned for completeness. If you are
  1155. switching over to using g:termdebug_config you can find the old variable name
  1156. and take over the value, then delete the deprecated variable.
  1157. Prompt mode ~
  1158. *termdebug-prompt*
  1159. When the |+terminal| feature is not supported and on MS-Windows, gdb will run
  1160. in a buffer with 'buftype' set to "prompt". This works slightly differently:
  1161. - The gdb window will be in Insert mode while typing commands. Go to Normal
  1162. mode with <Esc>, then you can move around in the buffer, copy/paste, etc.
  1163. Go back to editing the gdb command with any command that starts Insert mode,
  1164. such as `a` or `i`.
  1165. - The program being debugged will run in a separate window. On MS-Windows
  1166. this is a new console window. On Unix, if the |+terminal| feature is
  1167. available a Terminal window will be opened to run the debugged program in.
  1168. *termdebug_use_prompt*
  1169. Prompt mode can be used even when the |+terminal| feature is present with: >
  1170. let g:termdebug_config['use_prompt'] = 1
  1171. If there is no g:termdebug_config you can use: >
  1172. let g:termdebug_use_prompt = 1
  1173. <
  1174. *termdebug_map_K*
  1175. The K key is normally mapped to :Evaluate. If you do not want this use: >
  1176. let g:termdebug_config['map_K'] = 0
  1177. If there is no g:termdebug_config you can use: >
  1178. let g:termdebug_map_K = 0
  1179. <
  1180. *termdebug_disasm_window*
  1181. If you want the Asm window shown by default, set the flag to 1.
  1182. the "disasm_window_height" entry can be used to set the window height: >
  1183. let g:termdebug_config['disasm_window'] = 1
  1184. let g:termdebug_config['disasm_window_height'] = 15
  1185. If there is no g:termdebug_config you can use: >
  1186. let g:termdebug_disasm_window = 15
  1187. Any value greater than 1 will set the Asm window height to that value.
  1188. Communication ~
  1189. *termdebug-communication*
  1190. There is another, hidden, buffer, which is used for Vim to communicate with
  1191. gdb. The buffer name is "gdb communication". Do not delete this buffer, it
  1192. will break the debugger.
  1193. Gdb has some weird behavior, the plugin does its best to work around that.
  1194. For example, after typing "continue" in the gdb window a CTRL-C can be used to
  1195. interrupt the running program. But after using the MI command
  1196. "-exec-continue" pressing CTRL-C does not interrupt. Therefore you will see
  1197. "continue" being used for the `:Continue` command, instead of using the
  1198. communication channel.
  1199. GDB command ~
  1200. *g:termdebugger*
  1201. To change the name of the gdb command, set "debugger" entry in
  1202. g:termdebug_config or the "g:termdebugger" variable before invoking
  1203. `:Termdebug`: >
  1204. let g:termdebug_config['command'] = "mygdb"
  1205. If there is no g:termdebug_config you can use: >
  1206. let g:termdebugger = "mygdb"
  1207. If the command needs an argument use a List: >
  1208. let g:termdebug_config['command'] = ['rr', 'replay', '--']
  1209. If there is no g:termdebug_config you can use: >
  1210. let g:termdebugger = ['rr', 'replay', '--']
  1211. Several arguments will be added to make gdb work well for the debugger.
  1212. If you want to modify them, add a function to filter the argument list: >
  1213. let g:termdebug_config['command_filter'] = MyDebugFilter
  1214. If you do not want the arguments to be added, but you do need to set the
  1215. "pty", use a function to add the necessary arguments: >
  1216. let g:termdebug_config['command_add_args'] = MyAddArguments
  1217. The function will be called with the list of arguments so far, and a second
  1218. argument that is the name of the pty.
  1219. *gdb-version*
  1220. Only debuggers fully compatible with gdb will work. Vim uses the GDB/MI
  1221. interface. The "new-ui" command requires gdb version 7.12 or later. if you
  1222. get this error:
  1223. Undefined command: "new-ui". Try "help".~
  1224. Then your gdb is too old.
  1225. Colors ~
  1226. *hl-debugPC* *hl-debugBreakpoint*
  1227. The color of the signs can be adjusted with these highlight groups:
  1228. - debugPC the current position
  1229. - debugBreakpoint a breakpoint
  1230. The defaults are, when 'background' is "light":
  1231. hi debugPC term=reverse ctermbg=lightblue guibg=lightblue
  1232. hi debugBreakpoint term=reverse ctermbg=red guibg=red
  1233. When 'background' is "dark":
  1234. hi debugPC term=reverse ctermbg=darkblue guibg=darkblue
  1235. hi debugBreakpoint term=reverse ctermbg=red guibg=red
  1236. Shortcuts ~
  1237. *termdebug_shortcuts*
  1238. You can define your own shortcuts (mappings) to control gdb, that can work in
  1239. any window, using the TermDebugSendCommand() function. Example: >
  1240. map ,w :call TermDebugSendCommand('where')<CR>
  1241. The argument is the gdb command.
  1242. Popup menu ~
  1243. *termdebug_popup*
  1244. By default the Termdebug plugin sets 'mousemodel' to "popup_setpos" and adds
  1245. these entries to the popup menu:
  1246. Set breakpoint `:Break`
  1247. Clear breakpoint `:Clear`
  1248. Evaluate `:Evaluate`
  1249. If you don't want this then disable it with: >
  1250. let g:termdebug_config['popup'] = 0
  1251. If there is no g:termdebug_config you can use: >
  1252. let g:termdebug_popup = 0
  1253. Window toolbar ~
  1254. *termdebug_winbar*
  1255. By default the Termdebug plugin creates a window toolbar if the mouse is
  1256. enabled (see |:Winbar|). If you don't want this then disable it with: >
  1257. let g:termdebug_config['winbar'] = 0
  1258. Vim window width ~
  1259. *termdebug_wide*
  1260. To change the width of the Vim window when debugging starts and use a vertical
  1261. split: >
  1262. let g:termdebug_config['wide'] = 163
  1263. If there is no g:termdebug_config you can use: >
  1264. let g:termdebug_wide = 163
  1265. This will set 'columns' to 163 when `:Termdebug` is used. The value is
  1266. restored when quitting the debugger.
  1267. If the wide value is set and 'columns' is already a greater value, then a
  1268. vertical split will be used without modifying 'columns'.
  1269. Set the wide value to 1 to use a vertical split without ever changing
  1270. 'columns'. This is useful when the terminal can't be resized by Vim.
  1271. vim:tw=78:ts=8:noet:ft=help:norl: