Skip to content

Italics

Romain Lafourcade edited this page Jul 19, 2023 · 2 revisions

Italics

The basics

The usual method for affecting how the text is displayed in a terminal emulator is to use escape sequences. For example, running the command below in your Unix shell should give you a red Hello!:

$ echo -e '\033[0;31mHello!\033[0m'

The exact semantics are not important but, in that simple example, the word Hello! is sandwiched between two escape codes:

  • \033[0;31m, which tells the terminal to use red from now on,
  • \033[0m, which tells the terminal to revert back to normal.

The principle is relatively straightforward: you start a "mode" (red, bold, italics, etc.), then you print some text that you want to be affected by that "mode", then you stop that "mode".

That is how your favorite package manager shows deprecation warnings when you install dependencies and how Vim does all its visual magic.

Note that this also concerns GUI Vim (GVim, the MacVim GUI), where that GUI window is for all intent and purpose a glorified terminal emulator.

Use of italics in Vim: the theory

In theory, using italics in a color scheme is dead easy:

hi Comment guifg=#afafff guibg=italic ctermfg=147 ctermbg=NONE cterm=italic

but the GUI and the TUI are not fighting on equal terms. Where the GUI, because of how it is built, can be reasonably trusted to do italics right, the terminal emulator in which Vim is executed can't. In other words, you can reasonably expect the snippet below to have the expected result in GUI Vim but not in TUI Vim.

Use of italics in Vim: the reality

Terminals, real and emulated, have a very long and convoluted history. The example I used in the first section will probably work as-is in most modern terminal emulators but that's because their authors generally try to be compliant with lots and lots of specifications that may date back to the sixties or earlier. In practice, there still are many discrepancies between how $TERMINAL_EMULATOR_A and $TERMINAL_EMULATOR_B implement $FEATURE_X and programs often have to follow different code paths in order to work consistently across terminal emulators, Vim included.

Vim uses a strategy called "feature detection": During startup, it asks its host what escape sequence to use for a given feature and either stores the answer if it is satisfactory or falls back to some other escape sequence if it is not.

[TBC]