使用argdo打开多个文件时,vim中的语法高亮显示未启用

使用argdo打开多个文件时,vim中的语法高亮显示未启用,vim,Vim,我经常从MacVim内部一次打开整套文件。为此,我通常使用以下命令: args *PATTERN* argdo tabedit 这会将工作目录中与模式匹配的所有文件加载到参数列表中,然后在单独的选项卡中打开它们。执行此操作时,语法突出显示不会自动打开,必须手动设置。如何解决此问题?我不熟悉args或argdo,但如果需要在不同选项卡中打开与模式匹配的文件,可以使用以下方法: :next * :tab ball :下一步*以正确的语法高亮显示方式打开模式中的所有文件,但是:tab ball打开

我经常从MacVim内部一次打开整套文件。为此,我通常使用以下命令:

args *PATTERN*
argdo tabedit

这会将工作目录中与模式匹配的所有文件加载到参数列表中,然后在单独的选项卡中打开它们。执行此操作时,语法突出显示不会自动打开,必须手动设置。如何解决此问题?

我不熟悉
args
argdo
,但如果需要在不同选项卡中打开与模式匹配的文件,可以使用以下方法:

:next *
:tab ball

:下一步*
以正确的语法高亮显示方式打开模式中的所有文件,但是
:tab ball
打开内存中的所有文件,如果您有不需要在选项卡中打开的缓冲区,这可能是一个问题。

argdo
语法
添加到
'eventignore'
设置中(请参见
:h argdo
). 这意味着这些文件没有任何突出显示,因为没有为该缓冲区触发语法自动命令事件。这使得它看起来像是没有设置
'filetype'
。事实并非如此。您可以通过执行
:set ft?
进行检查。您可以在上运行
:syntax以重新启用语法突出显示。但这并不是真正令人满意的,而且感觉很笨拙

可能更好的方法是不再使用选项卡,而是使用缓冲区和相关的缓冲区命令。Arglist相关的缓冲区命令有:
:下一个
:上一个
:第一个
,和
:最后一个
。您可以使用
:b file.c
:sb file.c
打开特定文件,以新的拆分方式打开缓冲区

我意识到这是一个难以下咽的药丸,当然有时你可能真的希望每个缓冲区都有自己的标签页。一旦你强迫自己更多地使用缓冲区,你就会发现很少需要标签。你可能想看看德鲁·尼尔的精彩表演。我还建议使用蒂姆·波普的方法更容易地在论点列表中移动

如果您真的必须让每个in-file在各自的选项卡中使用
:argdo tabe
,那么您可能应该在其后面加上
:syntax on
:tabdo doautocmd syntax

有关更多帮助,请参阅:

:h :argdo
:h arglist
:h buffers
:h :b
:h :sb
:h :next
:h :tabdo
:h :doa
:h Syntax
:h :syn-on

像这样的方法应该会奏效:

:argdo set eventignore-=Syntax | tabedit
语法
事件忽略设置中删除。

我已经在一个类似的问题中为
:bufdo
;下面是一个扩展版本,它也可以处理您的用例。解决
'eventignore'
的自动设置非常棘手,因此有充分的注释:

