在我的 .vimrc 中,如何检查配色方案是否存在?

发布于 2024-11-02 03:39:27 字数 46 浏览 3 评论 0原文

.vimrc 中,是否可以仅加载存在的配色方案?

In a .vimrc, is it possible to load a color scheme only if it exists?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

揪着可爱 2024-11-09 03:39:28

这是 nvim 的 LUA 解决方案:

local colorschemes = {
    'solarized',
    'desert',
}

for _, colorscheme in ipairs(colorschemes) do
    if pcall(vim.cmd.colorscheme, colorscheme) then
        break
    end
end

Here's LUA solution for nvim:

local colorschemes = {
    'solarized',
    'desert',
}

for _, colorscheme in ipairs(colorschemes) do
    if pcall(vim.cmd.colorscheme, colorscheme) then
        break
    end
end
走过海棠暮 2024-11-09 03:39:27

try-catch 中使用 :colorscheme 作为 Randy 已完成 如果您只想加载它(如果存在)并执行其他操作,则可能就足够了。如果您对 else 部分不感兴趣,可以使用简单的 :silent! colorcheme 就足够了。

否则, globpath() 是要走的路。然后,如果您确实愿意,可以检查使用 filereadable() 返回的每个路径。

" {rtp}/autoload/has.vim
function! has#colorscheme(name) abort
    let pat = 'colors/'.a:name.'.vim'
    return !empty(globpath(&rtp, pat))
endfunction

" .vimrc
if has#colorscheme('desert')
     ...

编辑: fileread($HOME.'/.vim/colors/'.name.'.vim') 可能看起来很简单,而且绝对有吸引力,但如果我们正在寻找的颜色方案,这还不够因为是在别处。通常,如果它已通过插件管理器安装在另一个目录中。在这种情况下,唯一可靠的方法是检查 vim 'runtimepath '(又名'rtp')。因此globpath()。请注意,:colorscheme name 命令在 {rtp}/colors/{name}.vim 中搜索。

Using :colorscheme in a try-catch as Randy has done may be enough if you just want to load it if it exists and do something else otherwise. If you are not interested in the else part, a simple :silent! colorscheme is enough.

Otherwise, globpath() is the way to go. You may, then, check each path returned with filereadable() if you really wish to.

" {rtp}/autoload/has.vim
function! has#colorscheme(name) abort
    let pat = 'colors/'.a:name.'.vim'
    return !empty(globpath(&rtp, pat))
endfunction

" .vimrc
if has#colorscheme('desert')
     ...

EDIT: filereadable($HOME.'/.vim/colors/'.name.'.vim') may seem simple and it's definitively attractive, but this is not enough if the colorscheme we're looking for is elsewhere. Typically if it has been installed in another directory thanks to a plugin manager. In that case the only reliable way is to check in the vim 'runtimepath' (a.k.a. 'rtp'). Hence globpath(). Note that :colorscheme name command searches in {rtp}/colors/{name}.vim.

勿忘心安 2024-11-09 03:39:27

@eckes 答案的替代方法是尝试加载颜色方案并处理错误(如果不存在):

try
    colorscheme mayormaynotexist
catch /^Vim\%((\a\+)\)\=:E185/
    " deal with it
endtry

An alternative to @eckes answer would be to try to load the colorscheme and deal with the error if it doesn't exist:

try
    colorscheme mayormaynotexist
catch /^Vim\%((\a\+)\)\=:E185/
    " deal with it
endtry
哀由 2024-11-09 03:39:27

您可以使用 fileread 函数检查颜色方案(例如 schemename)是否存在:在 ~/vimfiles/colors 下检查一次(Win32,对于 Unix,使用 ~/.vim/colors/),然后在 $VIMRUNTIME/colors/ 下:

if filereadable("/path/to/schemename.vim")
  colo schemename
endif

