36

When I run vim from the command line in iTerm, syntax highlighting doesn't seem to work locally.

In vim for example I have installed a nice colorscheme that works quite well in MacVim but it would be great if in iTerm it showed the same one.

Any ideas how I can turn this on?

This is the color scheme I'm trying to use http://www.vim.org/scripts/script.php?script_id=2340

sohnryang
  • 725
  • 2
  • 13
  • 27
Derek Organ
  • 8,323
  • 17
  • 56
  • 75

7 Answers7

84

In order to turn code highlighting on in vim, try to enable the syntax plugin:

:syntax enable
Sean
  • 5,233
  • 5
  • 22
  • 26
80

That color scheme looks like it only supports 256-color terminals. If Vim thinks that your terminal only supports 8 colors, you won't see that specific color scheme.

You can check this in Vim by:

:echo &t_Co

If that returns 8, this might be the problem. Try setting it to 256 in your ~/.vimrc and see if that helps:

let &t_Co=256
Curt Nelson
  • 3,132
  • 19
  • 13
  • I would presume that there must be some setting in iTerm, to allow 256 colours? – Derek Organ Sep 22 '10 at 11:25
  • 1
    Doesn't work for me under the 10.6 default terminal. It causes the text vim to flash black and white. – James McMahon Dec 10 '10 at 00:42
  • Apparently the mac terminal only supports 16 colors, http://jimlund.org/blog/?p=130. – James McMahon Dec 10 '10 at 00:51
  • 7
    New versions of iTerm2 now correctly set TERM=xterm-256color, so vim should know the correct number of colours are supported without setting any variables if you have a recent enough version. Otherwise, you can set this value in iTerm's preferences under the 'Terminal' tab. – Matt Sep 15 '11 at 08:08
  • 1
    The ":echo &t_Co" command doesn't return anything for me. (iTerm on Snow Leopard) – Niels Bom Jan 18 '12 at 21:42
  • wow thank you so much, this has bothered me for YEARS and I never bothered trying to fix it. – subelsky Mar 25 '12 at 20:45
  • Not "8 colors", 8-bit colors. Meaning 256 colors. Just pointing that out, haha – JackHasaKeyboard Jul 02 '16 at 01:39
  • 2
    Hmmm. I have the latest version of iTerm2. The `echo &t_Co` reports 256. I still don't get the same syntax highlighting that MacVim gets automatically. – timbo Oct 19 '16 at 22:21
28

To those who still have problem.

iTerm 2 -> Preferences -> Profiles -> Colors -> Minimum contrast -> lowest

setup

sohnryang
  • 725
  • 2
  • 13
  • 27
yqrashawn
  • 413
  • 5
  • 8
13

I've had this problem before, as well as some related issues, so I'll summarize what I found.

  1. Make sure iTerm is set to use 256 colors. Try $ echo $TERMat the command line, and if you don't see xterm-256color then follow the directions in this answer.

  2. Set up your vimrc to handle other terminals as well. The regular Terminal in Snow Leopard only supports 8 colors for instance and will blink if you try to use a 256 color color scheme (I just don't set one in that case). Here's what I have:

    " enable 256 colors in GNOME terminal (for my Ubuntu VM)
    if $COLORTERM == 'gnome-terminal'
        set t_Co=256
    endif
    
    " set your color scheme (replace wombat with whatever yours is called)
    " if you're using a gvim or macvim, then your color scheme may have a version
    " that uses more than 256 colors
    if has("gui_running")
        colorscheme wombat
    elseif &t_Co == 256
        colorscheme wombat256
    endif
    
    " turn on language specific syntax highlighting
    syntax on
    
Community
  • 1
  • 1
Dean
  • 8,632
  • 6
  • 45
  • 61
4

Despite following all the advice in this (and other, similar) questions, I eventually found my trouble in a forgotten part of a vimrc I had taken from somewhere on the web years ago (because it was rather nicely organised), and then extensively modified for my own purposes.

But the problem area was in a little group of settings that I had never touched, back in the original file I started with. The relevant bit of the .vimrc was:

   " GVIM- (here instead of .gvimrc)
   if has('gui_running')
          set guioptions-=T               " remove the toolbar
          set lines=40                    " 40 lines of text instead of 24,
   else
           set term=builtin_ansi       " Make arrow and other keys work
   endif

Unsurprisingly (in retrospect), that "set term" line resets things so that regardless of what type your terminal is reporting in the TERM environment variable, you wind up with a generic, 8-color ANSI terminal. Setting 'term' explicitly inside the .vimrc is probably a very bad idea, just like setting t_Co directly.

I removed this whole block (And put the gvim settings into .gvimrc, where they belong), and everything has been working correctly for me ever since.

Trevor Powell
  • 1,154
  • 9
  • 20
3

After trying all the other answers here, the final thing I needed was:

set termguicolors
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
3

Edit sudo vim ~/.vimrc and add "syntax on" should fix issue.

bharathvn
  • 33
  • 4