" Enable syntax highlighting when buffers are displayed in a window through
" :argdo and :bufdo, which disable the Syntax autocmd event to speed up
" processing.
augroup EnableSyntaxHighlighting
    " Filetype processing does happen, so we can detect a buffer initially
    " loaded during :argdo / :bufdo through a set filetype, but missing
    " b:current_syntax. Also don't do this when the user explicitly turned off
    " syntax highlighting via :syntax off.
    " The following autocmd is triggered twice:
    " 1. During the :...do iteration, where it is inactive, because
    " 'eventignore' includes "Syntax". This speeds up the iteration itself.
    " 2. After the iteration, when the user re-enters a buffer / window that was
    " loaded during the iteration. Here is becomes active and enables syntax
    " highlighting. Since that is done buffer after buffer, the delay doesn't
    " matter so much.
    " Note: When the :...do command itself edits the window (e.g. :argdo
    " tabedit), the BufWinEnter event won't fire and enable the syntax when the
    " window is re-visited. We need to hook into WinEnter, too. Note that for
    " :argdo split, each window only gets syntax highlighting as it is entered.
    " Alternatively, we could directly activate the normally effectless :syntax
    " enable through :set eventignore-=Syntax, but that would also cause the
    " slowdown during the iteration Vim wants to avoid.
    " Note: Must allow nesting of autocmds so that the :syntax enable triggers
    " the ColorScheme event. Otherwise, some highlighting groups may not be
    " restored properly.
    autocmd! BufWinEnter,WinEnter * nested if exists('syntax_on') && ! exists('b:current_syntax') && ! empty(&l:filetype) && index(split(&eventignore, ','), 'Syntax') == -1 | syntax enable | endif

    " The above does not handle reloading via :bufdo edit!, because the
    " b:current_syntax variable is not cleared by that. During the :bufdo,
    " 'eventignore' contains "Syntax", so this can be used to detect this
    " situation when the file is re-read into the buffer. Due to the
    " 'eventignore', an immediate :syntax enable is ignored, but by clearing
    " b:current_syntax, the above handler will do this when the reloaded buffer
    " is displayed in a window again.
    autocmd! BufRead * if exists('syntax_on') && exists('b:current_syntax') && ! empty(&l:filetype) && index(split(&eventignore, ','), 'Syntax') != -1 | unlet! b:current_syntax | endif
augroup END

有趣的是——这确实打开了所有带有语法高亮显示的文件,这正是我想要的。但它会打开所有加载的缓冲区,这是一个相当大的问题。使用args和argdo的目的是指定一组要打开的文件,而不管当前加载的是什么。不过,这仍然是一个不错的答案。我认为你应该编辑它,以明确这将以完整的语法突出显示打开它们,因为这就是问题所在。谢谢Peter的建议。我确实在很多时候使用缓冲区,但有时选项卡只是它的位置。当您有很好的:tabp和:tabn快捷方式时,在文件之间切换比使用缓冲区更容易。即使使用unpaird.vim之类的工具,我也发现选项卡切换更为优越,因为屏幕上始终可以很好地显示缓冲区名称。如果选项卡对您有效,那么请务必使用它们,但我必须说,当文件数量开始增加时,使用
4gt
之类的方法来获得适当的选项卡似乎有点愚蠢。然后,您可能会发现自己正在执行类似于
设置swb+=usetab
的操作,并开始使用
:sb file
。最终,当你得到100个文件时,这似乎是徒劳的。如果你在这里找到了自我,我建议你放弃每个标签一个缓冲区。在
vimrc
文件中放入
set hidden
,学习尽可能多的缓冲区命令,在拆分中找到乐趣,大量使用ctags/cscope,并使用大写字母标记。我已经知道
argdo
删除了synatx突出显示,但直到
eventignore
为止。谢谢。我喜欢简单。我完全沉浸在这个简单的解决方案中。感谢您的分享。这很好,但是它没有为arglist中的第一项打开语法。我认为这是因为set-eventignore-=语法实际上是在argdo命令打开缓冲区之后才运行的。基本上,这会对每个arglist项重复三个步骤:(1)在arglist中打开下一个缓冲区(在当前窗口中);(2) 设置eventignore-=语法;(3) 打开一个新选项卡(使用新的空缓冲区)。然后,当循环重复时,列表中的下一个参数将在空缓冲区中打开。一个不幸的副作用是你最终会得到一堆空的缓冲区。谢谢!这一个是赢家,因为它正是我所需要的。我也很欣赏它是如何工作的完整文档;很高兴我能帮忙。如果没有完整的文档,我将无法自己记住并扩展它。基于堆栈溢出问题改进自己的配置总是很好的。