Enabling italics in vim syntax highlighting for mac terminal
Asked Answered
N

7

14

I'd like to have vim display my comments in italics, and I understand I need to place

cterm=italic

in the

hi Comment

line in the color.vim file I'm using. This, however, is having no effect on the text display, which I suspect has to do with some Terminal.app setting, unless I'm misunderstanding the vim syntax. I'd appreciate if someone can show me how to enable this feature.

Additionally, I am currently using the Monaco font, which does not have a separate italic file (however, the italic syntax-highlighting doesn't work for Consolas, Lucida, Bitstream Vera or other italic- or oblique-enabled fonts either). Assuming that a solution exists for fonts with italics, do I have to jump through any further hoops to get Monaco working?

Thanks for any input.

EDIT:
I'm surprised I haven't gotten an answer yet; this doesn't seem like it should be too difficult to do. Maybe it is. Alternatively, could someone explain why this would not be possible?

Nicholson answered 25/8, 2009 at 9:1 Comment(0)
L
6

As of SnowLeopard, Terminal doesn’t support the italic attribute (SGR, value 3). Feel free to file a request at http://bugreporter.apple.com.

Update: Italic support was added to Terminal in macOS Sierra 10.12. Note that the xterm terminfo files included with that version of ncurses do not declare the italic sitm capability. See the vim-specific workaround in Bahman Eslami’s answer https://mcmap.net/q/806150/-enabling-italics-in-vim-syntax-highlighting-for-mac-terminal or cheon’s answer https://mcmap.net/q/806150/-enabling-italics-in-vim-syntax-highlighting-for-mac-terminal for creating a terminfo file that declares sitm.

Lettuce answered 26/7, 2010 at 18:58 Comment(2)
I think macOS Big Sur broke italics again. Well.Escape
@MingyeWang Thanks for the comment. I was struggling to understand why the same init.vim was correctly working on one Mac and not working on another Mac. It could be that, one the working Mac, the configuration was there prior to upgrading to Big Sur. On the fresh Big Sur, italics are broken.Merriemerrielle
M
11

As of OS X Sierra 10.12, the default terminal app supports italics; however, the included version of ncurses contains xterm terminfo files that do not declare italic support (they do not define the sitm capability). To work around this in Vim, add the following to your vimrc file to define the terminal commands for enabling/disabling italics:

let &t_ZH="\e[3m"
let &t_ZR="\e[23m"

Then make sure the font you use supports italics and also your colorscheme contains italic for some syntax parts. Or, to customize the syntax highlighting locally to format comments in italics, add the following to your vimrc file:

highlight Comment cterm=italic
Mako answered 5/12, 2018 at 5:54 Comment(1)
This was the only solution that worked for me when running Vim in iTerm2 on macOS Monterey 12.1. (Apple Silicon M1 if that matters.)Crucible
L
6

As of SnowLeopard, Terminal doesn’t support the italic attribute (SGR, value 3). Feel free to file a request at http://bugreporter.apple.com.

Update: Italic support was added to Terminal in macOS Sierra 10.12. Note that the xterm terminfo files included with that version of ncurses do not declare the italic sitm capability. See the vim-specific workaround in Bahman Eslami’s answer https://mcmap.net/q/806150/-enabling-italics-in-vim-syntax-highlighting-for-mac-terminal or cheon’s answer https://mcmap.net/q/806150/-enabling-italics-in-vim-syntax-highlighting-for-mac-terminal for creating a terminfo file that declares sitm.

Lettuce answered 26/7, 2010 at 18:58 Comment(2)
I think macOS Big Sur broke italics again. Well.Escape
@MingyeWang Thanks for the comment. I was struggling to understand why the same init.vim was correctly working on one Mac and not working on another Mac. It could be that, one the working Mac, the configuration was there prior to upgrading to Big Sur. On the fresh Big Sur, italics are broken.Merriemerrielle
H
6

Italic support was added to Terminal in macOS Sierra 10.12 (after this question was asked); however, the xterm terminfo files included with that version of ncurses do not declare the italic sitm capability. You can work around this by creating a local terminfo file that declares the capability and inherits whichever terminfo file you’re currently using.

In the following instructions, use whatever name TERM is set to. xterm-256color is the default used by Terminal’s built-in default profiles.

Create a file named xterm-256color-italic.terminfo:

# A xterm-256color based TERMINFO that adds the escape sequences for italic.
xterm-256color-italic|xterm with 256 colors and italic,
  sitm=\E[3m, ritm=\E[23m,
  use=xterm-256color,

Run tic xterm-256-italic.terminfo in your terminal, it will generate a file ~/.terminfo/78/xterm-256color-italic. This file will be found by ncurses automatically when TERM is set to the file’s name.

To set TERM=xterm-256-italic for an individual Terminal profile:

profiles

or you can just replace /usr/share/terminfo/78/xterm-256color with ~/.terminfo/78/xterm-256color-italic

sudo cp /usr/share/terminfo/78/xterm-256color /usr/share/terminfo/78/xterm-256color-bak
sudo cp ~/.terminfo/78/xterm-256-color-italic /usr/share/terminfo/78/xterm-256color

Then you can find italics font in vim:

vim

Hege answered 30/1, 2018 at 2:25 Comment(2)
Doing this using the profile’s “Run command:” setting is helpful if you want to restrict this behavior to a particular profile, but I recommend using the standard shell login or rc file to do this conditionally based on whether TERM is set to xterm-256color, e.g., case "$TERM" in xterm-256color) TERM=xterm-256color-italic;; esacHeadlock
I wouldn't mess with non-standard TERM declarations like xterm-256-italic... you WILL sooner or later experience brokenness with it.Morphogenesis
S
2

Italics support is coming to the iTerm2 terminal app - it's in the nightly builds now. As mentioned in the enhancement request you need to configure the TERMINFO var correctly.

Semimonthly answered 12/6, 2013 at 13:56 Comment(0)
C
0

Konrad, don't know about Terminal.app, but italic is supported in a lot of different terminal emulators. urxvt, konsole, gnome-terminal come to mind.

Christianism answered 25/8, 2009 at 9:34 Comment(3)
any on Mac? Terminal.app and iTerm doesn't or maybe it's a TERM configuration problem.Expunction
Try Terminator, which is a nice cross-platform replacement for Terminal.app written in Java. No idea if you can get console vim to display italics, though, rather than just bold or inverted text, which is what it does on my box.Overhang
At the time this answer was created, Terminal did not support italic text. It does as of macOS Sierra 10.12. Additionally, the xterm terminfo files included with that version of ncurses do not declare the italic sitm capability. See the workaround in Bahman Eslami’s answer: https://mcmap.net/q/806150/-enabling-italics-in-vim-syntax-highlighting-for-mac-terminalHeadlock
T
0

You can't with the regular monaco-font it seems.

It seems that only MacVim enables the italics.

Here

Tropology answered 26/8, 2009 at 13:4 Comment(1)
What do you mean by "it seems"? Is there a url you could point me to?Nicholson
S
0

In my testing, adding set background= or set background=dark or set background=light to one's vimrc file does the trick! Even if I am using the default preinstalled xterm-256color terminfo with no italics information in it! Nothing else works, even if I use xterm-256color-italic suggested in previous answers.

Complete solution, building on top previous answers is:

let &t_ZH="\e[3m"
let &t_ZR="\e[23m"
highlight Comment cterm=italic
set background=dark " or set background=light or set background=

I don't know why this is so, have asked a question to Vim devs.

Safari answered 15/2, 2021 at 3:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.