vimrc make comments italic - vim

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?

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.

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.

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.

for GUI environments like gvim, a simple
highlight Comment gui=italic
does it.

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.

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.)

Related

How to get matching brace highlighting without cursor jump in vim

I am running vim 7.3 on several machines. By default MatchParen is enabled on all of my instances. Using gvim on my windows machine, it is doing exactly what I want - when my cursor is on a bracket, paren, etc. it visually highlights the match. It does not affect cursor navigation. However, on my Ubuntu boxes, when I move the cursor onto the character, it actually jumps to the match.
I'm sure that the behavior is caused by MatchParens because if I do a :NoMatchParen, it stops. Unfortunately, I also don't get the highlighting at that point. I can't figure out where my settings differ, though.
I'll like you even more if you can point me towards a plugin that will always highlight the closest enclosing pair of characters around my current position (like a code oriented version of MatchTagsAlways)
When showmatch is set, the cursor is actually jumping, and the following line fixes the problem:
set matchtime=0
More details:
http://vimdoc.sourceforge.net/htmldoc/options.html#'matchtime'
Just like FDinoff said in the accepted answer, this is probably only a problem of colors.
So if the color of the matching "paren" disorients you, tweaking colors of background and foreground is likely the solution.
But HOW do you do this?? ^^
I've had quite a journey through the vimdoc (it was not easy).
I've tested a whole bunch of variables and found that the relevant tweak is the [hi]ghlight command and specifically the MatchParen group.
The solution
Adding this in my .vimrc did the trick:
hi MatchParen ctermfg=208 ctermbg=bg
Note that vim config files are read from top to bottom, and some types of "words" are matched by several options. For example, a boolean could also be a keyword. Thus you have to pay attention to the order of these options.
How does this work?
My problem was that the background had the flashy color while the foreground had the color of the background of my terminal, which made it really confusing. Thus switching colors was the solution for me. But maybe you will have to tweak it differently.
First, you can check the current value for highlight MatchParen by entering the following command (while inside vim, in normal mode):
:hi MatchParen
You'll see hi MatchParen followed by XXX in the current style, followed by a list of argument=value separated by spaces.
The important arguments are ctermfg and ctermbg for the "terminal" vim, guifg and guibg for the "gui" vim. (Where fg means foreground and bg means background)
You can change a value and see the result in real time. Just put your cursor over a match character and enter the following command:
:hi MatchParen SomeArgument=SomeValue
This will not be saved, so don't worry. When you find a proper combination of values, you can add them in your .vimrc as shown above.
Personally, I set ctermfg to 208 (orange color) and ctermbg to bg (a keyword for the current background color, if known by vim).
If you use vim in a gui, take a look here for the available choice of colors.
The cursor isn't jumping. The color scheme probably has defined bad colors for the MatchParen highlight group which makes it look like the cursor is jumping.
Running default gVim (v7.4.461) without any configuration (i.e. no .vim files) in openSUSE 13.2 Legacy 32 Bit, :set showmatch? reveals that showmatch is on at start, which is not Vim's stated default behaviour. We can account for this by adding :set noshowmatch in our .vimrc.

How to make comments Italic in gVim?

I am fairly new to Vim and mainly use gVim for most of my coding purposes. I am trying to figure out what to add in my _vimrc (in windows) to make my comments italic.
I tried adding
highlight Comment cterm=italic
but that didn't work. My modifications so far in my vimrc (if it matters) is:
color slate
set number
set nowrap
set guioptions+=b
if has('gui_running')
set guifont=Consolas:h10
endif
So what can I do so that my comments appear in italics (consolas, italic, size 10)?
The cterm definition is only for high color terminals; for the GUI, you need to use the gui= argument:
highlight Comment cterm=italic gui=italic
Also, put this after the :colorscheme command in your ~/.vimrc, or else it might get overridden.

Vim different color scheme when printing

