How to Fix Missing Syntax Highlighting in Vim When SSHing to Older Linux Servers


2 views

When working across multiple servers via SSH, inconsistent Vim behavior can be frustrating. The key observations here are:

  • Syntax highlighting works on Server 2 (x86_64) but not Server 1 (i686)
  • Both servers report 8 colors via tput colors
  • The Vim versions are identical (7.0.237)
  • Manual :syntax on commands don't help

First, let's verify some critical settings:


# Check terminal type
echo $TERM

# Verify Vim's color detection
vim +"echo &t_Co" +q

# Check if syntax is actually enabled
vim +"echo has('syntax')" +q

For Server 1, we need to examine the compilation options more carefully. The :version output shows:


-syntax  # This is the critical missing feature
+terminfo

The Vim binary on Server 1 was compiled without syntax highlighting support (-syntax in features list). This is common in minimal/older distributions where maintainers disable features to reduce binary size.

Option 1: Install a Proper Vim Package

For RHEL/CentOS 5 systems:


# Check available packages
yum list vim*

# Install full-featured vim
yum install vim-enhanced

Option 2: Recompile Vim with Syntax Support

If you need to keep using the existing Vim:


# Download source
wget ftp://ftp.vim.org/pub/vim/unix/vim-7.0.tar.bz2
tar xjf vim-7.0.tar.bz2
cd vim70

# Configure with syntax support
./configure --with-features=normal --enable-syntax

# Compile and install
make
sudo make install

Option 3: Remote Editing Workaround

If you can't modify Server 1, edit files locally with proper syntax highlighting:


# From your local machine
vim scp://user@server1//path/to/file.php

Even with proper Vim, ensure your SSH client and terminal are configured correctly:


# In ~/.bashrc or SSH client config
export TERM=xterm-256color

# For older systems
export TERM=xterm-color

Add these to your ~/.vimrc for better color handling:


" Force color detection
if &t_Co == 8
    set t_Co=16
endif

" Basic color scheme for limited palettes
if &t_Co < 256
    colorscheme desert
endif

" Always try to enable syntax
if has("syntax")
    syntax on
endif

When working with Vim over SSH, syntax highlighting may fail to appear even when properly configured in /etc/vimrc or ~/.vimrc. This commonly occurs due to terminal emulation issues or missing Vim features in the compiled version.

First, verify your terminal's color capabilities:

tput colors
echo $TERM

For Server 1 in your case, both servers report 8 colors (tput colors returns 8), but syntax highlighting only works on Server 2. The key difference lies in Vim's compilation options.

Run :version and look for critical missing features:

VIM - Vi IMproved 7.0 (2006 May 7, compiled Aug  4 2010 07:21:18)
[...]
-syntax [...]

The -syntax flag indicates your Vim was compiled without syntax highlighting support. This explains why syntax on commands have no effect.

Option 1: Install a Full-Featured Vim

For CentOS/RHEL systems:

yum install vim-enhanced

For Debian/Ubuntu:

apt-get install vim

Option 2: Force Basic Colors

Add this to your ~/.vimrc:

" Force color support for limited terminals
if &t_Co == 8
  highlight Comment ctermfg=DarkGreen
  highlight Constant ctermfg=DarkRed
  highlight Identifier ctermfg=DarkCyan
  highlight Statement ctermfg=Brown
  highlight PreProc ctermfg=DarkMagenta
  highlight Type ctermfg=Blue
  highlight Special ctermfg=DarkRed
  highlight Underlined ctermfg=DarkBlue
endif

Option 3: Terminal Configuration

Ensure your SSH client and terminal emulator support colors:

export TERM=xterm-256color
ssh -Y user@server1

After making changes, test with:

vim --version | grep syntax
vim -c 'syntax on' test.php

For consistent results across SSH sessions, add to your ~/.bashrc:

# Ensure proper terminal type
if [ "$TERM" = "xterm" ]; then
    export TERM=xterm-256color
fi

# Alias to always use enhanced Vim
alias vim='vim -c "syntax on"'

If you can't install packages, consider using nano with syntax highlighting:

yum install nano
nano -Y sh test.php