You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vim-startify配置从Vimscript迁移至Lua:Git Modified与Untracked列表实现方法问询

Migrating Vim-startify Git Modified/Untracked Lists to Lua

Got it, let's get those git-specific lists working in your Lua config. Here's how to replicate your original Vimscript functionality perfectly:

Full Working Lua Config

-- Helper function to get git modified files
local function git_modified()
  -- Run git command and capture output as a list
  local files = vim.fn.systemlist('git ls-files -m 2>/dev/null')
  -- Filter out empty lines (avoids empty sections when not in a git repo)
  local non_empty_files = vim.tbl_filter(function(file)
    return file ~= ''
  end, files)
  -- Map each file to the structure startify expects
  return vim.tbl_map(function(file)
    return { line = file, path = file }
  end, non_empty_files)
end

-- Helper function to get git untracked files
local function git_untracked()
  local files = vim.fn.systemlist('git ls-files -o --exclude-standard 2>/dev/null')
  local non_empty_files = vim.tbl_filter(function(file)
    return file ~= ''
  end, files)
  return vim.tbl_map(function(file)
    return { line = file, path = file }
  end, non_empty_files)
end

-- Updated startify lists with git sections
vim.g.startify_lists = {
  { type = "dir", header = { " MRU " .. vim.fn.getcwd() } },
  { type = "sessions", header = { " Sessions" } },
  { type = "bookmarks", header = { " Bookmarks" } },
  { type = git_modified, header = { " git modified" } },
  { type = git_untracked, header = { " git untracked" } },
  { type = "commands", header = { " Commands" } },
}

Breakdown of Key Changes

  • Replacing Vimscript Functions: We use Lua's local function syntax instead of Vimscript's function!. The core git command stays the same—we just call it via vim.fn.systemlist (Lua's equivalent of Vimscript's systemlist()).
  • Handling Empty Results: Added vim.tbl_filter to strip out empty lines. This prevents startify from showing an empty git section when you're not in a Git repository.
  • Mapping to Startify's Expected Structure: vim.tbl_map does the same job as Vimscript's map(), converting each filename into the {line = ..., path = ...} table that startify uses to render list items.
  • Adding to startify_lists: Just like in Vimscript, we reference the Lua functions directly in the type field of each list entry.

内容的提问来源于stack exchange,提问作者abidibo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 14:57:27