【问题标题】:Vim switching between highlighted and non-highlighted syntaxVim 在高亮和非高亮语法之间切换
【发布时间】:2015-06-14 19:19:55
【问题描述】:

这是我的问题:

  1. 我用vim /etc/file.toml打开一个文件,它的语法高亮显示
  2. 我编辑它并保存它
  3. 有时(不是每次),当我稍后重新打开文件时,它不再突出显示语法。

有人知道如何解决这个问题吗?缓存问题?

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

【问题讨论】:

  • 我们需要检查您的.vimrc 和使用过的插件列表以提供帮助。 :syntax on 会恢复东西吗?
  • 我使用默认的.vimrc(我刚刚添加了set bg=dark)。 :syntax on 没有解决问题。
  • :hi 输出中还有颜色吗? :colorscheme default 会恢复东西吗?
  • 是的,:hi 中有颜色,:colorscheme default 什么都不做……
  • 你有vim相关的环境变量吗? env | grep VIM

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


【解决方案1】:

尝试在.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了解更多信息。

编辑:经过更多研究,我发现如果上述方法不起作用,那是因为该特定文件类型的文件类型插件正在覆盖用户设置。为了解决这个问题,您可以在您的主 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

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

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

【讨论】:

  • 我会尽快尝试,但实际上我无法访问我的虚拟机。我会与您保持联系。
  • 你给我的提示没有变化
  • 嗯。我记得不久前遇到了完全相同的问题,但不幸的是我似乎不记得我做了什么来解决它。我可以看看你的 .vimrc。此外,下次出现此问题时,运行 :5verbose set filetype? 并运行 :5verbose set syntax? 并让我知道它的内容。我会看看我是否不记得我在解决问题的 vimrc 中所做的更改。
  • 这两个命令给了我这个:/usr/share/vim/vim74/filetype.vim/usr/share/vim/vim74/syntax/syntax.vim
  • 这两个命令返回这个:/usr/share/vim/vim74/filetype.vim/usr/share/vim/vim74/syntax/syntax.vim(当语法高亮打开时,它会在今天早上重新出现)。当没有高亮显示时,命令返回filetype=syntax=(两分钟前又消失了)。
猜你喜欢
  • 1970-01-01
  • 2012-08-16
  • 2014-11-14
  • 2011-06-14
  • 2011-01-19
  • 2015-08-08
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多