nvim_terminal_emulator.txt 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. *terminal_emulator.txt* Nvim
  2. NVIM REFERENCE MANUAL by Thiago de Arruda
  3. Terminal emulator *terminal* *terminal-emulator*
  4. Nvim embeds a VT220/xterm terminal emulator based on libvterm. The terminal is
  5. presented as a special 'buftype', asynchronously updated as data is received
  6. from the connected program.
  7. Terminal buffers behave like normal buffers, except:
  8. - With 'modifiable', lines can be edited but not deleted.
  9. - 'scrollback' controls how many lines are kept.
  10. - Output is followed ("tailed") if cursor is on the last line.
  11. - 'modified' is the default. You can set 'nomodified' to avoid a warning when
  12. closing the terminal buffer.
  13. - 'bufhidden' defaults to "hide".
  14. Type |gO| to see the table of contents.
  15. ==============================================================================
  16. Start *terminal-start*
  17. There are several ways to create a terminal buffer:
  18. - Run the |:terminal| command.
  19. - Call the |nvim_open_term()| or |termopen()| function.
  20. - Edit a "term://" buffer. Examples: >
  21. :edit term://bash
  22. :vsplit term://top
  23. < Note: To open a "term://" buffer from an autocmd, the |autocmd-nested|
  24. modifier is required. >
  25. autocmd VimEnter * ++nested split term://sh
  26. < (This is only mentioned for reference; use |:terminal| instead.)
  27. When the terminal starts, the buffer contents are updated and the buffer is
  28. named in the form of `term://{cwd}//{pid}:{cmd}`. This naming scheme is used
  29. by |:mksession| to restore a terminal buffer (by restarting the {cmd}).
  30. The terminal environment is initialized as in |jobstart-env|.
  31. ==============================================================================
  32. Input *terminal-input*
  33. To send input, enter |Terminal-mode| with |i|, |I|, |a|, |A| or
  34. |:startinsert|. In this mode all keys except <C-\> are sent to the underlying
  35. program. If <C-\> is pressed, the next key is sent unless it is <C-N> or <C-O>.
  36. Use <C-\><C-N> to return to normal mode. |CTRL-\_CTRL-N|
  37. Use <C-\><C-O> to execute one normal mode command and then return to terminal
  38. mode. *t_CTRL-\_CTRL-O*
  39. Terminal-mode forces these local options:
  40. 'cursorlineopt' = number
  41. 'nocursorcolumn'
  42. 'scrolloff' = 0
  43. 'sidescrolloff' = 0
  44. Terminal-mode has its own |:tnoremap| namespace for mappings, this can be used
  45. to automate any terminal interaction.
  46. To map <Esc> to exit terminal-mode: >
  47. :tnoremap <Esc> <C-\><C-n>
  48. To simulate |i_CTRL-R| in terminal-mode: >
  49. :tnoremap <expr> <C-R> '<C-\><C-N>"'.nr2char(getchar()).'pi'
  50. To use `ALT+{h,j,k,l}` to navigate windows from any mode: >
  51. :tnoremap <A-h> <C-\><C-N><C-w>h
  52. :tnoremap <A-j> <C-\><C-N><C-w>j
  53. :tnoremap <A-k> <C-\><C-N><C-w>k
  54. :tnoremap <A-l> <C-\><C-N><C-w>l
  55. :inoremap <A-h> <C-\><C-N><C-w>h
  56. :inoremap <A-j> <C-\><C-N><C-w>j
  57. :inoremap <A-k> <C-\><C-N><C-w>k
  58. :inoremap <A-l> <C-\><C-N><C-w>l
  59. :nnoremap <A-h> <C-w>h
  60. :nnoremap <A-j> <C-w>j
  61. :nnoremap <A-k> <C-w>k
  62. :nnoremap <A-l> <C-w>l
  63. You can also create menus similar to terminal mode mappings, but you have to
  64. use |:tlmenu| instead of |:tmenu|.
  65. Mouse input has the following behavior:
  66. - If the program has enabled mouse events, the corresponding events will be
  67. forwarded to the program.
  68. - If mouse events are disabled (the default), terminal focus will be lost and
  69. the event will be processed as in a normal buffer.
  70. - If another window is clicked, terminal focus will be lost and nvim will jump
  71. to the clicked window
  72. - If the mouse wheel is used while the mouse is positioned in another window,
  73. the terminal won't lose focus and the hovered window will be scrolled.
  74. ==============================================================================
  75. Configuration *terminal-config*
  76. Options: 'modified', 'scrollback'
  77. Events: |TermOpen|, |TermEnter|, |TermLeave|, |TermClose|
  78. Highlight groups: |hl-TermCursor|, |hl-TermCursorNC|
  79. Terminal sets local defaults for some options, which may differ from your
  80. global configuration.
  81. - 'list' is disabled
  82. - 'wrap' is disabled
  83. You can change the defaults with a TermOpen autocommand: >
  84. au TermOpen * setlocal list
  85. TERMINAL COLORS ~
  86. The `{g,b}:terminal_color_x` variables control the terminal color palette,
  87. where `x` is the color index between 0 and 255 inclusive. The variables are
  88. read during |TermOpen|. The value must be a color name or hexadecimal string.
  89. Example: >
  90. let g:terminal_color_4 = '#ff0000'
  91. let g:terminal_color_5 = 'green'
  92. Only works for RGB UIs (see 'termguicolors'); for 256-color terminals the
  93. color index is just forwarded.
  94. Editor highlighting (|syntax-highlighting|, |highlight-groups|, etc.) has
  95. higher precedence: it is applied after terminal colors are resolved.
  96. ==============================================================================
  97. Status Variables *terminal-status*
  98. Terminal buffers maintain some buffer-local variables and options. The values
  99. are initialized before TermOpen, so you can use them in a local 'statusline'.
  100. Example: >
  101. :autocmd TermOpen * setlocal statusline=%{b:term_title}
  102. - *b:term_title* Terminal title (user-writable), typically displayed in the
  103. window title or tab title of a graphical terminal emulator. Terminal
  104. programs can set this by emitting an escape sequence.
  105. - |'channel'| Terminal PTY |job-id|. Can be used with |chansend()| to send
  106. input to the terminal.
  107. - The |TermClose| event gives the terminal job exit code in the |v:event|
  108. "status" field. For example, this autocmd closes terminal buffers if the job
  109. exited without error: >
  110. autocmd TermClose * if !v:event.status | exe 'bdelete! '..expand('<abuf>') | endif
  111. Use |jobwait()| to check if the terminal job has finished: >
  112. let running = jobwait([&channel], 0)[0] == -1
  113. ==============================================================================
  114. :Termdebug plugin *terminal-debug*
  115. The Terminal debugging plugin can be used to debug a program with gdb and view
  116. the source code in a Vim window. Since this is completely contained inside
  117. Vim this also works remotely over an ssh connection.
  118. Starting ~
  119. *termdebug-starting*
  120. Load the plugin with this command: >
  121. packadd termdebug
  122. < *:Termdebug*
  123. To start debugging use `:Termdebug` or `:TermdebugCommand` followed by the
  124. command name, for example: >
  125. :Termdebug vim
  126. This opens two windows:
  127. gdb window A terminal window in which "gdb vim" is executed. Here you
  128. can directly interact with gdb.
  129. program window A terminal window for the executed program. When "run" is
  130. used in gdb the program I/O will happen in this window, so
  131. that it does not interfere with controlling gdb.
  132. The current window is used to show the source code. When gdb pauses the
  133. source file location will be displayed, if possible. A sign is used to
  134. highlight the current position, using highlight group debugPC.
  135. If the buffer in the current window is modified, another window will be opened
  136. to display the current gdb position.
  137. Focus the terminal of the executed program to interact with it. This works
  138. the same as any command running in a terminal window.
  139. When the debugger ends, typically by typing "quit" in the gdb window, the two
  140. opened windows are closed.
  141. Only one debugger can be active at a time.
  142. *:TermdebugCommand*
  143. If you want to give specific commands to the command being debugged, you can
  144. use the `:TermdebugCommand` command followed by the command name and
  145. additional parameters. >
  146. :TermdebugCommand vim --clean -c ':set nu'
  147. Both the `:Termdebug` and `:TermdebugCommand` support an optional "!" bang
  148. argument to start the command right away, without pausing at the gdb window
  149. (and cursor will be in the debugged window). For example: >
  150. :TermdebugCommand! vim --clean
  151. To attach gdb to an already running executable or use a core file, pass extra
  152. arguments. E.g.: >
  153. :Termdebug vim core
  154. :Termdebug vim 98343
  155. If no argument is given, you'll end up in a gdb window, in which you need to
  156. specify which command to run using e.g. the gdb `file` command.
  157. Example session ~
  158. *termdebug-example*
  159. Start in the Vim "src" directory and build Vim: >
  160. % make
  161. Start Vim: >
  162. % ./vim
  163. Load the termdebug plugin and start debugging Vim: >
  164. :packadd termdebug
  165. :Termdebug vim
  166. You should now have three windows:
  167. source - where you started
  168. gdb - you can type gdb commands here
  169. program - the executed program will use this window
  170. Put focus on the gdb window and type: >
  171. break ex_help
  172. run
  173. Vim will start running in the program window. Put focus there and type: >
  174. :help gui
  175. Gdb will run into the ex_help breakpoint. The source window now shows the
  176. ex_cmds.c file. A red "1 " marker will appear in the signcolumn where the
  177. breakpoint was set. The line where the debugger stopped is highlighted. You
  178. can now step through the program. You will see the highlighting move as the
  179. debugger executes a line of source code.
  180. Run ":Next" a few times until the for loop is highlighted. Put the cursor on
  181. the end of "eap->arg", then call ":Eval". You will see this displayed:
  182. "eap->arg": 0x555555e68855 "gui" ~
  183. This way you can inspect the value of local variables. You can also focus the
  184. gdb window and use a "print" command, e.g.: >
  185. print *eap
  186. If mouse pointer movements are working, Vim will also show a balloon when the
  187. mouse rests on text that can be evaluated by gdb.
  188. You can also use the "K" mapping that will either use neovim floating windows
  189. if available to show the results or print below the status bar.
  190. Now go back to the source window and put the cursor on the first line after
  191. the for loop, then type: >
  192. :Break
  193. You will see a "1" marker appear, this indicates the new breakpoint. Now
  194. run ":Cont" command and the code until the breakpoint will be executed.
  195. You can type more advanced commands in the gdb window. For example, type: >
  196. watch curbuf
  197. Now run ":Cont" (or type "cont" in the gdb window). Execution
  198. will now continue until the value of "curbuf" changes, which is in do_ecmd().
  199. To remove this watchpoint again type in the gdb window: >
  200. delete 3
  201. You can see the stack by typing in the gdb window: >
  202. where
  203. Move through the stack frames, e.g. with: >
  204. frame 3
  205. The source window will show the code, at the point where the call was made to
  206. a deeper level.
  207. Stepping through code ~
  208. *termdebug-stepping*
  209. Put focus on the gdb window to type commands there. Some common ones are:
  210. - CTRL-C interrupt the program
  211. - next execute the current line and stop at the next line
  212. - step execute the current line and stop at the next statement,
  213. entering functions
  214. - until execute until past the current cursor line or past a specified
  215. position or the current stack frame returns
  216. - finish execute until leaving the current function
  217. - where show the stack
  218. - frame N go to the Nth stack frame
  219. - continue continue execution
  220. *:Run* *:Arguments*
  221. In the window showing the source code these commands can be used to control
  222. gdb:
  223. `:Run` [args] run the program with [args] or the previous arguments
  224. `:Arguments` {args} set arguments for the next `:Run`
  225. *:Break* set a breakpoint at the current line; a sign will be displayed
  226. *:Clear* delete the breakpoint at the current line
  227. *:Step* execute the gdb "step" command
  228. *:Over* execute the gdb "next" command (`:Next` is a Vim command)
  229. *:Until* execute the gdb "until" command
  230. *:Finish* execute the gdb "finish" command
  231. *:Continue* execute the gdb "continue" command
  232. *:Stop* interrupt the program
  233. If gdb stops at a source line and there is no window currently showing the
  234. source code, a new window will be created for the source code. This also
  235. happens if the buffer in the source code window has been modified and can't be
  236. abandoned.
  237. Gdb gives each breakpoint a number. In Vim the number shows up in the sign
  238. column, with a red background. You can use these gdb commands:
  239. - info break list breakpoints
  240. - delete N delete breakpoint N
  241. You can also use the `:Clear` command if the cursor is in the line with the
  242. breakpoint, or use the "Clear breakpoint" right-click menu entry.
  243. Inspecting variables ~
  244. *termdebug-variables* *:Evaluate*
  245. `:Evaluate` evaluate the expression under the cursor
  246. `K` same (see |termdebug_map_K| to disable)
  247. `:Evaluate` {expr} evaluate {expr}
  248. `:'<,'>Evaluate` evaluate the Visually selected text
  249. This is similar to using "print" in the gdb window.
  250. You can usually shorten `:Evaluate` to `:Ev`.
  251. Other commands ~
  252. *termdebug-commands*
  253. *:Gdb* jump to the gdb window
  254. *:Program* jump to the window with the running program
  255. *:Source* jump to the window with the source code, create it if there
  256. isn't one
  257. *:Asm* jump to the window with the disassembly, create it if there
  258. isn't one
  259. Events ~
  260. *termdebug-events*
  261. Four autocommands can be used: >
  262. au User TermdebugStartPre echomsg 'debugging starting'
  263. au User TermdebugStartPost echomsg 'debugging started'
  264. au User TermdebugStopPre echomsg 'debugging stopping'
  265. au User TermdebugStopPost echomsg 'debugging stopped'
  266. <
  267. *TermdebugStartPre*
  268. TermdebugStartPre Before starting debugging.
  269. Not triggered if the debugger is already
  270. running or the debugger command cannot be
  271. executed.
  272. *TermdebugStartPost*
  273. TermdebugStartPost After debugging has initialized.
  274. If a "!" bang is passed to `:Termdebug` or
  275. `:TermdebugCommand` the event is triggered
  276. before running the provided command in gdb.
  277. *TermdebugStopPre*
  278. TermdebugStopPre Before debugging ends, when gdb is terminated,
  279. most likely after issuing a "quit" command in
  280. the gdb window.
  281. *TermdebugStopPost*
  282. TermdebugStopPost After debugging has ended, gdb-related windows
  283. are closed, debug buffers wiped out and
  284. the state before the debugging was restored.
  285. Prompt mode ~
  286. *termdebug-prompt*
  287. When on MS-Windows, gdb will run in a buffer with 'buftype' set to "prompt".
  288. This works slightly differently:
  289. - The gdb window will be in Insert mode while typing commands. Go to Normal
  290. mode with <Esc>, then you can move around in the buffer, copy/paste, etc.
  291. Go back to editing the gdb command with any command that starts Insert mode,
  292. such as `a` or `i`.
  293. - A separate :terminal window will be opened to run the debugged program in.
  294. *termdebug_use_prompt*
  295. Prompt mode can be used with: >
  296. let g:termdebug_config['use_prompt'] = 1
  297. Or if there is no g:termdebug_config: >
  298. let g:termdebug_use_prompt = 1
  299. <
  300. *termdebug_map_K*
  301. The K key is normally mapped to :Evaluate. If you do not want this use: >
  302. let g:termdebug_config['map_K'] = 0
  303. Or if there is no g:termdebug_config: >
  304. let g:termdebug_map_K = 0
  305. <
  306. *termdebug_disasm_window*
  307. If you want the Asm window shown by default, set the flag to 1.
  308. the "disasm_window_height" entry can be used to set the window height: >
  309. let g:termdebug_config['disasm_window'] = 1
  310. let g:termdebug_config['disasm_window_height'] = 15
  311. or, if there is no g:termdebug_config: >
  312. let g:termdebug_disasm_window = 15
  313. Any value greater than 1 will set the Asm window height to that value.
  314. Communication ~
  315. *termdebug-communication*
  316. There is another, hidden, buffer, which is used for Vim to communicate with
  317. gdb. The buffer name is "gdb communication". Do not delete this buffer, it
  318. will break the debugger.
  319. Gdb has some weird behavior, the plugin does its best to work around that.
  320. For example, after typing "continue" in the gdb window a CTRL-C can be used to
  321. interrupt the running program. But after using the MI command
  322. "-exec-continue" pressing CTRL-C does not interrupt. Therefore you will see
  323. "continue" being used for the `:Continue` command, instead of using the
  324. communication channel.
  325. Customizing ~
  326. *termdebug-customizing* *g:termdebug_config*
  327. In the past several global variables were used for configuration. These are
  328. deprecated, using the g:termdebug_config dictionary is preferred. When
  329. g:termdebug_config exists the other global variables will not be used.
  330. GDB command ~
  331. *g:termdebugger*
  332. To change the name of the gdb command, set "debugger" entry in
  333. g:termdebug_config or the "g:termdebugger" variable before invoking
  334. `:Termdebug`: >
  335. let g:termdebug_config['command'] = "mygdb"
  336. Or if there is no g:termdebug_config: >
  337. let g:termdebugger = "mygdb"
  338. If the command needs an argument use a List: >
  339. let g:termdebug_config['command'] = ['rr', 'replay', '--']
  340. Or if there is no g:termdebug_config: >
  341. let g:termdebugger = ['rr', 'replay', '--']
  342. To not use neovim floating windows for previewing variable evaluation, set the
  343. `g:termdebug_useFloatingHover` variable like this: >
  344. let g:termdebug_useFloatingHover = 0
  345. If you are a mouse person, you can also define a mapping using your right
  346. click to one of the terminal command like evaluate the variable under the
  347. cursor: >
  348. nnoremap <RightMouse> :Evaluate<CR>
  349. or set/unset a breakpoint: >
  350. nnoremap <RightMouse> :Break<CR>
  351. Several arguments will be added to make gdb work well for the debugger.
  352. If you want to modify them, add a function to filter the argument list: >
  353. let g:termdebug_config['command_filter'] = MyDebugFilter
  354. If you do not want the arguments to be added, but you do need to set the
  355. "pty", use a function to add the necessary arguments: >
  356. let g:termdebug_config['command_add_args'] = MyAddArguments
  357. The function will be called with the list of arguments so far, and a second
  358. argument that is the name of the pty.
  359. *gdb-version*
  360. Only debuggers fully compatible with gdb will work. Vim uses the GDB/MI
  361. interface. The "new-ui" command requires gdb version 7.12 or later. if you
  362. get this error:
  363. Undefined command: "new-ui". Try "help".~
  364. Then your gdb is too old.
  365. Colors ~
  366. *hl-debugPC* *hl-debugBreakpoint*
  367. The color of the signs can be adjusted with these highlight groups:
  368. - debugPC the current position
  369. - debugBreakpoint a breakpoint
  370. The defaults are, when 'background' is "light":
  371. hi debugPC term=reverse ctermbg=lightblue guibg=lightblue
  372. hi debugBreakpoint term=reverse ctermbg=red guibg=red
  373. When 'background' is "dark":
  374. hi debugPC term=reverse ctermbg=darkblue guibg=darkblue
  375. hi debugBreakpoint term=reverse ctermbg=red guibg=red
  376. Shortcuts ~
  377. *termdebug_shortcuts*
  378. You can define your own shortcuts (mappings) to control gdb, that can work in
  379. any window, using the TermDebugSendCommand() function. Example: >
  380. map ,w :call TermDebugSendCommand('where')<CR>
  381. The argument is the gdb command.
  382. Popup menu ~
  383. *termdebug_popup*
  384. By default the Termdebug plugin sets 'mousemodel' to "popup_setpos" and adds
  385. these entries to the popup menu:
  386. Set breakpoint `:Break`
  387. Clear breakpoint `:Clear`
  388. Evaluate `:Evaluate`
  389. If you don't want this then disable it with: >
  390. let g:termdebug_config['popup'] = 0
  391. or if there is no g:termdebug_config: >
  392. let g:termdebug_popup = 0
  393. Vim window width ~
  394. *termdebug_wide*
  395. To change the width of the Vim window when debugging starts and use a vertical
  396. split: >
  397. let g:termdebug_config['wide'] = 163
  398. Or if there is no g:termdebug_config: >
  399. let g:termdebug_wide = 163
  400. This will set 'columns' to 163 when `:Termdebug` is used. The value is
  401. restored when quitting the debugger.
  402. If the wide value is set and 'columns' is already a greater value, then a
  403. vertical split will be used without modifying 'columns'.
  404. Set the wide value to 1 to use a vertical split without ever changing
  405. 'columns'. This is useful when the terminal can't be resized by Vim.
  406. vim:tw=78:ts=8:noet:ft=help:norl: