一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

typst-preview.nvim:实践指南

时间:2026-09-12 14:32:01 编辑:袖梨 来源:一聚教程网

在项目中评估typst-preview.nvim,可以先看清用途边界:Neovim 的低延迟打字机预览。一旦进入日常自动化环节,输入边界、依赖和失败处理如果不清楚就很难稳定复用会直接影响交付,这也是我最关心的风险。更实际的做法是用一项范围明确的真实任务完成最小试跑,同时核对配置时间、输出质量、异常信息和维护痕迹,不要直接在关键项目上。如果团队属于愿意先做小范围验证并复查原始文档的团队,它有继续测试的理由;否则先看替代方案会更省时间。

<h1> ✨ Typst Preview for Neovim ✨ </h1>

Myriad-Dreamin/tinymist 的 Neovim 插件。

https://github.com/chomosuke/typst-preview.nvim/assets/38484873/9f8ecf0f-aa1c-4edb-85a9-96a8005e8f25

特点

  • 低延迟预览:在输入时立即预览文档。增量渲染技术 使预览延迟尽可能低。
  • 代码和预览之间的交叉跳转。您可以点击预览跳转到 相应的代码位置,并让预览在 Neovim 中跟随光标移动。

安装

依赖关系

  • curl

Lazy.nvim:

{
  'chomosuke/typst-preview.nvim',
  lazy = false, -- or ft = 'typst'
  version = '1.*',
  opts = {}, -- lazy.nvim will implicitly calls `setup {}`
}

Packer.nvim:

use {
  'chomosuke/typst-preview.nvim',
  tag = 'v1.*',
  config = function()
    require 'typst-preview'.setup {}
  end,
}

vim 插件:

Plug 'chomosuke/typst-preview.nvim', {'tag': 'v1.*'}

注意: 您可以通过固定此的次要版本来固定打字员的次要版本 插件,i.e.,v1.1.* 而不是 v1.*

用法

命令/功能:

  • :TypstPreviewUpdaterequire 'typst-preview'.update()
    • 下载必要的二进制文件 vim.fn.fnamemodify(vim.fn.stdpath 'data' .. '/typst-preview/', ':p').
    • 必须在运行任何其他命令之前运行此命令。它是隐式运行的 when calling setup {}.
  • :TypstPreview:
    • 开始预览。或者,可以指定所需的预览模式: :TypstPreview document (default) or :TypstPreview slide for slide mode.
  • :TypstPreviewStop:
    • 停止预览。
  • :TypstPreviewToggle:
    • 切换预览。
  • :TypstPreviewFollowCursorrequire 'typst-preview'.set_follow_cursor(true)
    • 当光标移动时滚动预览。
    • 默认情况下此功能处于启用状态。
  • :TypstPreviewNoFollowCursorrequire 'typst-preview'.set_follow_cursor(false)
    • 当光标移动时不要滚动预览。
  • :TypstPreviewFollowCursorTogglerequire 'typst-preview'.set_follow_cursor(not init.get_follow_cursor()).
  • :TypstPreviewSyncCursorrequire 'typst-preview'.sync_with_cursor()
    • 将预览滚动到当前光标位置。这可以与以下组合使用 :TypstPreviewNoFollowCursor so that the preview only scroll to the current cursor position when you want it to.

⚙配置

该插件应该开箱即用,无需配置。然而,调用 需要 setup() 以确保插件依赖的二进制文件是 已经下载并且是最新的。

默认

require 'typst-preview'.setup {
  -- Setting this true will enable logging debug information to
  -- `vim.fn.stdpath 'data' .. '/typst-preview/log.txt'`
  debug = false,

  -- Custom format string to open the output link provided with %s
  -- Example: open_cmd = 'firefox %s -P typst-preview --class typst-preview'
  open_cmd = nil,

  -- Custom port to open the preview server. Default is random.
  -- Example: port = 8000
  port = 0,

  -- Custom host to bind the preview server to.
  -- Note that '0.0.0.0' is not supported and [won't be](https://github.com/Myriad-Dreamin/tinymist/issues/2105)
  -- Example: host = '192.168.0.10'
  host = '127.0.0.1',

  -- Setting this to 'always' will invert black and white in the preview
  -- Setting this to 'auto' will invert depending if the browser has enable
  -- dark mode
  -- Setting this to '{"rest": "<option>","image": "<option>"}' will apply
  -- your choice of color inversion to images and everything else
  -- separately.
  invert_colors = 'never',

  -- Only render the visible portion of the document.
  -- Improves performance significantly for large documents.
  -- Disable if you experience rendering issues.
  partial_rendering = true,

  -- Whether the preview will follow the cursor in the source file
  follow_cursor = true,

  -- Provide the path to binaries for dependencies.
  -- Setting this will skip the download of the binary by the plugin.
  -- Warning: Be aware that your version might be older than the one
  -- required.
  dependencies_bin = {
    tinymist = nil,
    websocat = nil
  },

  -- A list of extra arguments (or nil) to be passed to previewer.
  -- For example, extra_args = { "--input=ver=draft", "--ignore-system-fonts" }
  extra_args = nil,

  -- This function will be called to determine the root of the typst project
  get_root = function(path_of_main_file)
    local root = os.getenv 'TYPST_ROOT'
    if root then
      return root
    end

    -- Look for a project marker so imports from parent dirs stay inside root
    local main_dir = vim.fs.dirname(vim.fn.fnamemodify(path_of_main_file, ':p'))
    local found = vim.fs.find({ 'typst.toml', '.git' }, { path = main_dir, upward = true })
    if #found > 0 then
      return vim.fs.dirname(found[1])
    end

    return main_dir
  end,

  -- This function will be called to determine the main file of the typst
  -- project.
  get_main_file = function(path_of_buffer)
    return path_of_buffer
  end,
}

使用Mason安装的tinymist

dependencies_bin 选项设置为 dependencies_bin = { tinymist = 'tinymist' } 或,在 Windows 上 dependencies_bin = { tinymist = 'tinymist.cmd' } 应指向 梅森安装tinymist。

❓ 与其他工具的比较

Enter-tainer/typst-preview 的作者写了一篇 很好比较这里的here。

  • niuiic/typst-preview.nvim:由于 niuiic/typst-preview.nvim 使用 typest-lsp,它具有类似的优点和 提到的 typest-lsp 的缺点 这里:
    • 由于 PDF 读卡器存在延迟,因此延迟较高。
    • 不支持代码和预览之间的交叉跳转。

热门栏目