Vim在突出显示和非突出显示语法之间切换

时间:2015-04-09 12:18:18

标签: vim syntax-highlighting vim-syntax-highlighting

这是我的问题:

  1. 我打开一个文件vim /etc/file.toml,其语法高亮显示
  2. 我编辑并保存
  3. 有时(不是每次),当我稍后重新打开文件时,它不再是语法高亮显示。
  4. 有人知道如何解决这个问题吗?缓存问题?

    注意:几乎每个文件扩展名都会出现此问题,我不切换用户(总是root)。

1 个答案:

答案 0 :(得分:0)

尝试在.vimrc中设置syntax enable而不是syntax on

直接来自:h syntax

This command switches on syntax highlighting: >

    :syntax enable

What this command actually does is to execute the command >
    :source $VIMRUNTIME/syntax/syntax.vim

If the VIM environment variable is not set, Vim will try to find
the path in another way (see |$VIMRUNTIME|).  Usually this works just
fine.  If it doesn't, try setting the VIM environment variable to the
directory where the Vim stuff is located.  For example, if your syntax files
are in the "/usr/vim/vim50/syntax" directory, set $VIMRUNTIME to
"/usr/vim/vim50".  You must do this in the shell, before starting Vim.

                            *:syn-on* *:syntax-on*
The ":syntax enable" command will keep your current color settings.  This
allows using ":highlight" commands to set your preferred colors before or
after using this command.  If you want Vim to overrule your settings with the
defaults, use: >
    :syntax on

阅读Here了解详情。

编辑:经过一些研究后,我发现如果上述方法不起作用,那是因为该特定文件类型的文件类型插件会覆盖用户设置。要解决此问题,您可以在home vim文件夹中创建一个名为after的新文件夹。 after中的每个文件都来自/usr/share/vim74中的任何文件。 after的结构必须与vim74文件夹文件夹的结构匹配。

为了方便起见,我写了一个快速的脚本

#!/usr/bin/bash

# Make the after folders you need
mkdir -p $HOME/.vim/after/ftplugin

# Create the Global Settings file for syntax highlighting and formatting options
touch $HOME/.vim/after/fo_globals.vim

# Create links to the fo_globals file
for file in /usr/share/vim/vim74/ftplugin/*.vim
    do
        ln -s $HOME/.vim/after/fo_globals.vim $HOME/.vim/after/ftplugin/`basename $file`
    done

然后使用语法和格式选项填写您的globals文件。我看起来像这样。

syntax enable
set formatoptions-=c
set formatoptions-=r
set formatoptions-=o
set autoindent
set smartindent
set nocindent
set backspace=indent,eol,start

set expandtab
set tabstop=4
set shiftwidth=4
set shiftround