Is there a way in _vimrc to set a different colorscheme to be used when printing files?
I like a dark background light text scheme on screen, but obviously that doesn't translate well to paper.
Edit: I can change the scheme manually before printing, then change it back after and it works fine. Just curious if there's a way to tell Vim to always use a specific scheme while printing.
This is what :hardcopy outputs:
How about something like
:command Hardcopy let colors_save = g:colors_name <Bar> colorscheme default <Bar> hardcopy <Bar> execute 'colorscheme' colors_save
Maybe throw in the 'bg' option. If you care about local variables, make it a function:
command Hardcopy call Hardcopy()
function! Hardcopy()
let colors_save = g:colors_name
colorscheme default
hardcopy
execute 'colorscheme' colors_save
endfun
The Vim plugin "Printer Dialog" allows to configure printing parameters, one of them is the colorscheme to be used for printing.
After installing and configuring "Printer Dialog" press \pd in the Vim window you want to print. The following "dialog" will open:
Beneath other things the syntax-highlighting for printing can be selected. See :help printer-dialog for further details.
The variable g:prd_syntaxList defines the syntax-highlightings that can be selected. Default is
g:prd_syntaxList = "no,current,default,print_bw,zellner"
See :help prd_syntaxList for details on how to setup this feature.
:hardcopy will always print with a white background. From :help hardcopy:
The current highlighting colors are used in the printout, with the following
considerations:
1) The normal background is always rendered as white (i.e. blank paper).
2) White text or the default foreground is rendered as black, so that it shows
up!
3) If 'background' is "dark", then the colours are darkened to compensate for
the fact that otherwise they would be too bright to show up clearly on
white paper.
However, I'm not sure how exactly "[...] the colours are darkened to compensate [...]" works, so you may still want to go with #benjifisher's solution.
Alternatively, you could use :TOhtml to get an identical representation (definitely with a different colorscheme in this case), and then print that out in some other way. See :help TOhtml for relevant options, e.g. g:html_number_lines.
Using :TOhtml works quite well. It creates a html file with the same colour scheme being used.
I like the onehalf (link to github download instructions for vim) colour schemes because it it has a simple light and dark scheme. The syntax highlighting is the same between both schemes, besides white and black text which is switched.
So you can have a dark scheme on your computer, and then still print your code out and retain the general syntax highlighting which I find really useful.
this is what I do:
open the code I want to print
change to light scheme if not already in it by :colorscheme :onehalflight
run :TOhmtl which also opens the html file in vim
go to new html file, and save it to desired location
example, to save as text.html to downloads: :w ~/Downloads/text.html
open saved html file with your favourite browser, and print or do whatever you
want, this is nice because it allows you to see how many pages you are going to
print before you break your printer!

vim wombat colorscheme not showing up correctly in xterm

I put the wombat.vim in my ~/.vim/colors/ and i have :colorscheme wombat in my .vimrc and yet the colorscheme is not what wombat looks like.
The scheme looks like a cross between two schemes.
Not sure what is wrong. Any ideas?
Looking at source code of wombat.vim I realized that it sets values of guifg and guibg, and those are for a GUI vim. Not my case. So I download a version which set values ctermfg and ctermbg for non-GUI versions. You can get the file here and leave it in:
~/.vim/colors/wombat256.vim
Colorschemes work in a 256-color terminal. Check your term value
:set term?
In my case the value was xterm, so changed it to xterm-256color and worked.
:set term=xterm-256color
Try with another terminal emulator or, better yet, using gvim.
Most of the time discrepancies between the expected colours and the ones you actually get are due to the way your terminal handles them, in particular for colour schemes outside the standard 16 colours palette.
Remove the colon at the beginning of the line in your .vimrc

Is it possible to not display a ~ for blank lines in Vim/Neovim?

Is it possible to not display a ~ for blank lines in Vim?
This confuses Mac Vim's scrollbar, and I quite don't like these tildes.
:hi NonText guifg=bg
That command should set the color of non text characters to be the same as the background color.
Vim 8.x:
You can now change the color just for the end of the buffer ~:
highlight EndOfBuffer ctermfg=black ctermbg=black
See changelog for Vim 8.x.
As jamessan said, you can’t disable them. The scrolling behavior isn’t specific to MacVim, either — it works the same way in the terminal and in gvim:
Instead of seeing this as a problem, what you should do is learn to see this as part of Vim’s flexibility. For example, you can use the zt command to scroll the current line to the top of the screen, regardless of where in the file it is. This can make it easier to write macros that do some work and then scroll back to where you were. The commands <C-E> and <C-Y> are made simpler because of this, as is the 'scrolloffset' option.
If you must, retrain your brain to think of Vim’s scrollbar as mapping to which line is on top, instead of which screenful is visible.
For NeoVim, you can set the fillchars value for eob to a space character and that will effectively hide it. (This might not work for plain Vim).
In Lua (Nvim 0.5+):
vim.wo.fillchars='eob: '
In VimScript:
set fillchars=eob:\
Note: Calling the above will override your fillchars value for other items as well (if set), so use this as a reference to set multiple values together:
set fillchars=eob:\ ,fold:\ ,vert:\│
Or use set fillchars+=... to append it your existing values.
You can't disable them, but you can change your colorscheme such that the NonText highlight group is colored the same as the Normal highlight group. However, this affects more than just the end of document tildes.
I doubt that it's actually "confusing" MacVim's scrollbar and if it is, then that's a bug in the patching that MacVim does.
The tilde ~ characters are meant to remind the user that those lines are not part of buffer content.
The above highlight trick will hide the ~ character, but it is still there. For some terminals, this may not even work. If you happen to be a Neovim user, you can use fillchars option to change the end of buffer symbol like this:
set fillchars=fold:\ ,vert:\│,eob:\ ,msgsep:‾
This will use space instead of ~ for end of buffer, effectively hiding the annoying ~.
You may also be interested in discussions here.

Resources