【问题标题】:Syntax highlight works for gvim (GUI version of vim) but not for vim (terminal-based vim)语法高亮适用于 gvim(vim 的 GUI 版本)但不适用于 vim(基于终端的 vim)
【发布时间】:2019-02-22 19:26:12
【问题描述】:

我是 vim 新手,我想用 vim 编写代码。我已经为 TypeScript 中的语法高亮 JSX 安装了必要的插件,一切都适用于 GUI 版本的 Vim,但不适用于基于终端的 Vim。请帮我找出问题所在。以下是我用于语法高亮的插件。

  1. 对于 TypeScript:https://github.com/leafgarland/typescript-vim
  2. 对于 TypeScript 中的 JSX:https://github.com/peitalin/vim-jsx-typescript

我正在使用 Vundle 安装插件。我尝试对上面提到的第二个插件的 README 部分中给出的颜色进行一些自定义。据我所知,当我使用hi 命令时,它对基于终端的 Vim 没有任何影响,但在基于 GUI 的 Vim 上运行良好。另外,如果可能的话,请将我指向任何文章/博客,让初学者了解有关 vim 和 vimscript 的更多信息。

编辑:

我正在尝试为 React 开发的 TypeScript 中的 JSX 设置语法高亮,我已经安装了上面的插件来实现这一点。下面是我的 .vimrc 文件的内容。

set nocompatible                " choose no compatibility with legacy vi syntax enable
set encoding=utf-8
set showcmd                     " display incomplete commands
set t_Co=256
filetype plugin indent on       " load file type plugins + indentation

"" monkai theme
syntax enable
colorscheme monokai

set nu
set guifont=monacob\ bold\ 9

highlight ColorColumn ctermbg=gray
set colorcolumn=80

"" Whitespace
set nowrap                      " don't wrap lines
set tabstop=2 shiftwidth=2      " a tab is two spaces (or set this to 4)
set expandtab                   " use spaces, not tabs (optinal)
set backspace=indent,eol,start  " backspace through everything in insert mode

"" Searching
set hlsearch                    " highlight matches
set incsearch                   " incremental searching
set ignorecase                  " searches are case insensitive...
set smartcase                   " ... unless they contain at least one capital letter

" set the runtime path to include Vundle and initialize
set rtp +=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
Plugin 'tpope/vim-fugitive.git'
Plugin 'tpope/vim-rails'
Plugin 'leafgarland/typescript-vim'
Plugin 'peitalin/vim-jsx-typescript'
" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" autocmd FileType typescript.tsx setlocal commentstring={/*\ %s\ */}
autocmd BufNewFile,BufRead *.tsx,*.jsx set filetype=typescript.tsx
autocmd BufNewFile,BufRead *.ts,*.js set filetype=typescript.jsx

" dark red
hi tsxTagName guifg=#E06C75

" orange
hi tsxCloseString guifg=#F99575
hi tsxCloseTag guifg=#F99575
hi tsxAttributeBraces guifg=#F99575
hi tsxEqual guifg=#F99575

" yellow
hi tsxAttrib guifg=#F8BD7F cterm=italic

下面是 GUI 版 Vim 中 JSX 语法高亮的截图

下面是终端版本 Vim 中 JSX 语法高亮的截图

正如我们在 .vimrc 和屏幕截图中看到的,在文件末尾声明的 JSX 标签颜色等在 Gvim 中可以正常工作,但对于基于终端的 vim 无效。请帮助我理解我在这里做错了什么。

【问题讨论】:

  • 你看到了什么?为什么是错的?你期望看到什么?你做了什么究竟

标签: typescript vim vi vim-plugin vim-syntax-highlighting


【解决方案1】:
" dark red
hi tsxTagName guifg=#E06C75

使用guifg 属性,您只需为 GVIM 定义颜色。

从 Vim 8 开始,有 :help 'termguicolors' 选项;你可以尝试:set termguicolors;有些终端确实支持。

否则,您必须通过ctermfg / ctermbg 属性为彩色终端单独定义颜色(从有限的调色板,取决于可用颜色的数量,:set t_Co? 会告诉您)。例如:

hi tsxTagName guifg=#E06C75 ctermfg=DarkRed

通过插件的替代方法

CSApprox 这样的插件可以采用GUI 颜色定义并将它们转换为高度匹配的256 色cterm 调色板,用于高色彩终端。这有助于配色方案,否则只能从平淡无奇的默认 16 色终端调色板中选择,或者只提供 GUI 颜色定义。

csexact 采用了另一种方法,它修改(支持的)终端的调色板以完全匹配 Vim 的 GUI 颜色。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 2019-01-25
    相关资源
    最近更新 更多