迁移文件类型自动命令设置从 vim 脚本到 Lua

huangapple go评论55阅读模式
英文:

Migrating Filetype autocmd setting from vimscript to lua

问题

我正在将我的vim脚本设置迁移到lua,但遇到了一个问题。

我有几个这种类型的autocmd设置,但我不知道如何迁移它们:

augroup py_fold_custom
    autocmd!
    autocmd FileType python setlocal foldmethod=indent
augroup END
augroup html_syntax_disable
    autocmd!
    autocmd Filetype html if getfsize(expand("%")) > 500000 | setlocal syntax=off | endif
augroup END

有人能帮忙吗?

英文:

I'm migrating my vimscript settings to lua, but hit a snag.

I got several of these type of autocmd settings which I don't have a clue how to migrate them

augroup py_fold_custom
    autocmd!
    autocmd FileType python setlocal foldmethod=indent
augroup END
augroup html_syntax_disable
    autocmd!
    autocmd Filetype html if getfsize(expand("%")) > 500000 | setlocal syntax=off | endif
augroup END

Can somebody help?

答案1

得分: 1

使用neovim是个不错的选择。我知道将这类命令转换成Lua可能会让人感到有些不知所措。虽然我已经转换了大部分的配置,但有一个巨大的 vim 脚本命令,我还没能够成功转换。

在neovim中,你有几种选项可以将这些转换成Lua。

首先是使用 autocommands(自动命令):

local generalSettingsGroup = vim.api.nvim_create_augroup('General settings', { clear = true })

vim.api.nvim_create_autocmd('FileType', {
    pattern = { '*.py' },
    callback = function()
        vim.opt.foldmethod = 'indent'
    end,
    group = generalSettingsGroup,
})

vim.api.nvim_create_autocmd('FileType', {
    pattern = { '*.html' },
    callback = function()
        if vim.api.nvim_buf_line_count(0) > 500000 then
            vim.bo.syntax = 'off'
        end
    end,
    group = generalSettingsGroup,
})

但你也可以使用 ftplugin 目录来处理这类事情。你仍然可以使用Lua,在检测到这些文件类型时,neovim将会执行这些代码,只需放在该目录中。

在你的配置目录中创建一个文件 ftplugin/python.lua,文件内容如下:

vim.opt.foldmethod = 'indent'

对于 HTML 文件,创建 ftplugin/html.lua 文件:

if vim.api.nvim_buf_line_count(0) > 500000 then
    vim.bo.syntax = 'off'
end

这是我脑海中首先想到的主要方法。希望这对你有所帮助!

英文:

Good choice switching to neovim. I know how it feels overwhelming to translate these sort of commands. While most of my config has been converted, I have one huge monster vim script command that I've yet to be able to translate.

As with everything in neovim you have several options of how to convert these to lua.

First, using autocommands

local generalSettingsGroup = vim.api.nvim_create_augroup('General settings', { clear = true })

vim.api.nvim_create_autocmd('FileType', {
    pattern = { '*.py' },
    callback = function()
        vim.opt.foldmethod = 'indent'
    end,
    group = generalSettingsGroup,
})

vim.api.nvim_create_autocmd('FileType', {
    pattern = { '*.html' },
    callback = function()
        if vim.api.nvim_buf_line_count(0) > 500000 then
            vim.bo.syntax = 'off'
        end
    end,
    group = generalSettingsGroup,
})

But you could also use the ftplugin directory for things like this.
You can still use lua, and upon detection of these file types this code will be run by neovim just by being in this directory.

In a file in your config directory create ftplugin/python.lua with this in the file

vim.opt.foldmethod = 'indent'

And for the html one ftplugin/html.lua

if vim.api.nvim_buf_line_count(0) > 500000 then
    vim.bo.syntax = 'off'
end

Those are the main ways that come to mind for me. I hope this helps!

huangapple
  • 本文由 发表于 2023年2月16日 01:47:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定