tui.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. *tui.txt* Nvim
  2. NVIM REFERENCE MANUAL
  3. Terminal UI *TUI* *tui*
  4. By default when you run `nvim` (without |--embed| or |--headless|) it starts
  5. the builtin "terminal UI" (TUI). This default UI is optional: you can run Nvim
  6. as a "headless" server, or you can use a |GUI|.
  7. Type |gO| to see the table of contents.
  8. ==============================================================================
  9. Startup *startup-tui* *startup-terminal*
  10. Nvim has a client-server architecture: by default when you run `nvim`, this
  11. starts the builtin UI client, which starts a `nvim --embed` server (child)
  12. process that the UI client connects to. After attaching to the server, the UI
  13. client calls |nvim_set_client_info()| (as recommended for all UIs |dev-ui|)
  14. and sets these fields on its channel: >
  15. client = {
  16. attributes = {
  17. license = 'Apache 2',
  18. pid = …,
  19. website = 'https://neovim.io',
  20. },
  21. name = 'nvim-tui',
  22. type = 'ui',
  23. version = { … },
  24. }
  25. Nvim guesses the terminal type when it starts (except in |--embed| and
  26. |--headless| modes). The |$TERM| environment variable is the primary hint that
  27. determines the terminal type.
  28. *terminfo* *E557* *E558* *E559*
  29. To display its user interface, Nvim reads a list of "terminal capabilities"
  30. from the system terminfo database (or builtin defaults if terminfo is not
  31. found). If that information is wrong, the screen may be messed up or keys may
  32. not be recognized.
  33. The Unibilium library (used to read terminfo) allows you to override the
  34. system terminfo with one in the "$HOME/.terminfo/" directory. Building your
  35. own terminfo is usually as simple as running this:
  36. >
  37. curl -LO https://invisible-island.net/datafiles/current/terminfo.src.gz
  38. gunzip terminfo.src.gz
  39. tic -x terminfo.src
  40. <
  41. *$TERM*
  42. The $TERM environment variable must match the terminal you are using!
  43. Otherwise Nvim cannot know what sequences your terminal expects, and weird
  44. or sub-optimal behavior will result (scrolling quirks, wrong colors, etc.).
  45. $TERM is also important because it is forwarded by SSH to the remote session,
  46. unlike most other environment variables.
  47. >
  48. For this terminal Set $TERM to |builtin-terms|
  49. -------------------------------------------------------------------------
  50. anything libvte-based vte, vte-256color Y
  51. (e.g. GNOME Terminal) (aliases: gnome, gnome-256color)
  52. iTerm (original) iterm, iTerm.app N
  53. iTerm2 (new capabilities) iterm2, iTerm2.app Y
  54. Konsole konsole-256color N
  55. Linux virtual terminal linux, linux-256color Y
  56. PuTTY putty, putty-256color Y
  57. rxvt rxvt, rxvt-256color Y
  58. screen screen, screen-256color Y
  59. simple terminal (st) st, st-256color Y
  60. Terminal.app nsterm N
  61. tmux tmux, tmux-256color Y
  62. Windows/ConEmu conemu Y
  63. Windows/Cygwin-built Nvim cygwin Y
  64. Windows/Interix interix Y
  65. Windows/VTP console vtpcon Y
  66. Windows/legacy console win32con Y
  67. xterm or compatible xterm, xterm-256color Y
  68. <
  69. *builtin-terms* *builtin_terms*
  70. If a |terminfo| database is not available or there is no entry for the current
  71. terminal, Nvim will map |$TERM| to a builtin entry according to the above
  72. table, or "ansi" if there is no match. For example "TERM=putty-256color" will
  73. be mapped to the builtin "putty" entry. See also |tui-colors|.
  74. The builtin terminfo is not combined with any external terminfo database, nor
  75. can it be used in preference to one. You can thus entirely override any
  76. omissions or out-of-date information in the builtin terminfo database by
  77. supplying an external one with entries for the terminal type.
  78. Settings depending on terminal *term-dependent-settings*
  79. If you want to set terminal-dependent options or mappings, you can do this in
  80. your init.vim. Example: >vim
  81. if $TERM =~ '^\(rxvt\|screen\|interix\|putty\)\(-.*\)\?$'
  82. set notermguicolors
  83. elseif $TERM =~ '^\(tmux\|iterm\|vte\|gnome\)\(-.*\)\?$'
  84. set termguicolors
  85. elseif $TERM =~ '^\(xterm\)\(-.*\)\?$'
  86. if $XTERM_VERSION != ''
  87. set termguicolors
  88. elseif $KONSOLE_PROFILE_NAME != ''
  89. set termguicolors
  90. elseif $VTE_VERSION != ''
  91. set termguicolors
  92. else
  93. set notermguicolors
  94. endif
  95. elseif $TERM =~ ...
  96. ... and so forth ...
  97. endif
  98. <
  99. *scroll-region* *xterm-scroll-region*
  100. Where possible, Nvim will use the terminal's ability to set a scroll region in
  101. order to redraw faster when a window is scrolled. If the terminal's terminfo
  102. description describes an ability to set top and bottom scroll margins, that is
  103. used.
  104. This will not speed up scrolling in a window that is not the full width of the
  105. terminal. Xterm has an extra ability, not described by terminfo, to set left
  106. and right scroll margins as well. If Nvim detects that the terminal is Xterm,
  107. it will make use of this ability to speed up scrolling that is not the full
  108. width of the terminal.
  109. *tui-input*
  110. Historically, terminal emulators could not distinguish between certain control
  111. key modifiers and other keys. For example, <C-I> and <Tab> are represented in
  112. the same way, as are <Esc> and <C-[>, <CR> and <C-M>, and <NL> and <C-J>.
  113. Modern terminal emulators are able to distinguish between these pairs of keys
  114. by encoding control modifiers differently. There are two common but distinct
  115. ways of doing this, known as "modifyOtherKeys" and "CSI u". Nvim supports both
  116. encoding methods and at startup will tell the terminal emulator that it
  117. understands these key encodings. If your terminal emulator supports it then
  118. this will allow you to map the key pairs listed above separately. |<Tab>|
  119. Nvim uses libtermkey to convert terminal escape sequences to key codes.
  120. |terminfo| is used first, and CSI sequences not in |terminfo| (including
  121. extended keys a.k.a. "modifyOtherKeys" or "CSI u") can also be parsed.
  122. For example, when running Nvim in tmux, this makes Nvim leave Insert mode and
  123. go to the window below: >
  124. tmux send-keys 'Escape' [ 2 7 u 'C-W' j
  125. Where `'Escape' [ 2 7 u` is an unambiguous "CSI u" sequence for the <Esc> key.
  126. The kitty keyboard protocol https://sw.kovidgoyal.net/kitty/keyboard-protocol/
  127. is partially supported, including keypad keys in Unicode Private Use Area.
  128. For example, this sequence is recognized by Nvim as <C-kEnter>: >
  129. CSI 57414 ; 5 u
  130. and can be used differently from <C-CR> in mappings.
  131. *tui-modifyOtherKeys* *tui-csiu*
  132. At startup Nvim will query your terminal to see if it supports the "CSI u"
  133. encoding by writing the sequence >
  134. CSI ? u CSI c
  135. If your terminal emulator responds with >
  136. CSI ? <flags> u
  137. this means your terminal supports the "CSI u" encoding and Nvim will tell your
  138. terminal to enable it by writing the sequence >
  139. CSI > 1 u
  140. If your terminal does not support "CSI u" then Nvim will instead enable the
  141. "modifyOtherKeys" encoding by writing the sequence >
  142. CSI > 4 ; 2 m
  143. When Nvim exits cleanly it will send the corresponding sequence to disable the
  144. special key encoding. If Nvim does not exit cleanly then your terminal
  145. emulator could be in a bad state. If this happens, simply run "reset".
  146. *tui-colors*
  147. Nvim uses 256 colours by default, ignoring |terminfo| for most terminal types,
  148. including "linux" (whose virtual terminals have had 256-colour support since
  149. 4.8) and anything claiming to be "xterm". Also when $COLORTERM or $TERM
  150. contain the string "256".
  151. Nvim similarly assumes that any terminal emulator that sets $COLORTERM to any
  152. value, is capable of at least 16-colour operation.
  153. *true-color* *xterm-true-color*
  154. Nvim emits true (24-bit) colours in the terminal, if 'termguicolors' is set.
  155. It uses the "setrgbf" and "setrgbb" |terminfo| extensions (proposed by Rüdiger
  156. Sonderfeld in 2013). If your terminfo definition is missing them, then Nvim
  157. will decide whether to add them to your terminfo definition, using the ISO
  158. 8613-6:1994/ITU T.416:1993 control sequences for setting RGB colours (but
  159. modified to use semicolons instead of colons unless the terminal is known to
  160. follow the standard).
  161. Another convention, pioneered in 2016 by tmux, is the "Tc" terminfo extension.
  162. If terminfo has this flag, Nvim will add constructed "setrgbf" and "setrgbb"
  163. capabilities as if they had been in the terminfo definition.
  164. If terminfo does not (yet) have this flag, Nvim will fall back to $TERM and
  165. other environment variables. It will add constructed "setrgbf" and "setrgbb"
  166. capabilities in the case of the "rxvt", "linux", "st", "tmux", and "iterm"
  167. terminal types, or when Konsole, genuine Xterm, a libvte terminal emulator
  168. version 0.36 or later, or a terminal emulator that sets the COLORTERM
  169. environment variable to "truecolor" is detected.
  170. *xterm-resize*
  171. Nvim can resize the terminal display on some terminals that implement an
  172. extension pioneered by dtterm. |terminfo| does not have a flag for this
  173. extension. So Nvim simply assumes that (all) "dtterm", "xterm", "teraterm",
  174. "rxvt" terminal types, and Konsole, are capable of this.
  175. *tui-cursor-shape*
  176. Nvim will adjust the shape of the cursor from a block to a line when in insert
  177. mode (or as specified by the 'guicursor' option), on terminals that support
  178. it. It uses the same |terminfo| extensions that were pioneered by tmux for
  179. this: "Ss" and "Se".
  180. Similarly, if you set the cursor highlight group with blend=100, Nvim hides
  181. the cursor through the "cvvis" and "civis" extensions.
  182. If your terminfo definition is missing them, then Nvim will decide whether to
  183. add them to your terminfo definition, by looking at $TERM and other
  184. environment variables. For the "rxvt", "putty", "linux", "screen",
  185. "teraterm", and "iterm" terminal types, or when Konsole, a libvte-based
  186. terminal emulator, or genuine Xterm are detected, it will add constructed
  187. "Ss" and "Se" capabilities.
  188. *tui-cursor-tmux*
  189. Within tmux it may appear that Nvim is not changing the cursor, but in fact it
  190. is tmux receiving instructions from Nvim to change the cursor and not knowing
  191. what to do in turn. tmux must translate what it receives from Nvim into
  192. whatever control sequence is appropriate for the host terminal. It shares
  193. a common mechanism with Nvim, of using the "Ss" and "Se" capabilities from
  194. terminfo (for the output terminal) if they are present. Unlike Nvim, if they
  195. are not in terminfo you must add them by setting "terminal-overrides" in
  196. ~/.tmux.conf .
  197. See the tmux(1) manual page for the details of how and what to do in the tmux
  198. configuration file. It will look something like: >bash
  199. set -ga terminal-overrides '*:Ss=\E[%p1%d q:Se=\E[ q'
  200. or (alas!) for Konsole 18.07.70 or older, something more complex like: >bash
  201. set -ga terminal-overrides 'xterm*:\E]50;CursorShape=%?%p1%{3}%<%t%{0}%e%{1}%;%d\007'
  202. <
  203. ==============================================================================
  204. Window size *window-size*
  205. [This is about the size of the whole window Vim is using, not a window that is
  206. created with the ":split" command.]
  207. On Unix systems, three methods are tried to get the window size:
  208. - an ioctl call (TIOCGSIZE or TIOCGWINSZ, depends on your system)
  209. - the environment variables "LINES" and "COLUMNS"
  210. - from the |terminfo| entries "lines" and "columns"
  211. If everything fails a default size of 24 lines and 80 columns is assumed. If
  212. a window-resize signal is received the size will be set again. If the window
  213. size is wrong you can use the 'lines' and 'columns' options to set the
  214. correct values. See |:mode|.
  215. ==============================================================================
  216. Slow and fast terminals *slow-fast-terminal*
  217. *slow-terminal*
  218. If you have a slow terminal you may want to reset the 'showcmd' and 'ruler'
  219. options. The command characters and cursor positions will not be shown in the
  220. status line (which involves a lot of cursor motions and attribute changes for
  221. every keypress or movement). If the terminal scrolls very slowly, set the
  222. 'scrolljump' to 5 or so. If the cursor is moved off the screen (e.g., with
  223. "j") Vim will scroll 5 lines at a time. Another possibility is to reduce the
  224. number of lines that Vim uses with the command "z{height}<CR>".
  225. If the characters from the terminal are arriving with more than 1 second
  226. between them you might want to set the 'timeout' and/or 'ttimeout' option.
  227. If you are using a color terminal that is slow when displaying lines beyond
  228. the end of a buffer, this is because Nvim is drawing the whitespace twice, in
  229. two sets of colours and attributes. To prevent this, use this command: >vim
  230. hi NonText cterm=NONE ctermfg=NONE
  231. This draws the spaces with the default colours and attributes, which allows the
  232. second pass of drawing to be optimized away. Note: Although in theory the
  233. colours of whitespace are immaterial, in practice they change the colours of
  234. cursors and selections that cross them. This may have a visible, but minor,
  235. effect on some UIs.
  236. vim:et:sw=2:tw=78:ts=8:ft=help:norl: