vimrc make comments italic
Asked Answered
A

6

50

How do I change the ~/.vimrc to have the comments in my code italicized?

In my ~/.vimrc file I have:

highlight Comment ctermfg=blue

that makes the comments blue. What do I need to do differently to make them italic?

Agincourt answered 16/8, 2010 at 15:5 Comment(0)
K
51
highlight Comment cterm=italic gui=italic

You'll need a font with an italic set and a terminal capable of displaying italics. Also, if you're using a color scheme other than the default, the above line should come after the color scheme is loaded in your ~/.vimrc so that the color scheme doesn't override it.

The cterm makes it work in the terminal and the gui is for graphical Vim clients.

Kramer answered 16/8, 2010 at 16:0 Comment(9)
why should it be in a special color scheme file? i find it more convenient to have all the customization in one. is there any website where they show how to get this done?Agincourt
See my edit. It can be in your ~/.vimrc, but it should come after the color scheme file is loaded, so that the color scheme file doesn't override your customizations. If you're not using a color scheme file (and it sounds like you aren't), don't worry about it.Kramer
seems italicizing is more trouble than its worth.Agincourt
@Hermann that's not a trouble at all. It feels quite natural once You get used to it. Many programs are configured with these .*rc files on unix based systems.Embroil
so where can i find the fonts with an italic set and is my terminal on kubuntu able to display italics?Agincourt
This doesn't work in an xterm (patch 314), even when the xterm can display italics with printf %s '\033[3mItalics\n (tried it with a TrueType font). It appears to be a limitation in vim, or some other configuration issue.Stratocumulus
I switched from vim to neovim and italics stopped working. Adding gui=italic solved the problem for me, even though I am running nvim inside a terminal.Coaxial
@Coaxial How did you manage to set the italic font for comments in the Novim?Menispermaceous
This doesn't work for me without also overwriting the escape codes from the other comments.Everetteeverglade
Z
54

First and foremost, you should check if you terminal is capable of displaying text in italics. In your terminal type (-e flag makes sure escape codes are interpreted)

echo -e "\e[3m foo \e[23m"

If you see foo then okay, otherwise you need to change terminal (Gnome Terminal and Konsole are good choices).

Then you should help Vim to recognise the kind of terminal you are using, put in you ~/.bashrc:

export TERM="xterm-256color"

Now you can try and see if this is enough, open a new file vim foo.html with the following content

<i>foo</i>

Do you see foo in italic? If no then you need to go a little further, right now Vim doesn't know the escape codes to switch to italic mode, you need to tell it (this is the hardest part, it took me a few years to figure that out).

Put the following two lines in your ~/.vimrc

set t_ZH=^[[3m
set t_ZR=^[[23m

These are the same escape codes we used before in the terminal, be aware that ^[ are not literal characters but represent the escape character, you can insert it in insert mode with CTRL-V followed by ESC (see :help i_CTRL-V)

Now reopen the file we created before foo.html and you should see foo in italic; if you don't then I can't help you any more. Otherwise you are almost done; there is one last step.

Put in you ~/.vimrc file

highlight Comment cterm=italic

after loading any colorscheme.

Zo answered 12/1, 2014 at 16:48 Comment(7)
Yes, as far as I know :-)Zo
I checked today but it does not work for me my gnome-terminal's version is 3.6.1 which runs under the Ubuntu. So can you check it?Rosamondrosamund
You are right, I'm currently using gnome-terminal 3.10.2, the only thing I can suggest you is to upgrade to a more recent version if you can :-(Zo
Doesn't the check require echo -e to enable interpretation of backslash escapes?Jaborandi
@GabrieleLana I meant echo -e "\e[3m foo \e[23m".Jaborandi
@BenjaminW. yup, the change got lost somehow :-/ now should be okZo
@GabrieleLana In my terminals running NeoVim 0.2.0 (Gnome Terminal 3.22.1, Terminix 1.3.5, Tilda 1.3.3), I had to set highlight Comment gui=italic for this to work properly.Recondition
K
51
highlight Comment cterm=italic gui=italic

You'll need a font with an italic set and a terminal capable of displaying italics. Also, if you're using a color scheme other than the default, the above line should come after the color scheme is loaded in your ~/.vimrc so that the color scheme doesn't override it.

The cterm makes it work in the terminal and the gui is for graphical Vim clients.

Kramer answered 16/8, 2010 at 16:0 Comment(9)
why should it be in a special color scheme file? i find it more convenient to have all the customization in one. is there any website where they show how to get this done?Agincourt
See my edit. It can be in your ~/.vimrc, but it should come after the color scheme file is loaded, so that the color scheme file doesn't override your customizations. If you're not using a color scheme file (and it sounds like you aren't), don't worry about it.Kramer
seems italicizing is more trouble than its worth.Agincourt
@Hermann that's not a trouble at all. It feels quite natural once You get used to it. Many programs are configured with these .*rc files on unix based systems.Embroil
so where can i find the fonts with an italic set and is my terminal on kubuntu able to display italics?Agincourt
This doesn't work in an xterm (patch 314), even when the xterm can display italics with printf %s '\033[3mItalics\n (tried it with a TrueType font). It appears to be a limitation in vim, or some other configuration issue.Stratocumulus
I switched from vim to neovim and italics stopped working. Adding gui=italic solved the problem for me, even though I am running nvim inside a terminal.Coaxial
@Coaxial How did you manage to set the italic font for comments in the Novim?Menispermaceous
This doesn't work for me without also overwriting the escape codes from the other comments.Everetteeverglade
H
28

In my case I had to put this in my vimrc file:

let &t_ZH="\e[3m"
let &t_ZR="\e[23m"
highlight Comment cterm=italic

Notice it is not the same as:

set t_ZH=^[[3m
set t_ZR=^[[23m
highlight Comment cterm=italic

The former worked for me, while the latter didn't.

Honeyed answered 19/6, 2015 at 12:12 Comment(4)
"let" instead of "set" worked for me as well on Ubuntu 14.04.3. I'm happily seeing italics in my vim now.Putandtake
Fabulous, +1, because the former solution uses ASCII only characters in a literal form. Cheers!Rai
for the latter, you do the ^[ by C-v Esc, not by typing ^ [Societal
THIS works on Mac, unlike the other, accepted solutionSelry
S
8

for GUI environments like gvim, a simple

highlight Comment gui=italic

does it.

Slovak answered 3/9, 2014 at 8:47 Comment(1)
This worked for me and I'm not using a GUI environment. Not sure if using a 4k monitor be relatedDeterminism
T
3

michaelmichael's answer should solve it for most cases. But, just in case you need this for a font in gvim that doesn't have italics (but oblique or something instead), you can try something like this in ~/.gvimrc

highlight Comment font=Bitstream_Vera_Sans_Mono_Oblique:h14

where h14 is the font size. This font should have the same cell size as your normal font though, so don't use an altogether different font.

Terina answered 18/8, 2010 at 21:21 Comment(0)
G
0

Because I'm using the Solarized colorscheme, I had to edit .vim/colors/solarized.vim as recommended in Solarized #120 to replace lines 137-157 with the following:

if has("gui_running") || ( has("unix") && system("tput sitm") == "\033[3m" )
    let s:terminal_italic=1
else
    let s:terminal_italic=0
endif

That was in addition following the instructions in this Gist and adding these two lines to my .vimrc, using Ctrl-vEsc to enter ^[:

set t_ZH=^[[3m
set t_ZR=^[[23m

(Thanks to Gabriele Lana for the tip to add those lines to my .vimrc.)

Garaway answered 12/5, 2021 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.