You could use the filereadable function to check if a color scheme (e.g. schemename) exists: check once under ~/vimfiles/colors (Win32, for Unix use ~/.vim/colors/) and once under $VIMRUNTIME/colors/:

if filereadable("/path/to/schemename.vim")
  colo schemename
endif
人生戏 2024-11-09 03:39:27

我的方法类似,

if filereadable( expand("$HOME/.vim/colors/railscast.vim") )
    colorscheme railscast
endif

这比硬编码整个路径更强大一点。

My method is similar,

if filereadable( expand("$HOME/.vim/colors/railscast.vim") )
    colorscheme railscast
endif

This is a little more robust than hardcoding the entire path.

飘逸的'云 2024-11-09 03:39:27

通常我会使用最喜欢的colorscheme,如果我最喜欢的颜色方案不可用,我会进行后备。嵌套的 try 将使这项工作有效:

try 
  colorscheme solarized
  catch
  try 
    colorscheme peachpuff
    catch
  endtry
endtry

如果两个 colorscheme 都不可用,则加载默认的(无论您的系统上发生什么情况)。如果一种或两种颜色方案不可用,则不会显示错误。将您喜欢的颜色方案放在第一位。

此外,不带参数的 catch 会捕获任何错误。如果您正在处理给出不同错误消息的不同区域设置,这会很方便。

Normally I use a favorite colorscheme with a fallback if my favorite is not available. A nested try will make this work:

try 
  colorscheme solarized
  catch
  try 
    colorscheme peachpuff
    catch
  endtry
endtry

If neither colorscheme is available, the default one is loaded (whatever that happens to be on your system). No errors are shown if one or both of the colorschemes are not available. Put your preferred colorscheme first.

Also, catch with no arguments catches any error. This is handy if you are dealing with different locales that give different error messages.

廻憶裏菂餘溫 2024-11-09 03:39:27

这是我在 .vimrc 文件中的内容。

if filereadable( expand("$HOME/.vim/colors/sublimemonokai.vim") )
    colorscheme sublimemonokai "https://github.com/ErichDonGubler/vim-sublime-monokai

    " vim-sublime-monokai only support 256 colours in terminal. If you are using a terminal which support truecolor like iterm2, enable the GUI color
    set termguicolors

    " Otherwise, use below setting to activate the 256 color in terminal
    set t_Co=256
else
    echom "The sublimemonokai.vim were not found to be used as colorscheme. elflord will be set for the timebeing..."
    colorscheme elflord
endif

基本上它会检查机器上是否存在我喜欢的配色方案。如果是,它将选择它并应用它所需的所有设置。否则它会选择 vim 附带的合适的配色方案。

通过查看其他答案,我的答案与 @user427390 答案共享一部分,并且它有一个额外的 else 条件。

以下链接对我编写自己的 .vimrc 和 vim 相关文件的脚本有很大帮助:
http://learnvimscriptthehardway.stevelosh.com/

This is wat I have in my .vimrc file.

if filereadable( expand("$HOME/.vim/colors/sublimemonokai.vim") )
    colorscheme sublimemonokai "https://github.com/ErichDonGubler/vim-sublime-monokai

    " vim-sublime-monokai only support 256 colours in terminal. If you are using a terminal which support truecolor like iterm2, enable the GUI color
    set termguicolors

    " Otherwise, use below setting to activate the 256 color in terminal
    set t_Co=256
else
    echom "The sublimemonokai.vim were not found to be used as colorscheme. elflord will be set for the timebeing..."
    colorscheme elflord
endif

basically it does check to see if the color scheme I like exists on the machine or not. If it does, it will select it and apply all the setting necessary for it. Otherwise it choose a suitable color scheme that is shipped with vim.

By looking at other answers, my answer shares bit part with @user427390 answer and it has an additional else condition.

The following link has helped me a lot in scripting my own .vimrc and vim related files:
http://learnvimscriptthehardway.stevelosh.com/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文